Add sockoption test

This commit is contained in:
Jianfeng Jiang
2023-10-16 11:08:00 +08:00
committed by Tate, Hongliang Tian
parent 11ff35d34e
commit 2eaf2e1290
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
int main() {
int sockfd;
int option;
// Create tcp socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Get send buffer size
int sendbuf;
socklen_t sendbuf_len = sizeof(sendbuf);
if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuf, &sendbuf_len) < 0 || sendbuf_len != sizeof(sendbuf)) {
perror("Getting SO_SNDBUF option failed");
exit(EXIT_FAILURE);
}
int error;
socklen_t error_len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &error_len ) < 0 || error_len != sizeof(error) || error != 0) {
perror("Getting SO_SNDBUF option failed");
exit(EXIT_FAILURE);
}
// Disable Nagle algorithm
option = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option)) < 0) {
perror("Setting TCP_NODELAY option failed");
exit(EXIT_FAILURE);
}
// Enable reuse addr
option = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0) {
perror("Setting SO_REUSEADDR option failed");
exit(EXIT_FAILURE);
}
// Print new value
int nagle;
socklen_t nagle_len = sizeof(nagle);
if (getsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &nagle, &nagle_len) < 0 || nagle != 1) {
perror("Getting TCP_NODELAY option failed.");
exit(EXIT_FAILURE);
}
int reuseaddr;
socklen_t reuseaddr_len = sizeof(reuseaddr);
if (getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, &reuseaddr_len) < 0 || reuseaddr != 1) {
perror("Getting SO_REUSEADDR option failed.");
exit(EXIT_FAILURE);
}
// Close socket
close(sockfd);
return 0;
}

View File

@ -14,7 +14,9 @@ echo "Start network test......"
./unix_server & ./unix_server &
./unix_client ./unix_client
./socketpair ./socketpair
./sockoption
./listen_backlog ./listen_backlog
./send_buf_full ./send_buf_full
echo "All network test passed" echo "All network test passed"