CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    6

    [RESOLVED] Read returns 0 or -1 (while data are present)

    Hello everybody,

    I'm not a socket expert but I have some experience with serial ports I/O under linux...

    I am working on client/server project for a home-brew protocol. There was a previous implementation of the client and the server but I have to make a cleaner one. We use the BSD sockets.

    On the client side I have to send a message (which is OK) and then wait for the header of the response. The header is a 16 bytes structures.

    I use select to wait for data. select returns 1 indicating that there is data to read... However, read() always returns -1 or 0.

    I have absolutely no idea of what is going on... Any help would be appreciated.

    Thanks in advance,
    Mathieu

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Read returns 0 or -1 (while data are present)

    Please post some code. It's impossible to diagnose based on what you've said.

  3. #3
    Join Date
    Apr 2010
    Posts
    6

    Re: Read returns 0 or -1 (while data are present)

    Hello,

    The socket is open as usual:
    Code:
      *sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (*sockfd < 0) {
        sockerror("in connect_to_robot (socket creation)");
        return DOCONNECT_FAIL;
      };
    
      /* for stream (TCP) sockets, specify "nodelay" */
      int opt = TRUE;
      if (setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, (const void *)&opt, sizeof(opt)) < 0) {
        sockerror("in connect_to_robot (setsockopt(TCP_NODELAY))");
        closesocket(*sockfd);
        *sockfd = SOCKET_ERROR;
        return DOCONNECT_FAIL;
      }
    Connection is OK. I can write on this socket.

    To read the header I use the code ("header" is the variable where I want to put the header; its a struct with the right fields and HEADER_SIZE is the size of such structure i.e. 16 bytes):
    Code:
            uint8_t *rcvptr, *buffer = (uint8_t *)header;
    	size_t to_read=HEADER_SIZE, total_read=0;
    	ssize_t n_read;
    
            /* Wait for response */
    	FD_ZERO(&readfds);
    	FD_SET(fd, &readfds);
    	readyfds = readfds;
    
    	rcvptr = buffer;
    	while (to_read) {
    		ready = select(FD_SETSIZE, &readyfds, NULL, NULL, NULL);
    		fprintf(stderr, "ready=%d\n", ready);
    		if (ready < 0) { /* select returned an error */
    			if (errno!=EINTR) { /* this is not an interruption */
    				perror("read_header");
    				return READ_MESSAGE_FAIL;
    			} else {
    				continue;
    			}
    		}
    		if (ready == 0) { /* select timed out */
    			return READ_MESSAGE_FAIL;
    		}
    
    		/* We can read in the buffer */
    		if (FD_ISSET(fd, &readfds)) {
    	  		fprintf(stderr, "read=to_read=%zu\n", to_read);
    			n_read = read(fd, rcvptr, to_read);
            	        fprintf(stderr, "read=n_read=%zd\n", n_read);
    			if (n_read < 0) {
    				perror("read_header");
    				return READ_MESSAGE_FAIL;
            		}
    			rcvptr += n_read;
    			total_read += (size_t)n_read;
    			to_read -= n_read;
    		}
    	}
    Executing the code I get:
    Code:
    ready=1
    read=to_read=16
    read=n_read=0
    ready=1
    read=to_read=16
    read=n_read=0
    ready=1
    read=to_read=16
    read=n_read=0
    ....
    AFAI the line "ready=1" indicates that the socket is ready for reading. But read() always returns 0...

    Thanks for your reply.

  4. #4
    Join Date
    Apr 2010
    Posts
    6

    Re: Read returns 0 or -1 (while data are present)

    Hello,

    I have realized that there is a nasty bug in my code (I don't send enough data to the server). I can't test now (it's too late) but I will keep you informed later...
    Last edited by duboism; April 15th, 2010 at 03:24 PM. Reason: Hey, hey a bug in a but

  5. #5
    Join Date
    Apr 2010
    Posts
    6

    Re: Read returns 0 or -1 (while data are present)

    Hum, apparently this was not the cause...

  6. #6
    Join Date
    Apr 2010
    Posts
    6

    Re: Read returns 0 or -1 (while data are present)

    Hum, in fact I was sending too much data to the server...

    Now select never returns...

  7. #7
    Join Date
    Apr 2010
    Posts
    6

    Re: Read returns 0 or -1 (while data are present)

    Hello,

    After some intense hacking my colleagues and I realized that we misunderstood the protocol... In fact the server was replying on another port! This seems a rather strange design...

    Anyway it works now.

    Thanks for replying (and kudos to my colleagues).

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured