Java TCP socket: data transfer is slow

后端 未结 12 1963
误落风尘
误落风尘 2020-12-01 09:50

I set up a server with a ServerSocket, connect to it with a client machine. They\'re directly networked through a switch and the ping time is <1ms.

Now, I try to

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 10:28

    Since I cannot yet comment on this site, I must write answer to @Erik here.

    The problem is that DataOutputStream doesn't buffer. The whole Stream-thing in Java is based on decorators design pattern. So you could write

    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

    It will wrap the original stream in a BufferedOutputStream which is more efficient, which is then wrapped into a DataOutputStream which offers additional nice features like writeInt(), writeLong() and so on.

提交回复
热议问题