简体   繁体   中英

TCP java chat, client not listen

I have problem with my ClientListener, server send TCP package but Client do not display it.

Here is my code

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)

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. Since readLine() waits for a line seperator (fe "\\n"), your messages can't be processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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