Skip to content

Instantly share code, notes, and snippets.

@phemmer
Created May 18, 2014 09:29
Show Gist options
  • Save phemmer/ea6decb6435947c5f520 to your computer and use it in GitHub Desktop.
Save phemmer/ea6decb6435947c5f520 to your computer and use it in GitHub Desktop.
cerver/client code to test maximum number of connections
/*
Opens multiple connections to 127.0.0.1:2222 until an error occurs.
It displays a counter of the number of open connections so far.
As soon as an error occurs, it prints the error and exits.
The counter will be the number of open connections at the time of error.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
int main() {
setbuf(stdout, NULL);
int connection_count = 0;
int client_fd;
struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = inet_addr("127.0.0.1"),
.sin_port = htons(2222)
};
while(1) {
printf("\r%06d", connection_count);
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
printf("\n"); perror(""); exit(1);
}
if(connect(client_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
printf("\n"); perror(""); exit(1);
}
connection_count++;
}
}
/*
Listens on 127.0.0.1:2222 for incoming connections.
When a connection is received, it increments and displays a counter.
When the connection is closed, it decrements the counter.
It assumes that any activity on the client socket is a 'close', so don't send data, or it'll close the connection.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
int main() {
setbuf(stdout, NULL);
int listen_fd;
struct sockaddr_in listen_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = inet_addr("127.0.0.1"),
.sin_port = htons(2222)
};
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
int on = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
bind(listen_fd, (struct sockaddr *) &listen_addr, sizeof(listen_addr));
listen(listen_fd, 16);
struct epoll_event ev, events[16];
int event_count, epollfd;
epollfd = epoll_create(1);
ev.events = EPOLLIN;
ev.data.fd = listen_fd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_fd, &ev);
int i;
int connection_count = 0;
int client_fd;
while(1) {
printf("\r%06d", connection_count);
//printf("%06d\n", connection_count);
event_count = epoll_wait(epollfd, events, 16, -1);
for (i = 0; i < event_count; i++) {
//printf("FD=%d\n", events[i].data.fd);
if (events[i].data.fd == listen_fd) {
//printf("Accepting\n");
client_fd = accept(listen_fd, NULL, NULL);
if (client_fd == -1) {
perror("Couldn't accept connection");
exit(1);
} else {
ev.data.fd = client_fd;
epoll_ctl(epollfd, EPOLL_CTL_ADD, client_fd, &ev);
connection_count += 1;
}
} else {
// client connection. assume the socket is closing
ev.data.fd = events[i].data.fd;
epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);
close(events[i].data.fd);
connection_count -= 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment