c program client server

This is a discussion on c program client server within the Networking/Device Communication forums, part of the General Programming Boards category; Hello, I have written a client and server c program, which I have taken from some example code. However, the ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    c program client server

    Hello,

    I have written a client and server c program, which I have taken from some example code. However, the program run ok, but I can't get anything back from the server. The client should send a single character and the server should increment and then send it back again.

    Have I done anything wrong.

    Please feel free to point out any bad programming code.

    Many thanks for any suggestions,

    Steve

    My client code below:
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[])
    {
       printf("This is the client program\n");
    
       int sockfd;
       int len;
       struct sockaddr_in address;
       int result;
       char ch = 'A';
    
       //Create socket for client.
       sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
       //Name the socket as agreed with server.
       address.sin_family = AF_INET;
       address.sin_addr.s_addr = inet_addr("127.0.0.1");
       address.sin_port = htons(7734);
       len = sizeof(address);
    
       //
       result = connect(sockfd, (struct sockaddr *)&address, len);
    
       if(result == 1)
       {
          perror("Error has occurred");
          exit(0);
       }
    
       //Read and write via sockfd
       write(sockfd, &ch, 1);
       read(sockfd, &ch, 1);
       printf("Char from server = %c\n", ch);
       close(sockfd);
    
       exit(0);
    }
    Server code:
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[])
    {
       //Declaring process variables.
       int server_sockfd, client_sockfd;
       int server_len, client_len;
       struct sockaddr_in server_address;
       struct sockaddr_in client_address;
    
       //Remove any old socket and create an unnamed socket for the server.
       server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
       server_address.sin_family = AF_INET;
       server_address.sin_addr.s_addr = htons(INADDR_ANY);
       server_len = sizeof(server_address);
    
       bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
    
       //Create a connection queue and wait for clients
       listen(server_sockfd, 5);
       while(1)
       {
          char ch;
          printf("server waiting\n");
    
          //Accept a connection
          client_len = sizeof(client_address);
          client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
    
          //Read write to client on client_sockfd
    
          read(client_sockfd, &ch, 1);
          ch++;
          write(client_sockfd, &ch, 1);
          close(client_sockfd);
       }
    
       return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should check the return value from socket().

    You should check the return value from read and write to see that the read and write operations go through OK - they can fail under some circumstances.

    Likewise for bind() and listen()

    You probably don't want to close the socket inside the while(1) loop, as that will prevent a second request from getting through.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I've gotten these working with each other.

    CLIENT
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    
    ///   CLIENT   
    
    
    int main(int argc, char *argv[])
    {
    	printf("This is the client program\n");
    
    	int sockfd;
    	int len, rc ;
    	struct sockaddr_in address;
    	int result;
    	char ch = 'A';
    
       //Create socket for client.
    	sockfd = socket(PF_INET, SOCK_STREAM, 0);
    	if (sockfd == -1) { 
    		perror("Socket create failed.\n") ; 
    		return -1 ; 
    	} 
    	
    	//Name the socket as agreed with server.
    	address.sin_family = AF_INET;
    	address.sin_addr.s_addr = inet_addr("127.0.0.1");
    	address.sin_port = htons(7734);
    	len = sizeof(address);
    
    	result = connect(sockfd, (struct sockaddr *)&address, len);
    	if(result == -1)
    	{
    		perror("Error has occurred");
    		exit(-1);
    	}
    
    	while ( ch < 'Y') {
    
    		//Read and write via sockfd
    		rc = write(sockfd, &ch, 1);
    		printf("write rc = &#37;d\n", rc ) ; 
    		if (rc == -1) break ; 
    		
    		read(sockfd, &ch, 1);
    		printf("Char from server = %c\n", ch);
    		//if (ch == 'A') sleep(5) ;  // pause 5 seconds 
    	} 
    	close(sockfd);
    
    	exit(0);
    }
    SERVER
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    ///   SERVER   
    
    int main(int argc, char *argv[])
    {
    	//Declaring process variables.
    	int server_sockfd, client_sockfd;
    	int server_len ; 
    	int rc ; 
    	unsigned client_len;
    	struct sockaddr_in server_address;
    	struct sockaddr_in client_address;
    
    	//Remove any old socket and create an unnamed socket for the server.
    	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    	server_address.sin_family = AF_INET;
    	server_address.sin_addr.s_addr = htons(INADDR_ANY);
    	server_address.sin_port = htons(7734) ; 
    	server_len = sizeof(server_address);
    
    	rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
    	printf("RC from bind = %d\n", rc ) ; 
    	
    	//Create a connection queue and wait for clients
    	rc = listen(server_sockfd, 5);
    	printf("RC from listen = %d\n", rc ) ; 
    
    	client_len = sizeof(client_address);
    	client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
    	printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
    
    	while(1)
    	{
    		char ch;
    		printf("server waiting\n");
    
    		//Accept a connection
    		//client_len = sizeof(client_address);
    		//client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
    		//printf("after accept()... client_sockfd = %d\n", client_sockfd) ; 
    		//Read write to client on client_sockfd
    
    		rc = read(client_sockfd, &ch, 1);
    		printf("RC from read = %d\n", rc ) ; 		
    		if (ch=='X') break ; 
    		ch++;
    		write(client_sockfd, &ch, 1);
    	}
    
    	printf("server exiting\n");
    
    	//close(client_sockfd);
    	close(client_sockfd);
    	return 0;
    }
    Todd

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Thanks for your help,

    Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 10:45 AM
  2. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 04:01 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 05:31 AM
  5. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 02:58 PM

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