Like Tree1Likes
  • 1 Post By christop

not receiving messages in server/client program

This is a discussion on not receiving messages in server/client program within the C Programming forums, part of the General Programming Boards category; hello, i am trying to implement a tcp client and tcp server. i am able to establish the connection but ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    38

    not receiving messages in server/client program

    hello,

    i am trying to implement a tcp client and tcp server. i am able to establish the connection but when i send a message from client, the server is not able to receive it. here is my code below, if you can plz let me know what i am doi ng wrong, i wud really appreciate it. i think there is a problem in my server implementation

    Client:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    #define PORT 3490
    
    
    int main(int argc, char *argv[])
    {
    	struct sockaddr_in server_info;
    	struct hostent *he;
    	int socket_fd,num;
    	char *buffer;
    	
    	if (argc != 2) {
    		fprintf(stderr, "Usage: client hostname\n");
    		exit(1);
    	}
    	
    	if ((he = gethostbyname(argv[1]))==NULL) {
    		fprintf(stderr, "Cannot get host name\n");
    		exit(1);
    	}
    	
    	if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
    		fprintf(stderr, "Socket Failure!!\n");
    		exit(1);
    	}
    	
    	memset(&server_info, 0, sizeof(server_info));
    	server_info.sin_family = AF_INET;
    	server_info.sin_port = htons(PORT);
    	server_info.sin_addr = *((struct in_addr *)he->h_addr);
    	if (connect(socket_fd, (struct sockaddr *)&server_info, sizeof(struct sockaddr))<0) {
    		//fprintf(stderr, "Connection Failure\n");
    		perror("connect");
    		exit(1);
    	}
    	
    	buffer = "Hello World!! I am networking!!\n";
    	if ((send(socket_fd,buffer, sizeof(buffer),0))== -1) {
    		fprintf(stderr, "Failure Sending Message\n");
    		exit(1);
    	}
    	else {
    		printf("Message being sent: %s\n",buffer);
    	}
    	close(socket_fd);	
    	
    }
    Server:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    #define PORT 3490
    #define BACKLOG 10
    
    
    int main()
    {
    	struct sockaddr_in server;
    	struct sockaddr_in dest;
    	int status,socket_fd, client_fd,size,num;
    	char *buffer;
    	int yes =1;
    	
    	memset(&server, 0, sizeof(server));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(PORT);
    	server.sin_addr.s_addr = INADDR_ANY; 
    	
    	if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
    		fprintf(stderr, "Socket failure!!\n");
    		exit(1);
    	}
    	
    	if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
    	
    	if ((status = bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1)	 { //sizeof(struct sockaddr) 
    		fprintf(stderr, "Binding Failure\n");
    		exit(1);
    	}
    	
    	if ((listen(socket_fd, BACKLOG))== -1){
    		fprintf(stderr, "Listening Failure\n");
    		exit(1);
    	}
    	while(1) {
    		
    		size = sizeof(dest);
    		if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size)==-1)) {
    			fprintf(stderr,"Accept Failure\n");
    		}
    		printf("Server got connection from client %s\n", inet_ntoa(dest.sin_addr));
    		buffer = "Hello World!! I am networking!!\n";
    	
    		if ((num = recv(client_fd, buffer, 1024,0))== -1) {
    			fprintf(stderr,"Error in receiving message!!\n");
    			exit(1);
    		}	
    		buffer[num] = '\0';
    		printf("Message received: %s\n", buffer); 
    		close(client_fd);	
    		//close(socket_fd);	
    	}	
    	
    }
    i would really appreciate some help.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    I don't see anything especially wrong in the client, but in the server you're not allocating space for the buffer. As it is, you're pointing buffer to a const char array (a string literal).

    Instead of this:
    Code:
     char *buffer;
    You could do this:
    Code:
     char buffer[1024+1]; // +1 for nul terminator
    And remove the string literal.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    i did that but still no result.

    the error i am getting is on server side:

    recv: Socket operation on non-socket

    any other suggestions?? i am only one step away from establishing my client-server communication!!

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    since i am running both client and server on same machine, i run my program as:

    for server:
    gcc -o server ue_server.c
    ./server

    for client:

    gcc -o client ue_client.c
    ./client 127.0.0.1

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    It's a bug with your parentheses:
    Code:
     if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size)==-1)) {
    Should be:
    Code:
     if ((client_fd = accept(socket_fd, (struct sockaddr *)&dest, &size))==-1) {
    Notice the movement of a closing parenthesis. Your version always set client_fd to 0 (or to 1 if accept failed).
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    yes, that was the mistake!!! thnx alot!!
    i am able to now send and receive messages but only one at a time!!
    how can i maintain a continuous connection?? so that if i send anything via client the server will echo it and viceversa and then the client/server waits for the next message??? shud i have another infinite while loop??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server sends unknown number of messages, client doesn't know when he stops
    By Phoenix_Rebirth in forum Networking/Device Communication
    Replies: 0
    Last Post: 11-21-2010, 01:48 PM
  2. 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, 11:45 AM
  3. Replies: 2
    Last Post: 11-23-2007, 02:10 AM
  4. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 03:58 PM
  5. Simple client - receiving data from server and keyboard w/o blocking
    By Spitball in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-08-2005, 12:32 PM

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