This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: recv() blocking for non-blocking socket



Here is a simple version of what I'm doing:


/* Start of source code */
#define FAIL (1)
{
  int sock_handle;
  char errmsg[1024];
  int sockAddrSize;
  struct sockaddr_in serverAddr;
  char *address;
  int port;
  int true;

  true = 1;
  address = "127.0.0.1";
  port = 5001;

  sock_handle = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_handle == -1) {
    if (errmsg!=NULL)
      sprintf(errmsg, "socket(): %s", strerror(errno));
    return FAIL;
  }
  sockAddrSize = sizeof(struct sockaddr_in);
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons((unsigned short)port);
  if ( (serverAddr.sin_addr.s_addr = inet_addr(address)) ==
       ((unsigned long)(-1)) ) {
    struct hostent *he = gethostbyname(address);
    if (!he) {
      if (errmsg!=NULL)
        strcpy(errmsg, "cannot resolve address");
      return FAIL;
    }
    serverAddr.sin_addr.s_addr =
      (he->h_addr_list[0][0] * (256*256*256)) +
      (he->h_addr_list[0][1] * (256*256)) +
      (he->h_addr_list[0][2] * (256)) +
      (he->h_addr_list[0][3]);
  }
  if (connect(sock_handle, (struct sockaddr *)&serverAddr, 
              sockAddrSize) == -1) {
    if (errmsg!=NULL)
      sprintf(errmsg, "connect(): %s", strerror(errno));
    return FAIL;
  }
  if (ioctl(sock_handle, FIONBIO, &true) != 0) {
    if (errmsg!=NULL)
      sprintf(errmsg, "ioctl(): %s", strerror(errno));
    return FAIL;
  }
  while (1) {
    char buf[4];
    int n;
    int buf_size = 4;
    n = recv(sock_handle, buf, buf_size, 0);
    prnitf("recieved %u bytes\n", n);
  }
}
/* End of source code */

-Omid

On Mon, 10 Sep 2001, egor duda wrote:

> Hi!
> 
> Monday, 10 September, 2001 Omid Roshan-Afshar omid@acorn-networks.com wrote:
> 
> ORA> I've looked through the archive, and I found
> ORA> some discussion about using accept() on non-blocking
> ORA> sockets, but none on recv(). If someone could spot
> ORA> any stupid errors in the following code, that would
> ORA> be great.
> 
> ORA>   {
> ORA>     int true = 1;
> ORA>     if (ioctl(sock_handle, FIONBIO, &true) != 0) {
> ORA>       if (errmsg!=NULL)
> ORA>         sprintf(errmsg, "ioctl(): %s", strerror(errno));
> ORA>       return GFAPI_ERR_FAIL;
> ORA>     }
> ORA>   }
> 
> ORA> I call recv after I do this and it blocks. I *think* I'm
> ORA> doing the appropriate thing to make the socket non-blocking.
> 
> can you provide a small self-contained testcase?
> 
> it's impossible to conclude from your code snippet what kind of socket
> you are using, how exactly recv() is called, etc.
> 
> Egor.            mailto:deo@logos-m.ru ICQ 5165414 FidoNet 2:5020/496.19
> 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]