Thread: Socket Help - Multiple Clients

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Socket Help - Multiple Clients

    Hey everyone,

    This is my first post on the forum, and I need some help. I am working on a basic client/server style program. Basically the server allows for 3 clients to connect at the same time, and it takes in bid requests and the server needs to keep track of a global bidding value that all clients can see. Here is my hacked up server code, I'm not sure why my forking method is not working. Thanks in advance.

    Code:
    /* Include all libraries for Berkeley functions */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/times.h>
    #include <sys/types.h>
    #include <strings.h>
    #include <string.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    
    /* a default port and the maximum buffer length */
    #define DEFAULT_PORT 6565 
    #define BUFLEN 256	
    
    int x;
    
    int main(int argc, char **argv)
    {
    	
    	/* variable declartion */
    	int n, sd, new_sd, port, client_len, ip, pid, items;
    	char buffer[BUFLEN], *bp;
    	struct sockaddr_in server, client;
    	struct hostent *the_client;
    
    	/* switch case for command line arguments
             case 1 - the default port # is assigned to the socket (port 6565)
    	 case 2 - the user can assign a port # instead of default */
    	switch(argc)
    	{
    		case 1:
    			port = DEFAULT_PORT;
    			break;
    		case 2:
    			port = atoi(argv[1]);
    			break;
    		default:
    			fprintf(stderr, "Usage %s [port]\n", argv[0]);
    			fprintf(stderr, "Exiting...\n");
    			exit(0);
    	}
    
    	sd = socket(AF_INET, SOCK_STREAM, 0);	//create the socket to use socket stream
    
    	/* check to see if sd returned a value < 0 - checking if socket was created */
    	if (sd < 0)
    	{
    		fprintf(stderr, "ERROR: opening socket\n");
    		fprintf(stderr, "Exiting...\n");
    		exit(0);
    	}
    	
    	/* setup server attributess*/
    	bzero((char *) &server, sizeof(server));
    	server.sin_family = AF_INET;
    	server.sin_addr.s_addr = INADDR_ANY;
    	server.sin_port = htons(port);
    
    	/* binding the socking */
    	if (bind(sd, (struct sockaddr *) &server, sizeof(server)) < 0)
    	{
    		fprintf(stderr, "ERROR: binding socket\n");
    		fprintf(stderr, "Exiting...\n");
    		exit(0);
    	}
    
    	/* queue up to 5 requests */
    	listen(sd, 5);
    	client_len = sizeof(client);
    
    	char *item;
    
    	/* create random price for clients to bid on item*/
    	srand( (unsigned)time( NULL ) );
    	items = randomStartingPrice(1,4);
    	switch (items)
    	{
    		case 1: item = "Music CD"; break;
    		case 2: item = "DVD Player"; break;
    		case 3: item = "Sports Card"; break;
    		case 4: item = "House"; break;
    		default: break;
    	}
    	printf("Bidding price starts at for %s: $%d.00\n", item, items);
    	printf("\n**** Customer Bids Below ****\n");
    
    
    	int PRICE = 0;
    	/* infinite loop to keep socket tcp connection open*/
    	while(1)
    	{	printf("price is %d\n", x);
    		/* accept - */
    		new_sd = accept(sd, (struct sockaddr *) &client, &client_len);
    	
    		if (new_sd < 0)
    		{
    			fprintf(stderr, "ERROR: on accepting\n");
    			fprintf(stderr, "Exiting...\n");
    			exit(0);
    		}
    		
    			//close(sd);	
    		pid = fork();
    		
    
    		
    		if (pid == 0)
    		{	printf("forking...\n");
    			close(sd);
    			
    		}	
    
    		if (the_client < 0)
    		{
    			fprintf(stderr, "ERROR: on get IP\n");
    			fprintf(stderr, "Exiting...\n");
    			exit(0);
    		}
    		
    		//sprintf(buffer, "CURRENT PRICE: $%d.00\n", PRICE);
    		//n = write(new_sd, buffer, strlen(buffer));	//send the price to the clients
    		
    		//if (n < 0)
    		//{
    		//	fprintf(stderr, "ERROR: socket write\n");
    		/////	fprintf(stderr, "Exiting...\n");
    		//	exit(0);
    		//}
    		
    		/* receive the bid from the client */
    		bzero(buffer, BUFLEN);
    		n = read(new_sd, buffer, BUFLEN);
    		
    		if (n < 0)
    		{
    			fprintf(stderr, "ERROR: socket read\n");
    			fprintf(stderr, "Exiting...\n");
    			exit(0);
    		}
    		
    		
    		/* extract the price from the clients bid */
    		int new_price = atoi(buffer);
    
    		if (new_price > PRICE)
    		{
    			PRICE = new_price;
    			/* receieve bid from client */
    			int peerlen = sizeof(struct sockaddr);
    			getpeername(new_sd, (struct sockaddr *)&client, &client_len);
    			printf("Client IP: %s - Bids : $ %d.00 dollars\n", inet_ntoa(client.sin_addr), PRICE);
    			
    			/*send the price of the bid to the client */
    			bzero(buffer, BUFLEN);
    			sprintf(buffer, "Your Bid was: $%i.00\n", PRICE);
    			n = write(new_sd, buffer, strlen(buffer));	//send the price to the clients
    			
    			/* pass new price to function below*/
    			savePrice(PRICE);
    		}
    		else
    		{
    			bzero(buffer, BUFLEN);
    			sprintf(buffer, "Bid is too small!\n");
    			n = write(new_sd,buffer, strlen(buffer));
    		}
    		x = PRICE;
    	
    	//else
    	//{
    		close(new_sd);
    	//}//
    
    	}
    	/* close all sockets */
    	//close(new_sd);
    	//close(sd);
    	return(0);
    }
    
    /*Fuction to return the random value */
    int randomStartingPrice(int min, int max)
    {
    	return ( rand() % (max - min) + min );
    }
    
    int savePrice(int p)
    {
    	int newp;
    	newp = p;
    	return newp;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why fork? Just use non-blocking sockets.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    can someone post an example code of non blocking sockets.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    See http://www.google.com/q=%22non-blocking+sockets%22+C

    And see http://beej.us/guide/bgnet/output/ht....html#advanced

    Using non-blocking I/O is often not a good idea, since you have to busy-wait and waste CPU time checking for non-existent data.

    Another option would be to use select() or poll(). These functions will block until a descriptor is ready to receive data.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's usually what you do with non-blocking sockets. You use select to see when a read is ready.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    But you don't need to use non-blocking sockets with select.

    The point of select is that it blocks, and returns when and only when (unless a timeout is specified) a call to read() (or accept(), etc.) will not block on that descriptor, and the reason it of course won't block is because you've waiting for there to be data ready.

    By contrast, a non-blocking socket is one where you can call read any time, regardless of whether there's data available, and it would return unsucessfully with an errno of EWOULDBLOCK. This feature is unnecessary when using select, the socket can still be blocking, you just use select to make sure there's data ready.

    I guess it boils down to interpretation
    Last edited by cwr; 11-17-2005 at 02:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-11-2008, 11:04 PM
  2. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 10:34 AM
  3. Problem using sockets for multiple clients
    By turmoil in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-01-2005, 07:12 PM
  4. Socket Project (getting one of multiple clients IP)
    By Cl0wn in forum C Programming
    Replies: 2
    Last Post: 04-05-2003, 12:01 PM
  5. Replies: 2
    Last Post: 03-05-2002, 05:52 AM