Thread: server/client trouble yet again lol

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    server/client trouble yet again lol

    yeah man i was taking a break from coding just dirt biking all day. I started to miss coding, so i tried to brush up on my tcp socket programming and i am now running into a problem. I have been working on this freaking thing for the last 4 hours lol NO LUCK!

    The handshake is OK. Then i send the server a message, the server receives, then prompts. Server sends, client receives, but control is returned back to the server.

    So the server is prompted twice in a row. After you send the second message on the server side, control is returned back to the client. Client prompts, and when you hit the return key, it then displays the second message from the server.

    This thing is freaking a pain in my ass. Can someone please help me spot the bug?

    RUNNING CLIENT in TERMINAL:
    Code:
    ubuntu@ubuntu:~/Documents$ ./client 127.0.0.1 12348
    Made a connection to 189.204.35.0
    Message: hey
    sup
    Message: hey 2
    sup 2
    Message: ^C
    RUNNING SERVER in TERMINAL:
    Code:
    ubuntu@ubuntu:~/Documents$ ./server 12348
    hey
    Message(1): sup
    Message(1): sup 2
    hey 2
    Message(1): ^C
    CLIENT read and write CODE:
    Code:
    for( ; ; ) { 
    int i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer)); 
      	fprintf(stdout, "Message: "); 
    	fgets(buffer, sizeof(buffer)-1, stdin); 
    		if((writefd = write(sockfd, buffer, sizeof(buffer))) < 0)
    		{ 
    		fprintf(stderr, "%s. WRITE(c)\n", strerror(errno));
    		exit(1);
    		}
    
    		if(writefd == 0)
    		{
    		printf("Nothing was written.\n");
    		}
    
    
    	i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer)-1); 
    
    	if((readfd = read(sockfd, buffer, sizeof(buffer))) < 0) 
    	{ 
    	fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr)); 
    	printf("%s. READ(c)\n", strerror(errno));
    	exit(1);
    	} 
    	
    	//Test to see if the buffer is blank. Uncomment to test.
    	if(readfd == 0)
    	{
    	printf("Null buffer. READ()\n");
    	}
    		else 
    		{
    		  fprintf(stdout, "%s", buffer);
    		}
    
    }
    SERVER read and write CODE:
    Code:
    for( ; ; ) {
    int i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer)-1); 
    	if((readfd = read(newsockfd, buffer, sizeof(buffer))) < 0) 
    	{ 
    	fprintf(stderr, "%s. READ(s)\n", strerror(errno)); 
    	exit(1);
    	} 
    
    	//Test to see if the buffer is blank. Uncomment to test.
    	if(readfd == 0)
    	{
    	printf("Null buffer. READ()\n");
    	}
    		else 
    		{
    		  fprintf(stdout, "%s", buffer); 
    		  //If you use getchar() here, it will not double prompt, but a carriage retur is required.
    		}
    
    i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer));
      fprintf(stdout, "Message(1): ");  
      fgets(buffer, sizeof(buffer)-1, stdin); 
    	if((writefd = write(newsockfd, buffer, sizeof(buffer))) < 0) 
    	{
    	fprintf(stderr, "%s. WRITE(s)\n", strerror(errno));
    	exit(1);
    	}
    
    	if(writefd == 0)
    	{
    	printf("Nothing was written.\n");
    	}
    	
    
    }
    
    //Does it matter what order I close the fd's in?
    close(sockfd);
    close(newsockfd);
    Last edited by Annonymous; 11-13-2011 at 03:04 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, talking of repeating the same message twice...

    How about a whole thread repeated twice?
    select server writes message twice then hangs
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    lol Thats pretty funny Salem! I had back ups of working servers on ripway but i cant access the site because it says its under construction.

    I never did post the working updated server in that other thread. Im kicking myself in the butt now, wishing i did!

  4. #4
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    <says sarcastically>i appreciate your help salem! Keep up the good work lol

    Well with the help of our awesome member Salem, i solved the problem. On the client side, i was using sizeof instead of strlen on the write function. Thanks Salem!!

    Let me post both the working client and server so i have a back up this time lol

    CLIENT:
    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <sys/socket.h> 
    #include <sys/types.h> 
    #include <netdb.h> 
    #include <netinet/in.h> 
    #include <arpa/inet.h>
    #include <errno.h>
    
    int main(int argc, char *argv[]) { 
    int sockfd, portno, readfd, writefd, yes = 1; 
    char buffer[1026]; 
    struct hostent *server; 
    struct sockaddr_in serv_addr, cli_addr;
    
    	if(argc < 3) fprintf(stderr, "Ussage: %s IP Address port #", argv[0]);
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    	if(sockfd < 0)  
    	{
    	fprintf(stderr, "%s.", strerror(errno));
    	exit(1);
    	}
    
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
    
    bzero((char *) &serv_addr, sizeof(serv_addr)); 
    server = gethostbyname(argv[1]); 
    	if(server == NULL) 
    	{
    	fprintf(stderr, "No such host.");
    	printf("%s", strerror(errno)); 
    	exit(1);
    	}
    portno = atoi(argv[2]); 
    serv_addr.sin_family = AF_INET; 
    memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); 
    serv_addr.sin_port = htons(portno);
    
    	if(connect(sockfd, (const struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    	  { 
    	  fprintf(stderr, "%s. CONNECT()\n", strerror(errno));
    	  exit(1);
    	  } 
    		else 
    		{ 
    		  fprintf(stdout, "Made a connection to %s\n", inet_ntoa(cli_addr.sin_addr)); 
    		}
    
    for( ; ; ) { 
    int i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer)); 
      	fprintf(stdout, "Message: "); 
    	fgets(buffer, sizeof(buffer)-1, stdin); 
    		if((writefd = write(sockfd, buffer, strlen(buffer))) < 0)
    		{ 
    		fprintf(stderr, "%s. WRITE(c)\n", strerror(errno));
    		exit(1);
    		}
    
    		/*if(writefd == 0)
    		{
    		printf("Nothing was written.\n");
    		exit(1);
    		}*/
    
    
    	i = sizeof(buffer); if(i > 0) bzero(buffer, sizeof(buffer)-1); 
    
    	if((readfd = read(sockfd, buffer, sizeof(buffer))) < 0) 
    	{ 
    	fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr)); 
    	printf("%s. READ(c)\n", strerror(errno));
    	exit(1);
    	} 
    	
    	//Test to see if the buffer is blank. Uncomment to test.
    	/*if(readfd == 0)
    	{
    	printf("Null buffer. READ()\n");
    	exit(1);
    	}*/
    		else 
    		{
    		  fprintf(stdout, "%s", buffer);
    		}
    
    }
    
    close(sockfd);
    
    return 0;
    }
    SERVER:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <errno.h>
    
    int main(int argc, char *argv[]) {
    int sockfd, newsockfd, portno, readfd, writefd;
    char buffer[1024]; 
    socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0) 
    	{
    	printf("%s. SOCKET()", strerror(errno));
    	exit(1);	
    	}
    
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(portno);
    
    //Bind returns zero upon success, and -1 on error.
    bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    if(bind < 0) 
    	{
    	printf("%s. BIND()", strerror(errno));
    	exit(1);	
    	}
    
    listen(sockfd, 10);
    clilen = sizeof(cli_addr);
    
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if(newsockfd < 0) 
    	{
    	printf("%s. ACCEPT()", strerror(errno));
    	exit(1);	
    	}
    
    for( ; ; ) {
    int i = sizeof(buffer); if(i > 0) bzero(buffer, 1024); 
    	if((readfd = read(newsockfd, buffer, sizeof(buffer)-1)) < 0) 
    	{ 
    	fprintf(stderr, "%s. READ(s)\n", strerror(errno)); 
    	exit(1);
    	} 
    
    	//Test to see if the buffer is blank. Uncomment to test.
    	/*if(readfd == 0)
    	{
    	printf("Null buffer. READ()\n");
    	exit(1);
    	}*/
    		else 
    		{
    		  fprintf(stdout, "%s\n", buffer); 
    		  //If you use getchar() here, it will not double prompt, but a carriage retur is required.
    		}
    
    i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer));
      fprintf(stdout, "Message: ");  
      fgets(buffer, sizeof(buffer)-1, stdin); 
    	if((writefd = write(newsockfd, buffer, sizeof(buffer)-1)) < 0) 
    	{
    	fprintf(stderr, "%s. WRITE(s)\n", strerror(errno));
    	exit(1);
    	}
    
    	/*if(writefd == 0)
    	{
    	printf("Nothing was written.\n");
    	exit(1);
    	}*/
    	
    
    }
    
    close(newsockfd);
    close(sockfd);
    
    return 0;
    }
    I appreciate all the help lol

    Feel free to respond with extreme hostility, because of my sarcastic post lol Even tho half of the members on here do the same because they have a huge ego<puffs chest out and mockingly says> I'm a software engineer thats too good to respond with a simple response lol Im better than everyone else.

    Psht, yeah ok.
    Last edited by Annonymous; 11-13-2011 at 05:22 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm, I compiled and ran these:

    SERVER SIDE:
    Code:
    root~/C»./serve 666
    okay
    
    Message: yes
    and
    
    Message: then
    so
    
    Message: what
    huh
    
    Message: etc...
    check!
    CLIENT SIDE
    Code:
    root~/C»./a.out 127.0.0.1 666
    Made a connection to 16.0.0.0
    Message: okay
    yes
    Message: and
    then
    Message: so
    what
    Message: huh
    etc...
    Message: check!
    The client prints the wrong address in "Made a connection" because of this:

    Code:
              fprintf(stdout, "Made a connection to %s\n", inet_ntoa(cli_addr.sin_addr));
    "cli" should be "serv". But I don't experience the problem you describe...of course, the code from post #4 is slightly different than the code from post #1; eg, there is no (1) in the server prompt.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Well MK27, for some reason when I used strlen on the clientside write function it started working properly. Why is that? It works properly now.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Annonymous View Post
    Well MK27, for some reason when I used strlen on the clientside write function it started working properly. Why is that? It works properly now.
    That's certainly better than using sizeof(), since you do not want to send the junk in the buffer after the '\0'. The code in post #4 uses strlen(). I don't know why that would make the difference tho.

    You should change the server code to use strlen() in the write too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Will do MK27, thank you.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you're still pointlessly calling bzero() all over the place, and even more pointlessly, you have if statements which evaluate to compile-time constants.

    i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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, 11:45 AM
  2. how we can get client id in server
    By cnu_sree in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-04-2006, 05:14 AM
  3. Client - Server
    By sCandal2 in forum Windows Programming
    Replies: 5
    Last Post: 11-16-2004, 06:24 AM
  4. both client & server?
    By pode in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-16-2003, 02:13 PM
  5. client / server
    By PutoAmo in forum C Programming
    Replies: 4
    Last Post: 05-24-2002, 05:09 PM