9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] ape/socket again non-blocking command succeeds but still blocks
@ 2011-01-11  1:31 Fernan Bolando
  2011-01-11  6:11 ` Federico G. Benavento
  0 siblings, 1 reply; 7+ messages in thread
From: Fernan Bolando @ 2011-01-11  1:31 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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;
}



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-01-11  8:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-11  1:31 [9fans] ape/socket again non-blocking command succeeds but still blocks Fernan Bolando
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

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).