LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2011, 06:50 AM   #1
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Rep: Reputation: Disabled
Asynchronous accept() with posix sockets.


Hi! I know it is possible to use asynchronous I/O with read/write, but is it possible to accept incoming connections when they arrive and still do other things in the meantime without using multithreading and without having to periodically check with accept function (non-blocking mode)?

Usually you do sth like this:

Code:
int main()
{
  socket(sock, ...)
  bind(sock, ...)
  listen(sock, ...)
  while (run)
  {
    accept(sock, ...0;
    spawn_thread(); //or fork()
  }
}
I'd like to do sth like this:

Code:
void func()
{
  spawn_thread();
}

int main()
{
  socket(sock, ... , ASYNC)
  bind(sock, ...)
  listen(sock, ...)
  enable_SIGIO(); //or other proper signal
  accept(sock, ..., func);
  while (run)
  {
    sleep(1);
  }
}
It doesn't necessarily have to be posix, the target is Linux 2.6

Last edited by BartekBl; 07-11-2011 at 06:57 AM. Reason: ORT
 
Old 07-11-2011, 03:43 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
If you're willing to use a select() loop, then:
  1. Open the listening socket in async mode (O_NONBLOCK).
  2. Set up the loop in the normal manner.
  3. Before going into the loop, do the listen().
  4. Each time through the loop, add the listening socket to the read fd's, but not to the write fd's.
  5. When an accept() is possible, then the select() will say that you can "read" on the listening socket.
 
Old 07-12-2011, 03:13 AM   #3
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
AFAIK I could as well use accept with non-blocking mode to poll for waiting connections, but still its non blocking and what I want is asynchronous (like receving SIGIO whenever someone tries to connect so that I can accept it). Unfortunatelly due to some hardware problems I can't test it now. Thanks for your help anyway.

EDIT: I think that I what I want can't be done marking the thread as solved.

Last edited by BartekBl; 08-16-2011 at 07:49 AM. Reason: solved
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
QTcpSocket (Client) -> POSIX Sockets (Server) Gavin Harper Programming 1 01-19-2011 08:36 PM
question about python and sockets.. as the accept line() exacutes program hangs xskycamefalling Programming 1 02-14-2010 02:44 PM
question about python and sockets.. as the accept line() exacutes program hangs xskycamefalling Programming 1 02-10-2010 06:32 PM
Qt asynchronous sockets problem greeklegend Programming 0 11-26-2008 06:52 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration