JAVA的服务器和客户端怎么做
importjava.net.Socket;importjava.net.ServerSocket;importjava.io.DataInputStream;impor...
import java.net.Socket;
import java.net.ServerSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class HeLangServer
{
public static void main(String[] args)
{
System.out.print("启动菔务噐(何)");
try
{
ServerSocket aa=new ServerSocket(7700);
System.out.print("等待客户端连接...");
Socket bb=aa.accept();
System.out.print("ser.." +bb);
DataInputStream cc=new DataInputStream(bb.getInputStream());
DataOutputStream dd=new DataOutputStream(bb.getOutputStream());
String message=cc.readUTF();
String serverMessage="";
// int suiji=Integer.parseInt("!");
while (!message.equals("end"))
{
System.out.print("客户端 "+message);
if(message.equals("何"))
{
serverMessage="恭囍伱囬答㊣确";
}
}
}
catch(Exception e)
{
}
}
} 展开
import java.net.ServerSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class HeLangServer
{
public static void main(String[] args)
{
System.out.print("启动菔务噐(何)");
try
{
ServerSocket aa=new ServerSocket(7700);
System.out.print("等待客户端连接...");
Socket bb=aa.accept();
System.out.print("ser.." +bb);
DataInputStream cc=new DataInputStream(bb.getInputStream());
DataOutputStream dd=new DataOutputStream(bb.getOutputStream());
String message=cc.readUTF();
String serverMessage="";
// int suiji=Integer.parseInt("!");
while (!message.equals("end"))
{
System.out.print("客户端 "+message);
if(message.equals("何"))
{
serverMessage="恭囍伱囬答㊣确";
}
}
}
catch(Exception e)
{
}
}
} 展开
2个回答
展开全部
楼上的太长了吧,我写个简单的
import java.net.*;
import java.io.*;
import java.util.*;
/**
* TCP协议下单向通信的客户端程序。
* @author new
*
*/
public class TCPClientA {
public static void main(String[] args) {
Socket s=null;
try {
s=new Socket("127.0.0.1",8888);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br.readLine();
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(s!=null)try{s.close();}catch(IOException e){}
}
}
}
================================
import java.net.*;
import java.io.*;
import java.util.*;
/**
* TCP协议下单向通信的服务器端程序。
* @author new
*
*/
public class TCPServerA {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintStream ps=null;
try {
ss=new ServerSocket(8888);
while(true){
System.out.println("服务器已启动,在8888端口:");
s=ss.accept();
System.out.println("得到 "+s.getInetAddress()+" 的连接请求...");
ps=new PrintStream(s.getOutputStream());
ps.println(new Date());
ps.flush();
System.out.println("已向客户端发送数据!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(ps!=null)ps.close();
if(s!=null)try{s.close();}catch(IOException e){}
if(ss!=null)try{ss.close();}catch(IOException e){}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
/**
* TCP协议下单向通信的客户端程序。
* @author new
*
*/
public class TCPClientA {
public static void main(String[] args) {
Socket s=null;
try {
s=new Socket("127.0.0.1",8888);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br.readLine();
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(s!=null)try{s.close();}catch(IOException e){}
}
}
}
================================
import java.net.*;
import java.io.*;
import java.util.*;
/**
* TCP协议下单向通信的服务器端程序。
* @author new
*
*/
public class TCPServerA {
public static void main(String[] args) {
ServerSocket ss=null;
Socket s=null;
PrintStream ps=null;
try {
ss=new ServerSocket(8888);
while(true){
System.out.println("服务器已启动,在8888端口:");
s=ss.accept();
System.out.println("得到 "+s.getInetAddress()+" 的连接请求...");
ps=new PrintStream(s.getOutputStream());
ps.println(new Date());
ps.flush();
System.out.println("已向客户端发送数据!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(ps!=null)ps.close();
if(s!=null)try{s.close();}catch(IOException e){}
if(ss!=null)try{ss.close();}catch(IOException e){}
}
}
}
展开全部
给你个例子。。
KKMultiServer.java
import java.net.*;
import java.io.*;
public class KKMultiServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}
KKMultiServerThread.java
import java.net.*;
import java.io.*;
public class KKMultiServerThread extends Thread
{
private Socket socket = null;
public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null)
{
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
KnockKnockClient.java
import java.io.*;
import java.net.*;
public class KnockKnockClient
{
public static void main(String[] args) throws IOException
{
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("hostname", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: biao.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
KnockKnockProtocol.java
import java.net.*;
import java.io.*;
public class KnockKnockProtocol
{
private static final int WAITING = 0;
private static final int SENTKNOCKKNOCK = 1;
private static final int SENTCLUE = 2;
private static final int ANOTHER = 3;
private static final int NUMJOKES = 5;
private int state = WAITING;
private int currentJoke = 0;
private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
private String[] answers = { "Turnip the heat, it's cold in here!",
"I didn't know you could yodel!",
"Bless you!",
"Is there an owl in here?",
"Is there an echo in here?" };
public String processInput(String theInput)
{
String theOutput = null;
if (state == WAITING) {
theOutput = "Knock! Knock!";
state = SENTKNOCKKNOCK;
}
else if (state == SENTKNOCKKNOCK) {
if (theInput.equalsIgnoreCase("Who's there?")) {
theOutput = clues[currentJoke];
state = SENTCLUE;
}
else {
theOutput = "You're supposed to say \"Who's there?\"! " +
"Try again. Knock! Knock!";
}
}
else if (state == SENTCLUE) {
if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
theOutput = answers[currentJoke] + " Want another? (y/n)";
state = ANOTHER;
} else {
theOutput = "You're supposed to say \"" + clues[currentJoke] + " who?\"" +
"! Try again. Knock! Knock!";
state = SENTKNOCKKNOCK;
}
}
else if (state == ANOTHER) {
if (theInput.equalsIgnoreCase("y")) {
theOutput = "Knock! Knock!";
if (currentJoke == (NUMJOKES - 1))
currentJoke = 0;
else
currentJoke++;
state = SENTKNOCKKNOCK;
}
else {
theOutput = "Bye.";
state = WAITING;
}
}
return theOutput;
}
}
客户端和服务器分别是那个类不用我告诉你了撒
KKMultiServer.java
import java.net.*;
import java.io.*;
public class KKMultiServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}
KKMultiServerThread.java
import java.net.*;
import java.io.*;
public class KKMultiServerThread extends Thread
{
private Socket socket = null;
public KKMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run()
{
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null)
{
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
KnockKnockClient.java
import java.io.*;
import java.net.*;
public class KnockKnockClient
{
public static void main(String[] args) throws IOException
{
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("hostname", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: biao.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
KnockKnockProtocol.java
import java.net.*;
import java.io.*;
public class KnockKnockProtocol
{
private static final int WAITING = 0;
private static final int SENTKNOCKKNOCK = 1;
private static final int SENTCLUE = 2;
private static final int ANOTHER = 3;
private static final int NUMJOKES = 5;
private int state = WAITING;
private int currentJoke = 0;
private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
private String[] answers = { "Turnip the heat, it's cold in here!",
"I didn't know you could yodel!",
"Bless you!",
"Is there an owl in here?",
"Is there an echo in here?" };
public String processInput(String theInput)
{
String theOutput = null;
if (state == WAITING) {
theOutput = "Knock! Knock!";
state = SENTKNOCKKNOCK;
}
else if (state == SENTKNOCKKNOCK) {
if (theInput.equalsIgnoreCase("Who's there?")) {
theOutput = clues[currentJoke];
state = SENTCLUE;
}
else {
theOutput = "You're supposed to say \"Who's there?\"! " +
"Try again. Knock! Knock!";
}
}
else if (state == SENTCLUE) {
if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
theOutput = answers[currentJoke] + " Want another? (y/n)";
state = ANOTHER;
} else {
theOutput = "You're supposed to say \"" + clues[currentJoke] + " who?\"" +
"! Try again. Knock! Knock!";
state = SENTKNOCKKNOCK;
}
}
else if (state == ANOTHER) {
if (theInput.equalsIgnoreCase("y")) {
theOutput = "Knock! Knock!";
if (currentJoke == (NUMJOKES - 1))
currentJoke = 0;
else
currentJoke++;
state = SENTKNOCKKNOCK;
}
else {
theOutput = "Bye.";
state = WAITING;
}
}
return theOutput;
}
}
客户端和服务器分别是那个类不用我告诉你了撒
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询