1

While SocketIn/OutputStreams in Java seem stream based to an API user, TCP packets are packet based. One can write only one byte or an array of bytes. But one could also write more bytes than a TCP packet could carry.

So how does Java form TCP packets from the write methods?

Creates calling write(singleByte) 4 times 4 TCP packets? Or does java join the bytes together?

Does java join and split bytes to form the TCP packets?

3
  • 1
    The OS is a lot better at handling those things than Java. No need to duplicate functionality by rewriting all that code into the JDK. Commented Aug 4, 2014 at 18:16
  • @Kayaman So this is handled by the os? Then how do most os's handle it?And why the downvote? Commented Aug 4, 2014 at 18:34
  • 1
    I didn't downvote you. Most OSes handle it effectively so we don't have to care about it. Commented Aug 4, 2014 at 18:58

1 Answer 1

1

No, Java, or more precisely the Java Virtual Machine, has no reason to get into these details.

What the JVM does, is that it opens a native Socket just like any native program would. It then allows the Java code to interact with the native socket through the various Java API. This leaves the networking details to the operating system's network stack (TCP/IP implementation).

Sign up to request clarification or add additional context in comments.

2 Comments

Is it even possible for native sockets to know whether the code would write a single byte more or not?
I was going to to write that it's impossible to know whether the underlying socket buffer was full, before calling write(), but then I googled and found this: stackoverflow.com/a/1805234/3199595

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.