LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2005, 10:28 AM   #1
mounterriver
LQ Newbie
 
Registered: Mar 2005
Posts: 2

Rep: Reputation: 0
Question Help on socket Programming non-blocking io


I am writing a socket application program and trying to use non-block io.
Eveything works fine in the blocked mode. However, after I called the
ioctl (socketfd, FIONBIO, &on);

the following error occurs when I start the program.

accept error: Resource temporarily unavailable


I have included a short test code

int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(1300); /* daytime server */

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
unsigned long arg = (0 ? 0 : 1);
int on = 0;
//ioctl(listenfd, FIONBIO, &on);
if (fcntl(listenfd, F_SETFL, FNDELAY) < 0)
{
perror( " error in setting socket to non-blocking io mode ");
exit(5);
}
Listen(listenfd, LISTENQ);

for ( ; ; ) {
printf(" waiting for connection \n");
len = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &len);
printf("connection from %s, port %d\n",
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));

ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(connfd, buff, strlen(buff));

Close(connfd);
}

Neither ioctl nor fcntl works.

Anyone knows how to set the non-blocking io for socket ?

Thanks
 
Old 03-19-2005, 04:58 AM   #2
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 22
I would say that now that you have set up a non-blocking socket, you would want to check whether your socket is ready for reading before trying to accept() on it. Otherwise, you will get such a "resource unavailable" error.

Use something like
Code:
if (FD_ISSET(listenfd, &read_fd)) {
  len = sizeof(cliaddr);
  connfd = Accept(listenfd, (SA *) &cliaddr, &len);
And put a pause in your loop. You seem to have requested a non-blocking socket, but have a loop which looks like it wants to block waiting for a socket request. (Also, the flag to the ioctl should be 1, as opposed to 0, to set it in non-blocking mode.)
 
Old 03-20-2005, 10:44 PM   #3
mounterriver
LQ Newbie
 
Registered: Mar 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks MaxFacta

I am just wondering whether it is true that both the server and client side having the same type of socket, i.e. Both block, or both non-block...
 
Old 03-21-2005, 07:43 AM   #4
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 22
The client could be blocking or non-blocking, depending on whether you want to wait for a connect() to complete. It does not have to be the same as the server.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C socket non-blocking bob66 Programming 1 03-13-2005 10:21 AM
Socket programming maldini1010 Programming 4 02-11-2005 11:16 PM
Socket Programming Chaitanyayardi Programming 6 10-22-2004 12:10 AM
want help in socket programming valib4u *BSD 2 09-11-2003 11:17 PM
Socket Programming cxel91a Programming 4 03-19-2003 10:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration