为了验证C为established状态而S为SYN_RECV状态,写了下面的源程序(在别人的基础上改的)
1. 源程序
1.1 client
#include "head.h" int main(int argc, char **argv) { struct sockaddr_in servaddr; int status, sockfd,count; int fd[20]; int index = 0; int flag = 1; count = atoi(argv[1]); while (count-- > 0) { fd[index] = socket(AF_INET, SOCK_STREAM, 0); sockfd = fd[index]; index++; if(sockfd < 0){ perror("socket() error"); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); status = inet_pton(AF_INET, "10.10.37.15", &servaddr.sin_addr); if(status <= 0){ perror("inet_pton() error"); exit(1); } setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag)); if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ close(sockfd); printf("%s......\n", strerror(errno)); return -1; } else { printf("i=%d,fd:%d, ESTABLISHED.\n", index-1, sockfd); } } pause();
return 0;
}
1.2 server
#include "head.h" int main(int argc, char **argv) { int listenfd, sockfd,ret; struct sockaddr_in servaddr; char *buf = "serverritethis"; char recv_buf[20]; listenfd = socket(AF_INET, SOCK_STREAM, 0); if(listenfd < 0){ perror("socket() error......\n"); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ perror("bind() error......\n"); exit(1); } if(listen(listenfd, 4) < 0){ perror("listen() error......\n"); exit(1); } printf("listening...\n"); while(1){ sleep(1); } exit(0); }
head.h
#ifndef __HEAD_H #define __HEAD_H #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <memory.h> #include <unistd.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/uio.h> #include <netinet/tcp.h> #include <sys/wait.h> #include <arpa/inet.h> #include <netdb.h> #include <sys/stat.h> #include <dirent.h> #include <signal.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <event.h> #include <sys/xattr.h> #include <sys/syscall.h> #include <sys/statvfs.h> extern int errno; #define SERV_PORT 8888 #endif
2.测试
2.1 测试步骤
1)gcc -o server server.c
./server
2) gcc -o client client.c(修改对应的server ip)
./client 10
2.2 测试结果
1)client
[root@bogon connect]# netstat -tanp | grep 8888
tcp 0 0 10.10.37.17:44841 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44840 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44842 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44845 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44848 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44846 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44839 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44847 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44844 10.10.37.15:8888 ESTABLISHED 18213/./syn
tcp 0 0 10.10.37.17:44843 10.10.37.15:8888 ESTABLISHED 18213/./syn
[root@bogon connect]#2) server
[root@CentOS56-64 test]# netstat -tanp | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 18383/recv
tcp 0 0 10.10.37.15:8888 10.10.37.17:44844 SYN_RECV -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44846 SYN_RECV -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44848 SYN_RECV -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44845 SYN_RECV -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44847 SYN_RECV -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44840 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44841 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44842 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44843 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44839 ESTABLISHED -
[root@CentOS56-64 test]#
过了大概3分钟后:
[root@CentOS56-64 test]# netstat -tanp | grep 8888
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 18383/recv
tcp 0 0 10.10.37.15:8888 10.10.37.17:44840 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44841 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44842 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44843 ESTABLISHED -
tcp 0 0 10.10.37.15:8888 10.10.37.17:44839 ESTABLISHED -
[root@CentOS56-64 test]#