Multithreading 错误TCP客户端服务器聊天程序使用线程java
我正在制作一个tcp客户端-服务器聊天程序。我的服务器是线程制作的,代码如下:Multithreading 错误TCP客户端服务器聊天程序使用线程java,multithreading,tcp,thread-safety,tcpclient,tcpserver,Multithreading,Tcp,Thread Safety,Tcpclient,Tcpserver,我正在制作一个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
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());
}
我的客户端文件是:(简单,没有线程)
现在一件奇怪的事情发生在我身上。当我注释掉两个文件上的while循环时,程序运行良好。但当我取消注释它时,我的客户端在向服务器发送消息后进入while循环,而我的服务器一直在等待响应
所以输出是:(客户机)
服务器的名称为:
Server binded at 192.168.1.242:9867
Run the Client
Hello from client
1
我认为这是由于使用了thread/asynchoronousprocess.Plz帮助
编辑的代码
服务器端:
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(in.readLine());
将从传入连接中读取一条线路并回显它。当它这样做时,行内容就消失了,如果您再次尝试调用“in.readLine()”,它将尝试读取另一行,而不是同一行
此外,这一行:
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
通常只出现一次。构建阅读器时,只需使用它。您不必反复重建它。对于您的示例,我强烈建议通过以下链接使用敲打协议
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
对我来说,代码中显而易见的主要错误是
while(! str.trim().equals("*"))
应该是什么时候
while ((str = in.readLine()) != null) {
System.out.println("Server: " + str);
这意味着您的线程将通过输入流持续侦听输入,然后在服务器返回响应后,您可以使用str变量执行任何操作为什么要在服务器中的每个循环上重新打开输入和输出流?我检查了它…但什么也没有发生…结果是sameI suggest你在问题的底部添加了一个标题为“已编辑但仍不能工作”的部分,这样你的原始代码就可以保留在那里,我们可以看到新代码。根据我给你的线索,你编辑的版本需要再编辑一些。这两个提示仍然适用。
in= new BufferedReader(new InputStreamReader(client.getInputStream()));
while(! str.trim().equals("*"))
while ((str = in.readLine()) != null) {
System.out.println("Server: " + str);