简体   繁体   English

TCP java聊天,客户端不听

[英]TCP java chat, client not listen

I have problem with my ClientListener, server send TCP package but Client do not display it.我的 ClientListener 有问题,服务器发送 TCP 包但客户端不显示它。

Here is my code这是我的代码

https://github.com/Behoston/sieci/tree/master/src/tcp https://github.com/Behoston/sieci/tree/master/src/tcp

(Server run new thread for each new client and this thread (to listen) and this thread run other to sending messages (it works - I can see messages in wireshark Client run main thread where can input some text and second therad (not working properly) to listen messages from serever) (服务器为每个新客户端运行新线程,这个线程(监听)和这个线程运行其他发送消息(它工作 - 我可以在wireshark 客户端运行主线程中看到消息,在那里可以输入一些文本和第二个 therad(无法正常工作) ) 收听来自服务器的消息)

It's quite hard to answer your question if you only provide a link to your repository, some code snippets of the crucial classes or methods would be helpful.如果您只提供存储库的链接,则很难回答您的问题,关键类或方法的一些代码片段会有所帮助。

If my understanding of the classes you provided is correct, this is the workflow you are trying to implement (correct me if I'm wrong):如果我对您提供的类的理解是正确的,这就是您要实现的工作流程(如果我错了,请纠正我):

  1. Server gets started服务器启动
  2. Client connects客户端连接
  3. Server sends a welcome message (which is not getting displayed on clientside):服务器发送欢迎消息(未在客户端显示):

     DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); outToClient.writeBytes("Hello, your ID is: " + clientId + "; To send message please use 3 first chars to set target");

On the recieving side:在接收端:

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            response = inFromServer.readLine();

As far as I see, you are waiting for a readLine() with your server sending a byteArray.据我所知,您正在等待服务器发送 byteArray 的readLine() Since readLine() waits for a line seperator (fe "\\n"), your messages can't be processed.由于readLine()等待行分隔符 (fe "\\n"),因此无法处理您的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM