Receive file via socket, TCP connection freezes

后端 未结 1 977
臣服心动
臣服心动 2021-01-15 20:35

I have stucked for 4h already with the sockets, the way I am using is is that there is only one application as client and server, once the client connect it is opening the t

相关标签:
1条回答
  • 2021-01-15 21:20

    You can't mixed buffered and unbuffered streams/readers/writers on the same socket. You will lose data in the buffers. Use the same stream pair for the life of the socket. In this case I would use DataInputStream and DataOutputStream, and the readUTF()/writeUTF() methods for the messages and filenames. You will also need to send the file length ahead of the file, unless the file is the last thing sent over the connection: otherwise the peer won't know when to stop reading the file and go back and start reading messages again.

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