Find the answer to your Linux question:
Results 1 to 2 of 2
i have attached the simplest form of client .c but with multiple receives ..,,. When i try to receive a single string it all works fine ,.., But when i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4

    Some one please help with simple client


    i have attached the simplest form of client .c but with multiple receives ..,,.
    When i try to receive a single string it all works fine ,..,
    But when i receive multiple strings using two recv() the program kinda stops responding .,,.
    Some one please tell me the poblem and/or the solution ,..,.,.
    The simple code is as below ,.., it wont take much time for you to read ,,. Pleese help.,,.
    Code:
    /*
    ** client.c -- a stream socket client demo
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    
    #include <arpa/inet.h>
    
    #define PORT "8189" // the port client will be connecting to 
    
    #define MAXDATASIZE 100 // max number of bytes we can get at once 
    
    // get sockaddr, IPv4 or IPv6:
    void *get_in_addr(struct sockaddr *sa)
    {
    	if (sa->sa_family == AF_INET) {
    		return &(((struct sockaddr_in*)sa)->sin_addr);
    	}
    
    	return &(((struct sockaddr_in6*)sa)->sin6_addr);
    }
    
    int main(int argc, char *argv[])
    {
    	int sockfd, numbytes;  
    	char buf[MAXDATASIZE], input[10];
    	struct addrinfo hints, *servinfo, *p;
    	int rv;
    	char s[INET6_ADDRSTRLEN];
    
    	if (argc != 2) {
    	    fprintf(stderr,"usage: client hostname\n");
    	    exit(1);
    	}
    
    	memset(&hints, 0, sizeof hints);
    	hints.ai_family = AF_UNSPEC;
    	hints.ai_socktype = SOCK_STREAM;
    
    	if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
    		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
    		return 1;
    	}
    
    	// loop through all the results and connect to the first we can
    	for(p = servinfo; p != NULL; p = p->ai_next) {
    		if ((sockfd = socket(p->ai_family, p->ai_socktype,
    				p->ai_protocol)) == -1) {
    			perror("client: socket");
    			continue;
    		}
    
    		if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
    			close(sockfd);
    			perror("client: connect");
    			continue;
    		}
    
    		break;
    	}
    
    	if (p == NULL) {
    		fprintf(stderr, "client: failed to connect\n");
    		return 2;
    	}
    
    	inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
    			s, sizeof s);
    	printf("client: connecting to %s\n", s);
    
    	freeaddrinfo(servinfo); // all done with this structure
    	printf("enter data to send:");
    	scanf("%s",&input);
    
    	
    	if (send(sockfd,&input, strlen(input)+1 , 1) == -1)
    	{
    			perror("send");
    			exit(1);
    	}
    	printf("df");
    	
    	
    
    	if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
    	    perror("recv");
    	    exit(1);
    	}
       
    
    	buf[numbytes] = '\0';
    
    	printf("client: received '%s'\n",buf);
    	if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
    	    perror("recv");
    	    exit(1);
    	}
       
    
    	buf[numbytes] = '\0';
    
    	printf("client: received '%s'\n",buf);
    		if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
    	    perror("recv");
    	    exit(1);
    	}
       
    
    	buf[numbytes] = '\0';
    
    	printf("client: received '%s'\n",buf);
    close(sockfd);
    
    
    	return 0;
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,452
    You are using a blocking recv() call, so it will likely be waiting for more data from the socket. Use the MSG_DONTWAIT flag in the call, and check the return for the number of bytes received. If it is -1, check if errno == EAGAIN, which means that the call would have blocked if you hadn't set the MSG_DONTWAIT flag.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •