java socket InputStream hangs/blocks on both client and server

前端 未结 2 756
春和景丽
春和景丽 2021-01-06 03:10

Hi, all

I am recently working on a tiny program aimed to close the browser remotely. The basic procedures are as follow:
Server side:

<
相关标签:
2条回答
  • 2021-01-06 03:46

    You are reading until EOS at both ends, which is a poor choice, as you can't 'send' the EOS without losing the ability to send further data on the same connection. You need to redesign your application protocol so you can read a command at a time without requiring that the connection be closed to complete it. For example, send lines, or a length word prefix, or a self-describing protocol such as XML or Java Object Serialization, or whatever you can read via DataInputStream without relying on EOFException.

    0 讨论(0)
  • 2021-01-06 03:55

    Your socket reading code is reading until EOF. you will not receive EOF until you close the socket. hence, your code never proceeds.

    if you only want to send a single command on the socket connection, then close the OutputStream after writing the command (using Socket.shutdownOutput()).

    If you want to send multiple commands on a single socket connection, then you will need to come up with a way of delimiting each command (simple example here).

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