diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e99f191 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +server +client + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..14f5bac --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +all: + gcc -O2 -o server server.c + gcc -O2 -o client client.c + diff --git a/README.md b/README.md index 9912ff8..f8f36dc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,34 @@ c1000k ====== -A tool to test if you system supports 1 million connections +This is the TCP server-client suit to help you to test if your OS supports c1000k(1 million connections). + +## Usage + +### Download and compile + + wget --no-check-certificate https://github.com/ideawu/c1000k/archive/master.zip + unzip master.zip + cd c1000k-master + make + +### start the server, listen on port 7000~7099 + + ./server 7000 + +### run the client + + ./client 127.0.0.1 7000 + +The server will ouput message like this and quit: + + connections: 153 + error: Too many open files + +It says the server can only accept 153 connections, it reaches the max-open-files limitation so it quit. + +The client will output message like this: + + connections: 154 + error: Connection refused + diff --git a/client.c b/client.c new file mode 100644 index 0000000..907786b --- /dev/null +++ b/client.c @@ -0,0 +1,73 @@ +/***************************************************** + * The TCP socket client to help you to test if your + * OS supports c1000k(1 million connections). + * @author: ideawu + * @link: http://www.ideawu.com/ + *****************************************************/ +#include +#include +#include +#include +#include +#include +#include + +#define MAX_PORTS 100 + +int main(int argc, char **argv){ + if(argc <= 2){ + printf("Usage: %s ip port\n", argv[0]); + exit(0); + } + + struct sockaddr_in addr; + const char *ip = argv[1]; + int base_port = atoi(argv[2]); + int opt = 1; + int bufsize; + socklen_t optlen; + int connections = 0; + + bzero(&addr, sizeof(addr)); + addr.sin_family = AF_INET; + inet_pton(AF_INET, ip, &addr.sin_addr); + + char tmp_data[10]; + int index = 0; + while(1){ + if(++index >= MAX_PORTS){ + index = 0; + } + int port = base_port + index; + //printf("connect to %s:%d\n", ip, port); + + addr.sin_port = htons((short)port); + + int sock; + if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + goto sock_err; + } + if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1){ + goto sock_err; + } + + connections ++; + + if(connections % 1000 == 999){ + //printf("press Enter to continue: "); + //getchar(); + printf("connections: %d, fd: %d\n", connections, sock); + } + usleep(1 * 1000); + + bufsize = 5000; + setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)); + setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)); + } + + return 0; +sock_err: + printf("connections: %d\n", connections); + printf("error: %s\n", strerror(errno)); + return 0; +} diff --git a/server.c b/server.c new file mode 100644 index 0000000..81671b5 --- /dev/null +++ b/server.c @@ -0,0 +1,114 @@ +/***************************************************** + * The TCP socket server to help you to test if your + * OS supports c1000k(1 million connections). + * @author: ideawu + * @link: http://www.ideawu.com/ + *****************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_PORTS 100 + +int main(int argc, char **argv){ + if(argc < 2){ + printf("Usage: %s port\n", argv[0]); + exit(0); + } + + struct sockaddr_in addr; + const char *ip = "0.0.0.0"; + int opt = 1; + int bufsize; + socklen_t optlen; + int connections = 0; + int base_port = 7000; + if(argc > 2){ + base_port = atoi(argv[1]); + } + + int server_socks[MAX_PORTS]; + + for(int i=0; i maxfd){ + maxfd = server_socks[i]; + } + } + int ret = select(maxfd + 1, &readset, NULL, NULL, NULL); + if(ret < 0){ + if(errno == EINTR){ + continue; + }else{ + printf("select error! %s\n", strerror(errno)); + exit(0); + } + } + if(ret == 0){ + continue; + } + + for(int i=0; i