最近在看《linux高性能服务器编程》,在此做个日记,以激励自己,同时分享于有需要的朋友。
1. 读取和设置socket文件描述符属性:
#include
int getsockopt(int sockfd, int level, int option_name, void *option_value, socklen_t *restrict option_len);
int setsockopt(int sockfd, int level, int option_name, const void *option_value, socklen_t option_len);
sockfd参数指定被操作的目标socket。
level参数指定要操作哪个协议的选项,即属性,比如:IPv4, IPv6, TCP 和通用socket选项。
option_name参数指定选项的名字。
option_value参数指定选项的值。
option_len参参数指定选项的长度。
调用成功时返回0, 失败时返回-1, 并设置errno。
2. 对服务器而言,有部分socket选项只能在调用listen前设置才会有效。因为连接socket只能由accept调用返回,而accept从listen监听队列中接受的连接至少已经完成了TCP三次握手的前两个步聚,listen监听队列的连接至少已进入SYN_RCVD状态,这时服务器已经往被连接上发送TCP同步报文。
3. SO_REUSeADDR选项:重用本地地址
未设置此项前,若服务端开启后,又关闭,此时sock处于TIME_WAIT状态,与之绑定的socket地址不可重用,而导致再次开启服务端失败。
经过setsockopt设置之后, 即使处于TIME_WAIT些状态也可以立即被重用。
int reuse = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizof(reuse));
4. SO_RCVBUF 和 SO_SNDBUF :TCP接收缓冲区和发送缓冲区的大小
当然,即使我们设置了这两项的大小时, 系统都会自动将其加倍, 并且不得小于某个最小值。
TCP接收缓冲区的最小值是 256 字节, 而发送缓冲区的最小值是 2048 字节。(不同系统可能会有差异)
这么做的目的是确保一个TCP连接拥有足够多的空闲缓冲区来处理拥塞。
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof(sendbuf));
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, (socklen_t*)&len);
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, sizeof(recvbuf));
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, (socklen_t*)&len);
5. 代码
//服务端
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
int main(int argc, char **argv)
{
if (argc <= 3) {
printf("Usage: %s ip port revc_size\n", basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
int sock = socket(PF_INET, SOCK_STREAM, 0);
assert(sock >= 0);
//设置地址可重用
int reuse = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizof(reuse));
int recvbuf = atoi(argv[3]);
int len = sizeof(recvbuf);
//设置接受缓冲区大小
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, sizeof(recvbuf));
//获取系统修改后的大小
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvbuf, (socklen_t*)&len);
printf("the receive buffer size after setting is %d\n", recvbuf);
int ret = bind(sock, (struct sockaddr*)&address, sizeof(address));
assert(ret != -1);
ret = listen(sock, 5);
assert(ret != -1);
struct sockaddr_in client;
socklen_t client_addrlength = sizeof(client);
int connfd = accept(sock, (struct sockaddr*)&client, &client_addrlength);
if (connfd < 0) {
printf("errno is: %d\n", errno);
}
else {
char buffer[BUFFER_SIZE];
memset(buffer, '\0', BUFFER_SIZE);
while (recv(connfd, buffer, BUFFER_SIZE-1, 0) > 0);
printf("recv: %s\n", buffer);
close(connfd);
}
close(sock);
return 0;
}
运行后:
jason@lightning:~/myproject/test_recv$ ./test_recv localhost 8000 50
the receive buffer size after setting is 2280
很明显被修改过了, 我们给的50, 被改为2280。
//客户端
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
int main(int argc, char **argv)
{
if(argc <= 3) {
fprintf(stderr, "Usage: %s ip port send_buffer_size\n",
basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
struct sockaddr_in server_address;
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
inet_pton(AF_INET, ip, &server_address.sin_addr);
int sock = socket(PF_INET, SOCK_STREAM, 0);
assert(sock >= 0);
int sendbuf = atoi(argv[3]);
int len = sizeof(sendbuf);
//设置发送缓冲区大小
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, sizeof(sendbuf));
//获取系统修改后的大小
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sendbuf, (socklen_t*)&len);
printf("the tcp send buffer size after setting is %d\n",
sendbuf);
if (connect(sock, (struct sockaddr*)&server_address, sizeof(server_address)) != -1) {
char buffer[BUFFER_SIZE];
memset(buffer, 'a', BUFFER_SIZE);
send(sock, buffer, BUFFER_SIZE, 0);
}
else {
printf("connect %s failed\n", ip);
}
close(sock);
return 0;
}
jason@lightning:~/myproject/test_send$ ./test_send localhost 8000 2000
the tcp send buffer size after setting is 4000
给的是2000, 被改成4000了。