9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Fernan Bolando <fernanbolando@mailc.net>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: [9fans] ape/socket again non-blocking command succeeds but still blocks
Date: Tue, 11 Jan 2011 09:31:35 +0800	[thread overview]
Message-ID: <AANLkTinb8_RrP=_JYnBXsubq5SnvPt9TXTKNFQzcQsdE@mail.gmail.com> (raw)

Hi all

I am testing the non-block socket of ape/plan9 the code below seems to
succeed in making a non-block sockets on unix, but not in plan9/ape it
still blocks with no error from fcntl.
Is this intended?



/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>

void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n, sta, fl;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("ERROR opening socket");

     printf("NON BLOCK Succeeded\n");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("ERROR on binding");
     fl = fcntl (sockfd, F_GETFL,0);
     sta = fcntl (sockfd, F_SETFL, fl | O_NONBLOCK);           /* set
nonblock */
     if (sta == -1)
 {
                       printf(" NON_BLOCK FAILED\n");
                        return 1;
     }
     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");
     return 0;
}



             reply	other threads:[~2011-01-11  1:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-11  1:31 Fernan Bolando [this message]
2011-01-11  6:11 ` Federico G. Benavento
2011-01-11  6:53   ` Fernan Bolando
2011-01-11  7:45     ` Federico G. Benavento
2011-01-11  8:06       ` Fernan Bolando
2011-01-11  8:18         ` Fernan Bolando
2011-01-11  8:57           ` Federico G. Benavento

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTinb8_RrP=_JYnBXsubq5SnvPt9TXTKNFQzcQsdE@mail.gmail.com' \
    --to=fernanbolando@mailc.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).