Java sockets - Client doesn't read the strings from server's socket

前端 未结 2 1566
北恋
北恋 2020-12-11 14:15

I have two Java projects that communicate with sockets through strings. One is a client and the other is a server. The server accepts the connection trough the \"ServerSocke

相关标签:
2条回答
  • 2020-12-11 14:40

    In your send methods, try changing out.write(m) to out.println(m).

    Each time you are using the readLine method it will keep reading until it hits a new line character or line separator. The println method will automatically put a line.separator at the end of the string you are trying to send.

    https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#println()

    0 讨论(0)
  • 2020-12-11 14:43

    Your server sends "USERNAME" and then waits. The client reads the next line, and blocks until it receives it. So you have a deadlock, since the server never sends an EOL character. If the client reads a line, the server should send a line.

    0 讨论(0)
提交回复
热议问题