how to write bytes to server socket

前端 未结 3 1464
故里飘歌
故里飘歌 2021-01-27 21:44

I\'m writing a java socket program to read data from server, I\'ve no control to server, below is protocol agreed,

  • 2-bytes: magic number
  • 2-bytes: data l
3条回答
  •  春和景丽
    2021-01-27 22:31

    Use a DataOutputStream around a BufferedOutputStream around the `Socket.getOutputStream(). Then you can use:

    • writeShort() for the magic number
    • writeShort() for the length word
    • write() for the payload.

    Similarly you can use DataInputStream and the corresponding readXXX() methods to read the response.

    NB You are writing to a socket here, not a server socket.

提交回复
热议问题