C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-24-2008, 12:36 PM   #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);
}
TomChesley is offline   Reply With Quote
Old 05-27-2008, 08:32 PM   #2
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
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.
abachler is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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