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 = * ;
if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -) {
// 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.
socket programming Max size of tcp/ip socket Buffer?的更多相关文章
- Socket: Java Socket 几个重要的TCP/IP选项解析(转)
Socket选择可以指定Socket类发送和接受数据的方式.在JDK1.4中共有8个Socket选择可以设置.这8个选项都定义在java.net.SocketOptions接口中.定义如下: publ ...
- mysql错误:Can’t create TCP/IP socket (10106) 解决方法
错误描述 “mysql错误:Can’t create TCP/IP socket (10106)”,目测是socket端口被占用的原因,然后在打开tomcat,报的错误中也包含了“socket”,再一 ...
- TCP IP Socket In C, 2e-chapter 1 Introduction
本章是基础概念,建议补计算机网络基础,这里不全. 目录 1 网络,数据包,协议 2 关于地址(address) 2.1 IP地址格式 2.2 IPv4和IPv6共存 2.3 端口号 2.4 特殊地址 ...
- TCP IP SOCKET 笔记
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- python 网络编程 TCP/IP socket UDP
TCP/IP简介 虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Micro ...
- JAVA Socket 底层是怎样基于TCP/IP 实现的???
首先必须明确:TCP/IP模型中有四层结构: 应用层(Application Layer).传输层(Transport Layer).网络层(Internet Layer ).链路层( ...
- 一个项目看java TCP/IP Socket编程
前一段时间刚做了个java程序和网络上多台机器的c程序通讯的项目,遵循的是TCP/IP协议,用到了java的Socket编程.网络通讯是java的强项,用TCP/IP协议可以方便的和网络上的其他程序互 ...
- Linux IPC tcp/ip socket 编程
模型 #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include < ...
- HTTP,TCP/IP,Socket
HTTP:超文本传输协议,首先它是一个协议,并且是基于TCP/IP协议基础之上的应用层协议. TCP/IP协议是传输层协议,主要解决数据如何在网络中传输,HTTP是应用层协议,主要解决如何包装数据. ...
随机推荐
- 中文翻译:pjsip教程(一)之PJNATH简介
在学习pjsip的过程中,发现只是单单的阅读英文官方文档,对于里边概念的理解还是不够透彻,并且苦于pjsip没有发现全一点的中文版本,所以想尽自己所能为建设和谐社会而贡献一份力量,文中定会有所疏漏,希 ...
- Sizzle引擎执行的流程图
Sizzle有太多都不是太懂,但能看懂这张图. 图片来源: http://www.cnblogs.com/aaronjs/p/3332805.html
- Windows phone 之手势识别(Drag)
首先,在XAML页面中,添加如下代码: 前提,别忘了添加 1.引入dll (silverlight for wndows phone toolkit) 2.引入命名空间 xmlns:toolkit=& ...
- 【原创】CMD常用命令:解决实际问题
1.查找某一目录下后缀名文某某的所有文件. 命令格式:dir *.mp3 /a -d/b/s >C:\Users\leemo\Desktop\total.txt MP3为文件后缀名.>是命 ...
- jQuery实现商品楼层的感觉
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- curl批处理从官方demo封装
官方demo // 创建一对cURL资源 $ch1 = curl_init(); $ch2 = curl_init(); // 设置URL和相应的选项 curl_setopt($ch1, CURLOP ...
- phpMyAdmin下载与安装
Part1 phpMyAdmin下载 浏览器输入网址 http://www.phpmyadmin.net 下载即可 我的下载是这样的 Part2 phpMyAdmin安装 解压下载的压缩包到apach ...
- yii1.1.15 accessRules的一个小bug
public function accessRules() { return array( array( 'allow', 'actions'=>array('ajaxChangeImage', ...
- windows下配置wnmp
最近尝试windows下配置nginx+php+mysql,在这里总结一下. 1.下载windows版本的nginx,官网下载地址:http://nginx.org/en/download.htm, ...
- Meditation Guide
Meditation “Stop!!!” don’t we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the da ...