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

* Re: [9fans] ape/socket again non-blocking command succeeds but still blocks
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Federico G. Benavento @ 2011-01-11  6:11 UTC (permalink / raw)
  To: fernanbolando, Fans of the OS Plan 9 from Bell Labs

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
<fernanbolando@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



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

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

I need to apologize, my sample code made things more confusing.

In unix the code I posted would have raised an error because
newsockfs=accept()...would not block and just pass through. The idea
is you can loop through accept() and multiplex multiple inputs.

but in plan9/ape it works just as how you shown regardless of the
fcntl non-block command. so I will not be able to loop through several
sockets because it would block.

On Tue, Jan 11, 2011 at 2:11 PM, Federico G. Benavento
<benavento@gmail.com> wrote:
> 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
> <fernanbolando@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
>



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

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

it's not fcnlt's fault, ape replaces your sockfd with a pipe
when you do listen(), you could call fcntl again after the
listen() call...

all this is usually combined with select() which in turns does
more magic behind the scenes and this behavior isn't
exposed.

On Tue, Jan 11, 2011 at 3:53 AM, Fernan Bolando <fernanbolando@mailc.net> wrote:
> I need to apologize, my sample code made things more confusing.
>
> In unix the code I posted would have raised an error because
> newsockfs=accept()...would not block and just pass through. The idea
> is you can loop through accept() and multiplex multiple inputs.
>
> but in plan9/ape it works just as how you shown regardless of the
> fcntl non-block command. so I will not be able to loop through several
> sockets because it would block.
>
> On Tue, Jan 11, 2011 at 2:11 PM, Federico G. Benavento
> <benavento@gmail.com> wrote:
>> 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
>> <fernanbolando@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
>>
>
>



-- 
Federico G. Benavento



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

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

On Tue, Jan 11, 2011 at 3:45 PM, Federico G. Benavento
<benavento@gmail.com> wrote:
> it's not fcnlt's fault, ape replaces your sockfd with a pipe
> when you do listen(), you could call fcntl again after the
> listen() call...
>
> all this is usually combined with select() which in turns does
> more magic behind the scenes and this behavior isn't
> exposed.
>

I understand, I am now using select() when compiled under plan9/ape,
it looks like it's working now. I originally found select() when I was
googling this problem, but I wasn't sure if adding select() will hide
an incompatibility issue between plan9/ape and some standard.

thanks for your help.



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

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

On Tue, Jan 11, 2011 at 4:06 PM, Fernan Bolando <fernanbolando@mailc.net> wrote:
> On Tue, Jan 11, 2011 at 3:45 PM, Federico G. Benavento
> <benavento@gmail.com> wrote:
>> it's not fcnlt's fault, ape replaces your sockfd with a pipe
>> when you do listen(), you could call fcntl again after the
>> listen() call...
>>
>> all this is usually combined with select() which in turns does
>> more magic behind the scenes and this behavior isn't
>> exposed.
>>
>
> I understand, I am now using select() when compiled under plan9/ape,
> it looks like it's working now. I originally found select() when I was
> googling this problem, but I wasn't sure if adding select() will hide
> an incompatibility issue between plan9/ape and some standard.
>
> thanks for your help.
>

On a second thought shouldn't fnctl raise an error that it was not
able to set non-block?



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

* Re: [9fans] ape/socket again non-blocking command succeeds but still blocks
  2011-01-11  8:18         ` Fernan Bolando
@ 2011-01-11  8:57           ` Federico G. Benavento
  0 siblings, 0 replies; 7+ messages in thread
From: Federico G. Benavento @ 2011-01-11  8:57 UTC (permalink / raw)
  To: fernanbolando; +Cc: Fans of the OS Plan 9 from Bell Labs

fcntl() didn't fail, it has to do with listen()'s implementation
which creates a pipe, dups and forks... but the new fd
doesn't have O_NONBLOCK set.

check /sys/src/ape/lib/bsd/listen.c

and yes it's an incompatibility

On Tue, Jan 11, 2011 at 5:18 AM, Fernan Bolando <fernanbolando@mailc.net> wrote:
> On Tue, Jan 11, 2011 at 4:06 PM, Fernan Bolando <fernanbolando@mailc.net> wrote:
>> On Tue, Jan 11, 2011 at 3:45 PM, Federico G. Benavento
>> <benavento@gmail.com> wrote:
>>> it's not fcnlt's fault, ape replaces your sockfd with a pipe
>>> when you do listen(), you could call fcntl again after the
>>> listen() call...
>>>
>>> all this is usually combined with select() which in turns does
>>> more magic behind the scenes and this behavior isn't
>>> exposed.
>>>
>>
>> I understand, I am now using select() when compiled under plan9/ape,
>> it looks like it's working now. I originally found select() when I was
>> googling this problem, but I wasn't sure if adding select() will hide
>> an incompatibility issue between plan9/ape and some standard.
>>
>> thanks for your help.
>>
>
> On a second thought shouldn't fnctl raise an error that it was not
> able to set non-block?
>



--
Federico G. Benavento



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