socket programming Max size of tcp/ip socket Buffer?

TCP data is buffered at both sender and receiver. The size of the receiver‘s socket receive buffer determines how much data can be in flight without acknowledgement, and the size of the sender‘s send buffer determines how much data can be sent before the sender blocks or gets EAGAIN/EWOULDBLOCK, depending on blocking/non-blocking mode. You can set these socket buffers as large as you like up to 2^32-1 bytes, but if you set the client receive buffer higher than 2^16-1 you must do so before connecting the socket, so that TCP window scaling can be negotiated in the connect handshake, so that the upper 16 bits can come into play. [The server receive buffer isn‘t relevant here, but if you set it >= 64k you need to set it on the listening socket, from where it will be inherited by accepted sockets, again so the handshake can negotiate window scaling.]

However I agree entirely with Martin James that this is a silly requirement. It wastes a thread, a thread stack, a socket, a large socket send buffer, an FD, and all the other associated resources at the server for two hours, and possibly affects other threads and therefore other clients. It also falsely gives the server the impression that two hours‘ worth of data has been received, when it has really only been transmitted to the receive buffer, which may lead to unknown complications in recovery situations: for example, the server may be unable to reconstruct the data sent so far ahead. You would better off not connecting until you are ready to start receiving the data, or else reading and spooling the data to yourself at the client for processing later.

If you want see your buffer size in terminal, you can take a look at:

  • /proc/sys/net/ipv4/tcp_rmem (for read)
  • /proc/sys/net/ipv4/tcp_wmem (for write)

They contain three numbers, which are minimum, default and maximum memory size values (in byte), respectively.

For getting the buffer size in c/c++ program the following is the flow

int n;
unsigned int m = sizeof(n);
int fdsocket;
fdsocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); // example
getsockopt(fdsocket,SOL_SOCKET,SO_RCVBUF,(void *)&n, &m);
// now the variable n will have the socket size

You can increase the value from the default, but you can‘t increase it beyond the maximum value. Use setsockopt to change the SO_RCVBUF option:

int n = 1024 * 1024;
if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1) {
  // deal with failure, or ignore if you can live with the default size
}

Note that this is the portable solution; it should work on any POSIX platform for increasing the receive buffer size. Linux has had autotuning for a while now (since 2.6.7, and with reasonable maximum buffer sizes since 2.6.17), which automatically adjusts the receive buffer size based on load. On kernels with autotuning, it is recommended that you not set the receive buffer size using setsockopt, as that will disable the kernel‘s autotuning. Using setsockopt to adjust the buffer size may still be necessary on other platforms, however.

时间: 2024-10-20 11:19:29

socket programming Max size of tcp/ip socket Buffer?的相关文章

Linux程序设计学习笔记----Socket网络编程基础之TCP/IP协议簇

转载请注明出处: ,谢谢! 内容提要 本节主要学习网络通信基础,主要涉及的内容是: TCP/IP协议簇基础:两个模型 IPv4协议基础:IP地址分类与表示,子网掩码等 IP地址转换:点分十进制\二进制 TCP/IP协议簇基础 OSI模型 我们知道计算机网络之中,有各种各样的设备,那么如何实现这些设备的通信呢? 显然是通过标准的通讯协议,但是,整个网络连接的过程相当复杂,包括硬件.软件数据封包与应用程序的互相链接等等,如果想要写一支将联网全部功能都串连在一块的程序,那么当某个小环节出现问题时,整只

mysql错误:Can’t create TCP/IP socket (10106)

昨天晚上十一点半,有个女同学打电话说电脑出问题了,说tomcat和mysql打不开了,各种急!因为后天就要答辩了,这些软件打不开,系统也就运行不起来!大半夜的让我怎么办,只好说明天早起帮看看! 早早的起来了,接过同学的电脑!回到宿舍看了一下,mysql果然打不开报了这样的错误"mysql错误:Can't create TCP/IP socket (10106)",目测是socket端口被占用的原因,然后在打开tomcat,报的错误中也包含了"socket",再一次加

TCP IP SOCKET 笔记

网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可比性, socket则是对TCP/IP协议的封装和应用(程序员层面上). 也可以说,TPC/IP协议是传输层协议,主要解决数据如何在网络中传输, 而HTTP是应用层协议,主要解决如何包装数据. 关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍: “我们在传输数据时,可以只使用(传输

一个项目看java TCP/IP Socket编程

前一段时间刚做了个java程序和网络上多台机器的c程序通讯的项目,遵循的是TCP/IP协议,用到了java的Socket编程.网络通讯是java的强项,用TCP/IP协议可以方便的和网络上的其他程序互通消息. 先来介绍下网络协议:     TCP/IP         Transmission Control Protocol 传输控制协议         Internet Protocol 互联网协议     UDP         User Datagram Protocol 用户数据协议

JAVA Socket 底层是怎样基于TCP/IP 实现的???

首先必须明确:TCP/IP模型中有四层结构:       应用层(Application Layer).传输层(Transport  Layer).网络层(Internet Layer  ).链路层(LinkLayer)  其中Ip协议(Internet Protocol)是位于网络层的,TCP协议时位于传输层的.通过Ip协议可以使可以使两台计算机使用同一种语言,从而允许Internet上连接不同类型的计算机和不同操作系统的网络.Ip协议只保证计算机能够接收和发送分组数据. 当计算机要和远程的计

Android TCP/IP Socket Test

TCP/IP协议:Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议,又名网络通讯协议,是Internet最基本的协议.Internet国际互联网络的基础,由网络层的IP协议和传输层的TCP协议组成.TCP/IP 定义了电子设备如何连入因特网,以及数据如何在它们之间传输的标准.协议采用了4层的层级结构,每一层都呼叫它的下一层所提供的网络来完成自己的需求.通俗而言:TCP负责发现传输的问题,一有问题就发出信号,

http tcp/ip socket

这两天重新看了下TCP/IP的基本概念,也重新看了下http协议,分别是<TCP/IP详解卷1:协议>和<Http:The Definitive Guide>这两本书. 看完后有两点感触: (1)基础的理论知识对工作还是有很大帮助.平时工作中,也许我们更多的关注问题怎么解决,而解决问题有多种方式,其中,来的快的就是百度或Android官网的开发文档,这两种方式基本能解决工作中的所有技术问题,且直接告诉你解决问题的方式(步骤或code直接上),这是互联网化知识共享的体现.而很多时候,

TCP/IP Socket 的理解

网络由下往上分为:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. TCP 协议对应于传输层,HTTP 协议对应应用层,Socket 则是对 TCP/IP 协议的封装. 也可以说 TCP/IP 协议是传输层协议,主要解决数据如何在网络中传输,HTTP 是应用层协议,解决如何包装传输的数据. 实际上 Socket 是对 TCP/IP 协议的封装,Socket 本身并不是协议,而是一个调用接口(API),通过 Scoket 我们才能使用 TCP/IP. 所以说 Socket 的出现只是使

ACE基本的TCP/IP Socket用法之TCP通信(接收什么就打印什么)1.0

客户端每隔8秒钟向服务端建立连接,发送内容之后关闭连接: 服务端不停的接受客户端的连接请求,收到内容之后就打印出来: 客户端从本地文件中读取内容发送给客户端: 客户端代码:client_main.cpp #include <iostream> #include <string> using namespace std; #include <fstream> #include <iterator> #include "ace/INET_Addr.h&