Hey all,
Basically this hacked up version of a tcp/ip tutorial is supposed to make as many connections as possible, then report how many it was able to make. This will tell you how many active TCP connections can exist.
But it doesn't work.
Basically when I run it agaisnt linux or solaris boxes, I get 188 as the result, and it just sits their afterwards. Now, I know that the number should be a lot higher than 188, and also that the numbers are different for solaris and linux.
Here's the code:
Code:
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXCONN 1024 /* Max number of connections */
int main(int argc, char *argv[])
{
int sock[MAXCONN]; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
int i=0,s=0,numconn;
if (argc < 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <IP> <Port>\n",
argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
echoServPort = atoi(argv[2]); /* Use given port, if any */
/* Create a reliable, stream socket using TCP */
printf("Creating %d sockets for the test...",MAXCONN);
i=0;
while (i<MAXCONN) {
if ((sock[i] = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
s=1;
i++;
}
printf("Done.\n");
printf("Currently opening connection number:\n");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
s=0;
i=0;
while (s!=1) {
if (connect(sock[i], (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
s=1;
printf("\b\b\b%d",i);
i++;
if (i>MAXCONN) {
printf("\nReached max number of connections. Recompile? ;-)\n");
s=1;
}
sleep(0.2);
}
close(sock[0]); //havent gotten around to closing them all, but i will
exit(0);
}
Ideas?