Search code examples
javacsocketsnio

Sockets - Difference between C and Java sending


I am converting a Java application (using NIO) to C sockets and am encountering a problem. At the same time, I am using grinder, as a TCPProxy, to see what's been transferred.

I have 4 lines for which i need to send to the client as part of the handshake.

Hello~Server\r\n
Hello1~Server1\r\n
Hello2~Server2\r\n
Hello3~Server3\r\n

For the Java application (using NIO), each string was sent out after every Java NIO flip(). That is to say the above 4 lines are sent out one at a time.

I have the following pseudo- code for Java.

  1. Clear Buffer,
  2. Put string into Buffer,
  3. Put size of string,
  4. Flip.

For C sockets, all 4 char arrays were sent out together even though I have 4 separate send()s.

Stumped. Any ideas?


Solution

  • TCP is a stream oriented protocol. That is to say, it should not matter how the stream is split up. So a design which requires splitting up the stream at specific boundaries is probably using TCP wrongly.

    Having said that, you can attempt to turn of "lumping" via setsockopt with the TCP_NODELAY option.