我正在制作一个 tcp 客户端服务器聊天程序。我的服务器是由线程制作的,其代码如下:
System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
System.out.println("Run the Client");
//ready to accept client request
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
//using output stream responsing data
out=new PrintStream(client.getOutputStream());
out.print("Welcome by server\n");
System.out.println("1");
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
while(! in.readLine().trim().equals("*")) {
//using output stream responsing data
System.out.println("3");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.println(str);
out.flush();
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
}
我的客户文件是:(简单,没有线程)
client =new Socket("127.0.0.1", 9867);
System.out.println("Client connected ");
//getting the o/p stream of that connection
out=new PrintStream(client.getOutputStream());
//sending the message to server
out.print("Hello from client\n");
//reading the response using input stream
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out=new PrintStream(client.getOutputStream());
out.print(str);
out.flush();
while(! in.readLine().trim().equals("*")) {
// opening the input stream to read data from server connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("4");
System.out.println(in.readLine());
System.out.println("5");
out=new PrintStream(client.getOutputStream());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.print(str);
out.flush();
}
现在对我来说发生了一件奇怪的事情。当我注释掉两个文件上的 while 循环时,程序运行良好。但是当我在向服务器发送味精后取消评论时,我的客户端进入while循环,我的服务器继续等待响应。
所以输出是:(客户端)
Client connected
Welcome by server
l;skc
4
和服务器是:
Server binded at 192.168.1.242:9867
Run the Client
Hello from client
1
我认为这是由于使用了线程/异步进程。请帮助
编辑代码
服务器端:
System.out.println("Server binded at "+((client.getInetAddress()).getLocalHost()).getHostAddress()+":9867");
System.out.println("Run the Client");
//ready to accept client request
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
//using output stream responsing data
out=new PrintStream(client.getOutputStream());
out.print("Welcome by server\n");
System.out.println("1");
str = in.readLine();
System.out.println(str);
while(!str.trim().equals("*")) {
//using output stream responsing data
System.out.println("3");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out.println(str);
out.flush();
//opening the input stream to read data from client connection
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
}
客户端:
客户端=新套接字(“127.0.0.1”,9867);
System.out.println("Client connected ");
//getting the o/p stream of that connection
out=new PrintStream(client.getOutputStream());
//sending the message to server
out.print("Hello from client\n");
//reading the response using input stream
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println(in.readLine());
bufferRead = new BufferedReader(new InputStreamReader(System.in));
str = bufferRead.readLine();
out=new PrintStream(client.getOutputStream());
out.print(str);
out.flush();
while(! str.trim().equals("*")) {
// opening the input stream to read data from server connection
str = in.readLine();
System.out.println("4");
System.out.println(str);
out=new PrintStream(client.getOutputStream());
str = bufferRead.readLine();
out.print(str);
out.flush();
}