isn't this the intended behavior? lotte% 8.out 777 NON BLOCK Succeeded Here is the message: hola
from a different window: lotte% telnet tcp!localhost!777 connected to tcp!localhost!777 on /net/tcp/20 hola I got your messagelotte% On Mon, Jan 10, 2011 at 10:31 PM, Fernan Bolando <fernanbola...@mailc.net> wrote: > 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; > } > > -- Federico G. Benavento