Like Tree4Likes
  • 2 Post By anduril462
  • 1 Post By anduril462
  • 1 Post By anduril462

tcp client/server pair can send data only one way

This is a discussion on tcp client/server pair can send data only one way within the C Programming forums, part of the General Programming Boards category; Consider the following code fragment: Code: <...> char arrOutBuffer[]="Robtarget Data..."; n = send(s,arrOutBuffer,strlen(arrOutBuffer),0); if (n < 0) perror("ERROR writing to ...

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

    tcp client/server pair can send data only one way

    Consider the following code fragment:

    Code:
    	<...>		char arrOutBuffer[]="Robtarget Data...";
    		n = send(s,arrOutBuffer,strlen(arrOutBuffer),0);
    		if (n < 0) perror("ERROR writing to socket");
    
    
    		char arrRespBuffer[256];
    		memset(arrRespBuffer,0,256);
    		n = recv(s,arrRespBuffer,strlen(arrRespBuffer),0);
    		if (n < 0) perror("ERROR reading from socket");
    		if (n == 0) puts("'n' is 0");
    		<...>
    Why is it that when I send data ("Robtarget Data...") from this client to the server the server reads/displays it properly BUT when I send a response from the server the client says "'n' is 0"?

    The applicable server fragment is:

    Code:
        <...>
        int a=1;
        while(a)
        {
            listen(sockfd,5);
            clilen = sizeof(cli_addr);
            newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
            if (newsockfd < 0) error("ERROR on accept");
    
    
            bzero(buffer,256);
            n = read(newsockfd,buffer,255);                
            if (n < 0) error("ERROR reading from socket");
            printf("Here is the message: %s\n",buffer);
    
    
            n = write(newsockfd,"I got your message",18);
            if (n < 0) error("ERROR writing to socket");
            close(newsockfd);
        }
        <...>
    The client is compiled for Win32.
    The server is compiled for Linux (cygwin).
    I don't have this problem when the client is written/compiled for Cygwin/Linux.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Code:
    memset(arrRespBuffer,0,256);
    n = recv(s,arrRespBuffer,strlen(arrRespBuffer),0);
    You set arrRespBuffer to all zeros (null characters). You then try to read strlen(arrRespBuffer) bytes. What do you think strlen of that buffer is?
    Salem and evb@studioe.com like this.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    I think it's 256:

    Code:
    char arrRespBuffer[256];

    EDIT: Correction, THOUGHT 256.
    Just ran a test. You're right of course.

    Thanks once more.
    Last edited by evb@studioe.com; 02-24-2012 at 05:43 PM. Reason: update

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Yeah, you want sizeof(arrRespBuffer), not strlen(arrRespBuffer).

    EDIT: That will only work if the declaration of arrRespBuffer is visible when you use sizeof. That is, if it's local to that function, or global. But global variables are evil, and you wouldn't dare use them, now would you?

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    No sir, no global variables.

    But question, why is it that both:

    Code:
    char arrRespBuffer[256];
    printf("1st strlen(arrRespBuffer):\t %i\n",strlen(arrRespBuffer));
    and:
    Code:
    char arrRespBuffer[25];
    printf("1st strlen(arrRespBuffer):\t %i\n",strlen(arrRespBuffer));
    give me:

    1st strlen(arrRespBuffer): 3
    ???


    EDIT: sizeof did it!
    Last edited by evb@studioe.com; 02-24-2012 at 07:01 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by evb@studioe.com View Post
    But question, why is it that both:
    ...
    give me:
    Sheer dumb luck. You're messing with uninitialized variables, and thus undefined behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 12-03-2011, 05:41 AM
  2. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 10:58 AM
  3. 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
  4. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  5. storing client data in server app
    By codec in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2003, 10:34 PM

Tags for this Thread


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