简体   繁体   English

Java和C之间的套接字通信:良好的缓冲区大小

[英]Socket communication between Java and C: Good buffer size

I have to implement a socket communicatio between a Server written in Java and a Client written in C. 我必须在用Java编写的服务器和用C编写的客户端之间实现套接字通信。

The maximum amount of data that I will have to transmit is 64KB. 我将要传输的最大数据量是64KB。

In the most socket communication tutorials they are working with buffer sizes of about 1024 Byte or less. 在大多数套接字通信教程中,它们使用的缓冲区大小约为1024字节或更小。 Is it a (maybe performance) problem to set the buffer to 64KB? 将缓冲区设置为64KB是否会导致性能问题?

The two software parts will run on the same machine or at least in the same local area network. 这两个软件部分将在同一台计算机上或至少在同一局域网中运行。

And if it is a problem: How to handle messages that are bigger than buffer in general? 如果存在问题:通常如何处理大于缓冲区的消息?

The buffer can be smaller than the messages without any problem while the receiver consumes the data as fast as the sender generates it. 缓冲区可以小于消息而没有任何问题,而接收方使用数据的速度与发送方生成数据的速度一样快。 A bigger buffer lets your receiver to have more time to process the message, but usually you don't need a giant buffer: for example, when you download software the size of a file can be more than 1GB, but your browser/ftp client just reads the buffer and stores the data in a file in your local hard disk. 较大的缓冲区使接收者有更多时间来处理消息,但是通常您不需要巨大的缓冲区:例如,当您下载软件时,文件大小可以大于1GB,但是浏览器/ ftp客户端只需读取缓冲区并将数据存储在本地硬盘中的文件中即可。

And in general, you can ignore the language used to create the client or the server, only the network protocol matters. 通常,您可以忽略用于创建客户端或服务器的语言,只有网络协议才重要。 Every language has its own libraries to handle sockets with ease. 每种语言都有自己的库来轻松处理套接字。

I suggest a larger buffer but I suspect you see less than 5% difference whether you use 1 KB or 64 KB. 我建议使用更大的缓冲区,但是我怀疑无论使用1 KB还是64 KB的差异都小于5%。

Note: b = bit and B = byte , k = 1000 and K = 1024 and it is best not to get the confused (not that it is likely to matter here) 注意: b = bitB = bytek = 1000和K = 1024 ,最好不要混淆(不是这里可能很重要)

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

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