更改套接字I/O缓冲大小

时间:2021-02-09 12:11:39   收藏:0   阅读:0

//更改I/O缓冲大小

//set_buffer.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
void error_handling(char*message);

int main(int argc, char *argv[])
{
int sock;
int snd_buf=1024*3,rcv_buf=1024*3;
int state;
socklen_t len;

sock = socket(PF_INET,SOCK_STREAM,0);
state = setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(void*)&rcv_buf,sizeof(rcv_buf));
if (state)
{
error_handling("setsockopt() error!");
}

state=setsockopt(sock,SOL_SOCKET,SO_SNDBUF,(void*)&snd_buf,sizeof(snd_buf));
if(state)
{
error_handling("setsockopt() error!");
}

len = sizeof(snd_buf);
state=getsockopt(sock,SOL_SOCKET,SO_SNDBUF,(void*)&snd_buf,&len);
if(state)
{
error_handling("getsockopt() error!!");
}

len = sizeof(rcv_buf);
state=getsockopt(sock,SOL_SOCKET,SO_RCVBUF,(void*)&rcv_buf,&len);
if(state)
{
error_handling("getsockopt() error!");
}

printf("Input buffer size:%d\n",rcv_buf);
printf("Output buffer size:%d\n",snd_buf);
return 0;
}

void error_handling(char*message)
{
fputs(message,stderr);
fputc(‘\n‘,stderr);
exit(1);
}

技术图片

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!