Count established connections

This is a discussion on Count established connections within the Networking/Device Communication forums, part of the General Programming Boards category; I am running an IRC server currently running on port 6667, using the current hybrid and a UDP server that ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    Count established connections

    I am running an IRC server currently running on port 6667, using the current hybrid and a UDP server that receives requests from a client and is supposed to pass back to the user the number of connections.
    Is there a way I can count the number connected to the irc server so I can pass it back to the users?
    I was able to do a perl script doing a netstat and passing that back to the client, but I would rather do this in C so that I might be able to add this UDP server in the IRC server code afterwards.
    My current UDP server code is as follows:
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <sys/socket.h>
    #include <resolv.h>
    
    #define DEFAULT_PORT	1235
    
    int main(int count, char *strings[])
    {	int sd;
    	int i;
    	int fport;
    	int uport;
    	int lport;
    	int sbytes = 20;
    	int port=DEFAULT_PORT;
    	struct sockaddr_in addr;
    	char buffer[1024];
    	char sbuffer[1024];
    	//char sport[6];
    	
    	char o1[5], o2[5], o3[5], o4[5];  //set string rep of each octet
    	int io1, io2, io3, io4;		//set int value for each octet
    
    	if ( count != 2 )
    		printf("usage: %s <port>\n...Using default port (%d).\n", strings[0], port);
    	else
    		port = atoi(strings[1]);
    	sd = socket(PF_INET, SOCK_DGRAM, 0);
    	bzero(&addr, sizeof(addr));
    	addr.sin_family = AF_INET;
    	addr.sin_port = htons(port);
    	addr.sin_addr.s_addr = INADDR_ANY;
    	if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    		perror("bind");
    	while (1)
    	{	int bytes, addr_len=sizeof(addr);
    
    		for (i = 0; i < 12; i++)
    		{
    			sbuffer[i] = 0;
    		}
    
    		bytes = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);
    
    /*
    		printf("msg from %s:%d (%d bytes)\n", inet_ntoa(addr.sin_addr),
    						ntohs(addr.sin_port), bytes);
    */
    
    		sscanf(inet_ntoa(addr.sin_addr), "%4[^.].%4[^.].%4[^.].%4[^.]", o1, o2, o3, o4);
    
    		//set from port to port received from
    		fport = ntohs(addr.sin_port);
    
    		//set upper byte of port	
    		uport = fport / 256;
    		//printf("upper uport = %d\n",uport);
    
    		//set lower byte of port
    		lport = fport - (uport * 256);
    		//printf("lower lport = %d\n",lport);
    	
    		//printf("sport = %s and fport = %d\n",sport, fport);
    
    		//int of each octet
    		io1 = atoi(o1);
    		io2 = atoi(o2);
    		io3 = atoi(o3);
    		io4 = atoi(o4);
    
    		//printf("From IP:%s %s %s %s: %d\n", o1, o2, o3, o4, fport);
    
    		//this is what we received in hex
    /*		printf("Received:\n");
    		printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
    			buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], 
    			buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11]);
    */
    		//set send buffer to stuff I want
    		//sbuffer[0] = buffer[4];
    		sbuffer[1] = buffer[5];
    		sbuffer[2] = buffer[6];
    		sbuffer[3] = buffer[7];
    		sbuffer[4] = buffer[8];
    		sbuffer[5] = buffer[9];
    		sbuffer[6] = buffer[10];
    		sbuffer[7] = buffer[11];
    		//sbuffer[0] = buffer[4];
    
    		/*for (i = 4; i < 12; i++)
    		{
    			sbuffer[i-4]=buffer[i];
    		}
    		*/
    		//sprintf(sport, "%x", fport);
    
    		//printf("From port in HEX: %s\n",sport);
    
    		//sprintf(sport, "%x", port);
    		//printf("To port in HEX: %s LEN = %d\n", sport, strlen(sport));
    
    		//set this to from ip		
    		sbuffer[8] = io1;
    		sbuffer[9] = io2;
    		sbuffer[10] = io3;
    		sbuffer[11] = io4;
    
    		//set this to port
    		sbuffer[12] = uport;
    		sbuffer[13] = lport;
    		
    		//not sure why i couldnt set it earlier
    		sbuffer[0] = buffer[4];
    		
    		//printf("%2x %2x\n",sbuffer[0], buffer[4]);
    
    		//pad the junk
    		sbuffer[14] = 0;
    		sbuffer[15] = 66;
    
    		//#   of users here all to 255 gives ? to users
    		sbuffer[16] = 255;
    		sbuffer[17] = 255;
    		sbuffer[18] = 255;
    		//i dont expect more than 64 put here at 19 otherwise use 18 and 19
    		sbuffer[19] = 255;
    
    /*		printf("Sending:\n");
    		printf("%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
    			sbuffer[0], sbuffer[1], sbuffer[2], sbuffer[3], sbuffer[4], sbuffer[5], 
    			sbuffer[6], sbuffer[7], sbuffer[8], sbuffer[9], sbuffer[10], sbuffer[11],
    			sbuffer[12], sbuffer[13], sbuffer[14], sbuffer[15], sbuffer[16],
    			sbuffer[17], sbuffer[18], sbuffer[19]);
    */
    		sendto(sd, sbuffer, sbytes, 0, (struct sockaddr*)&addr, sizeof(addr));
    	}
    	close(sd);
    }

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    CurrentConnections++ when a connection is made
    CurrentConnections-- when a connection is dropped
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  3. I'm missing something obvious
    By crash88 in forum C Programming
    Replies: 7
    Last Post: 07-02-2006, 12:51 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21