Java TCP socket: data transfer is slow

后端 未结 12 1949
误落风尘
误落风尘 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:24

    Hey, I figured I'd follow up for anyone that was interested.

    Here's the bizarre moral of the story:

    NEVER USE DataInputStream/DataOutputStream and sockets!!

    If I wrap the socket in a BufferedOutputStream/BufferedInputStream, life is great. Writing to it raw is just fine.

    But wrap the socket in a DataInputStream/DataOutputStream, or even have DataOutputStream(BufferedOutputStream(sock.getOutputStream())) is EXTREMELY SLOW.

    An explanation for that would be really interesting to me. But after swapping everything in and out, this is what's up. Try it yourself if you don't believe me.

    Thanks for all the quick help, though.

提交回复
热议问题