简体   繁体   English

Java如何从字节数组形成TCP数据包?

[英]How does Java form TCP packets from byte arrays?

While SocketIn/OutputStreams in Java seem stream based to an API user, TCP packets are packet based. Java中的SocketIn / OutputStreams似乎是基于API用户的流,而TCP数据包是基于数据包的。 One can write only one byte or an array of bytes. 一个人只能写一个字节或一个字节数组。 But one could also write more bytes than a TCP packet could carry. 但是,与TCP数据包相比,还可以写入更多字节。

So how does Java form TCP packets from the write methods? 那么Java如何通过write方法形成TCP数据包呢?

Creates calling write(singleByte) 4 times 4 TCP packets? 创建4 4个TCP数据包的调用write(singleByte)? Or does java join the bytes together? 还是java将字节连接在一起?

Does java join and split bytes to form the TCP packets? java是否会加入并拆分字节以形成TCP数据包?

No, Java, or more precisely the Java Virtual Machine, has no reason to get into these details. 不,Java,或更确切地说是Java虚拟机,没有理由深入了解这些细节。

What the JVM does, is that it opens a native Socket just like any native program would. JVM的作用是像打开任何本机程序一样打开本机Socket。 It then allows the Java code to interact with the native socket through the various Java API. 然后,它允许Java代码通过各种Java API与本机套接字交互。 This leaves the networking details to the operating system's network stack (TCP/IP implementation). 这会将网络详细信息留给操作系统的网络堆栈(TCP / IP实现)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM