[SLUG] Non-blocking sockets

2004-05-27 Thread Brad Douglas
Hi All,

I'm having some trouble trying to set a TCP socket to non-blocking (code example 
below).  Basically fnctl is returning -1 but when I try and get the errno, it's set to 
0 (which I beleive is success).

I've done a quick walk through with another hacker here but we cant' see anything 
obvious.

Any ideas on what I've balls up is greatly appreciated.

Regards,
Brad

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
int fileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if ( 0 > fileDescriptor )
printf("Unable to obtain new socket (errn = %d, text = \"%s\"\n",
errno, strerror(errno));

int currentFlags = fcntl(fileDescriptor, F_GETFL, 0);
if ( 0 > currentFlags )
printf("Unable to get current file descriptor flags\n");

int returnCode = fcntl(fileDescriptor, F_SETFL, currentFlags | O_NONBLOCK);
if ( -1 == returnCode );
printf("Unable to set file descriptor flags (errno = %d, text = \"%s\")\n",
errno, strerror(errno));

return 0;
}
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Non-blocking sockets

2004-05-27 Thread Erik de Castro Lopo
On Thu, 27 May 2004 18:28:09 +1000
"Brad Douglas" <[EMAIL PROTECTED]> wrote:

> Hi All,
> 
> I'm having some trouble trying to set a TCP socket to non-blocking 
> (code example below). 

Is that really what you want? Maybe using poll() or select() would
be a better option.

> Basically fnctl is returning -1 but when I 
> try and get the errno, it's set to 0 (which I beleive is success).

I'm not sure fcntl() works on sockets, I thought you had to use an ioctl().
This link:

 http://www.developerweb.net/sock-faq/detail.php?id=63

suggests that either works.

> I've done a quick walk through with another hacker here but we cant' see anything 
> obvious.
> 
> Any ideas on what I've balls up is greatly appreciated.
> 
> Regards,
> Brad
> 
> #include 
> #include 
> #include 
> #include 
> #include 
> #include 
> #include 
> 
> int main()
> {
> int fileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
> if ( 0 > fileDescriptor )
> printf("Unable to obtain new socket (errn = %d, text = \"%s\"\n",
> errno, strerror(errno));

Errrm, all you have is a socket, its not connected to anything. Maybe you
could connect() or accept() on the socket before you set it to non-blocking.

Erik
-- 
+---+
  Erik de Castro Lopo  [EMAIL PROTECTED] (Yes it's valid)
+---+
A good debugger is no substitue for a good test suite.
-- 
-- 
+---+
  Erik de Castro Lopo  [EMAIL PROTECTED] (Yes it's valid)
+---+
A good debugger is no substitue for a good test suite.
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Non-blocking sockets

2004-05-27 Thread John Clarke
On Thu, May 27, 2004 at 07:25:10PM +1000, Erik de Castro Lopo wrote:

> I'm not sure fcntl() works on sockets, I thought you had to use an ioctl().

fcntl() does work on sockets.  I've used it.

> > int fileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
> > if ( 0 > fileDescriptor )
> > printf("Unable to obtain new socket (errn = %d, text = \"%s\"\n",
> > errno, strerror(errno));
> 
> Errrm, all you have is a socket, its not connected to anything. Maybe you
> could connect() or accept() on the socket before you set it to non-blocking.

I was going to wait until I got home before I answered the original
post (my Unix Network Programming books are at home), but I think
you've found the answer.  From fcntl(2):

EBADFfd is not an open file descriptor, or the command
 was F_SETLK or F_SETLKW and the  file  descriptor
 open  mode  doesn't  match  with the type of lock
 requested.


> -- 
> +---+
>   Erik de Castro Lopo  [EMAIL PROTECTED] (Yes it's valid)
> +---+
> A good debugger is no substitue for a good test suite.
> -- 
> -- 
> +---+
>   Erik de Castro Lopo  [EMAIL PROTECTED] (Yes it's valid)
> +---+
> A good debugger is no substitue for a good test suite.


CChhrrss,,

JJoohhnn
-- 
Trying to do *anything* industrial-strength on a NT platform is doomed to 
failure - it's like the realisation that you've been allocated a bunk-space 
below a confirmed bedwetter. The question is not whether you are going to 
get pissed on, but when.-- Tanuki
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html