9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Federico G. Benavento" <benavento@gmail.com>
To: fernanbolando@mailc.net,
	Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] ape/socket again non-blocking command succeeds but still blocks
Date: Tue, 11 Jan 2011 04:45:12 -0300	[thread overview]
Message-ID: <AANLkTinh6ZpfKka0kL-7H0ggK5SO+SPBiL_Se4b_x2PO@mail.gmail.com> (raw)
In-Reply-To: <AANLkTimKqq5hUNNeXN5-MEOm5Oyiz01acNqUaUZOeAL0@mail.gmail.com>

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



  reply	other threads:[~2011-01-11  7:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-11  1:31 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 [this message]
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=AANLkTinh6ZpfKka0kL-7H0ggK5SO+SPBiL_Se4b_x2PO@mail.gmail.com \
    --to=benavento@gmail.com \
    --cc=9fans@9fans.net \
    --cc=fernanbolando@mailc.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).