Thread: Problem with simple socket client/server program

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    Problem with simple socket client/server program

    I'm trying to write a simple program with two source files named client.c and server.c All it does is use sockets to connect a client to a server. The client types in a message and the server recieves the message echos it back to the client.

    Here is client.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdbool.h>
    #include <sys/types.h>
    
    #define MAXBUF 20
    #define FALSE 0
    #define TRUE 1
    
    struct sockaddr_in sockme;
    int s;
    
    struct servent *pse;
    char * transport = "tcp";
    int portnum;
    char *hostnum;
    
    char bufout[MAXBUF+1];
    char bufin[MAXBUF+1];
    char *cp;
    char c;
    
    int result;
    int n;
    
    int main(int argc, char *argv[])
    {
    
    	if (argc == 3) {
    		hostnum = argv[1];
    		(void) sscanf(argv[2], "%d", &portnum);
    	}
    	else {
    		(void) printf("usage: client <hostnum> <portnum>\n");
    		exit(-1);
    	}
    
    	sockme.sin_family = AF_INET;
    
    	if ((sockme.sin_addr.s_addr = inet_addr(hostnum)) == INADDR_NONE) {
    		(void) printf("Invalid dotted decimal address\n");
    		exit(-1);
    	}
    
    	sockme.sin_port = htons(portnum);
    	
    	if((s = socket(PF_INET, SOCK_STREAM, 0)) == 0)
    	{
    		/* if socket failed then display error and exit */
    		perror("Create socket");
    		exit(EXIT_FAILURE);
    	}
    
    	if ((result = connect (s, (struct sockaddr *) &sockme, sizeof(sockme)))) {
    		perror("Connect failed\n");
    		close (s);
    		exit(-1);
    	};
    
    	(void) printf("Enter Message: \n");
    	cp = bufout;
    	while ((c = getchar()) != '\n') {
    		*cp++ = c;
    	}
    	*cp = '\0';
    	
    	result = send (s, bufout, strlen(bufout), 0);
    	
    	if(result != strlen(bufout)){
    		perror("send");
    		exit(EXIT_FAILURE);
    	}
    	
    	printf("Message Sent.\nWaiting for response.\n");
    	
    	do{
    		n = read ( s, bufin, MAXBUF );
    	}while(n != 0);
    	
    	(void) printf("Recieved: %s\n", bufin);
    	result = close(s);
    
    	return 0;
    }
    Here is server.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <stdbool.h>
    #include <sys/types.h>
    
    #define MAXBUF 20
    #define FALSE 0
    #define TRUE 1
    
    struct sockaddr_in sockme, newsockme;
    int s, childs;
    char *hostnum;
    
    int type;
    int portnum;
    
    int result;
    
    char bufout[MAXBUF+1];
    char bufin[MAXBUF+1];
    char *cp;
    char c;
    
    int n;
    
    
    int main(int argc, char *argv[])
    {
    
    	if (argc == 2) {
    		(void) sscanf(argv[1], "%d", &portnum);
    	}
    	else {
    		(void) printf("usage: server <portnum>\n");
    		exit(-1);
    	}
    	
    	sockme.sin_family = AF_INET;
    	sockme.sin_addr.s_addr = INADDR_ANY;
    	sockme.sin_port = htons(portnum);
    
    	/* the following socket is used to listen for incoming connection
    	 * requests by any clients
    	 */
    	
    	if((s = socket(PF_INET, SOCK_STREAM, 0)) == 0)
    	{
    		/* if socket failed then display error and exit */
    		perror("Create socket");
    		exit(EXIT_FAILURE);
    	}
    
    
    	result = bind(s, (struct sockaddr *) &sockme, sizeof(sockme));
    	
    	if(result < 0){
    		perror("bind");
    		exit(-1);
    	}
    		
    	result = listen(s, 5);
    	
    	if(result < 0){
    		perror("listen");
    		exit(-1);
    	}
    
    	while (1) {
    		socklen_t alen = sizeof(newsockme);
    		
    		childs = accept(s, (struct sockaddr *) &newsockme, &alen);
    		if(childs < 0){
    			perror("accept");
    			exit(-1);
    		}
    		printf("Handling client %s\n", inet_ntoa(newsockme.sin_addr));
    		
    		
    		do{
    			n = read ( s, bufin, MAXBUF );
    		}while(n != 0);
    		
    		bufin[MAXBUF] = '\0';
    	
    		(void) printf("Message recieved: %s\n", bufin);
    		(void) printf("Sending: %s\n", bufin);
    		
    		result = send (childs, bufin, strlen(bufin), 0);
    		result = close(childs);
    	}
    
    	return 0;
    }
    I run server.c with the:
    ./server 5601

    and client.c with:
    ./client 127.0.0.1 5601

    I get the following output from server.c:
    Handling client 127.0.0.1

    and from client.c:
    Enter Message:
    Hello World
    Message Sent.
    Waiting For Response.

    For some reason the do while loop in server.c never exits. Can anyone spot my mistake and point me in the right direction?

    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you check the return value of read and see what's being read?


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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3
    I changed the code in server.c to read

    Code:
    		
    do{
    	n = read ( s, bufin, MAXBUF );
    	(void) printf("Message recieved: %s\n", bufin);
    }while(n != 0);
    I directed standard output to the file output.txt and tried to send "Hello World" again. When I looked in output.txt there were just thousand of "Message recieved:" but no hello world anywhere.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3
    quick update:

    Code:
    		do{
    			n = read ( s, bufin, MAXBUF );
    			(void) printf("Message recieved: %s\n", bufin);
    			if(n < 0){
    				perror("read");
    				exit(-1);
    			}
    		}while(n != 0);
    returns:
    "read: transport endpoint not connected"

    Anyone have any ideas?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't think you can bind a server and a listener on the same port on the same computer, running at the same time.


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

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    87
    You are trying to read something off the wrong socket in the server:
    Code:
    n = read ( s, bufin, MAXBUF );
    You did it correct later on:
    Code:
    result = send (childs, bufin, strlen(bufin), 0);
    result = close(childs);
    Out of curiosity, what does the (void) cast do next to printf()?

    Jason

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It quiets the warnings from his particular warning mode when compiling. Other than that, nothing.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. little problem with a simple shell program i made.
    By keira in forum Linux Programming
    Replies: 2
    Last Post: 02-26-2008, 11:42 PM
  2. Socket Select problem
    By saipkjai in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-08-2008, 10:57 AM
  3. Socket program
    By mhetfield in forum C Programming
    Replies: 5
    Last Post: 04-03-2007, 03:46 PM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. Simple program problem.
    By RealityFusion in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2004, 05:08 AM