LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 04-20-2005, 05:45 PM   #1
thedevilsjester
Member
 
Registered: Apr 2005
Posts: 39

Rep: Reputation: 16
fork()'ed or multi threaded socket I/O?


I have some fairly decent socket code written, however I have run into a few problems.

First, using some telnet clients the "accept()" function will not return until they press a key.

Second, while one user is trying to connect (a proccess that can take upto 5 seconds sometimes), the rest of the app is 'on hold' because its a single threaded app.

My code is structured as follows

I have a function that sets up the Listening Port and I have a function that checks the fd(s) using select() and FD_ISSET().


Using google the best answers I could find where using a command fork(), but I couldnt see how that could fix my problem, still being a single thread it would still 'pause' during accept. I also couldnt find any examples of how I could use fork() with my current structure.

The only other option I can see is a multi threaded approch, however I have never written a multi threaded app in C++, and never one using sockets and I dont relish the thought of all of the precautions I will have to take with such an endevor.

Anyone experianced in this field have any ideas?

(Note I do not want to use any 3rd party socket libs, I want to use the basics. I am using Linux and GCC)
 
Old 04-21-2005, 05:00 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Using fork() will make your project multi-processed, so you probably don't want to do that.

I wrote something that uses sockets and select for a single-processed multi-connection server a while ago. I don't remember it having these problems, although I suspect it has some other bugs in the higher-level TELNET stuff. You might want to take a look at it:

http://www.rjlee.dyndns.org/projects/programs/telnet/
 
Old 04-21-2005, 05:07 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I'm wondering how is the code written that it stops. I guess you're using blocking functions. Make your socket non-blocking (option O_NONBLOCK, read man fcntl for more info). Then accept will return when there's no new connection. BTW If I remember correctly, you can add the listening socket to select read set and then, if you get that there's something new, use accept (it shouldn't block then).

fork() is not the best idea - your connections probably need to share data and program with fork() will be usually slower than with threads. You use threads nearly the same way as processes (they are ones in fact). Only function names are different.

Probably too much theory... Please post your code - without the unneded details. Only sockect, bind etc, the loop (?) you wait for accept, how your select looks like, where's the code that should be run for the client (function? piece of code? how much data need to be shared?)
 
Old 04-21-2005, 06:59 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Assuming you don't want threads or multiple processes, it sounds like you're missing select(). Actually, you can choose between select() and poll(), but it seems like most recent code uses select() (?)

My system has a select_tut man page. There's also google..

The idea is that your loop blocks inside of select(), and is woken up when *any* connection has data ready, or you can accept() on the master socket.

With select(), you basically don't need nonblocking IO. As long as you only read, write, or accept on an fd which select() has declared ready for the operation, you won't block. Actually there is a weird case where you can block caused by broken TCP checksums, so you technically should sed the filedescriptors nonblocking anyway.

As long as you don't operate on port 23, telnet clients should turn off all their extra junk, so you'll just get raw data as you expect.
 
Old 04-22-2005, 09:25 PM   #5
thedevilsjester
Member
 
Registered: Apr 2005
Posts: 39

Original Poster
Rep: Reputation: 16
If you read my post you will see that I mention that I do use select to poll if there are any new connections. Once a connection is in que then I call accept, which (when connecting via localhost) works instantly, however any 'outside' connection can take a few seconds to return.

Is there any linux native way to create a socket with callbacks?

So fork() does make it multiproccessed? Hmm, I need to research this and see if I can just use fork for the 'select&accept' function while the rest remains on the parent proccess...hmm


Here is the current function (stripped for easier reading)

Code:
/*fd's are setup before this line*/ 
char *dataBuffer;
dataBuffer = new char[BUFFERSIZE];
	
if ( select(FD_SETSIZE, &socket_read, NULL, NULL, &tv) > 0) 
{	
	if (FD_ISSET(listening_socket, &socket_read))
	{
		sin_size = sizeof(struct sockaddr_in); 			
		int new_connection = accept(listening_socket, (struct sockaddr *)&their_addr, &sin_size);					
	}	
}
Everything works properly, it only goes to the accept() function if someone is trying to connect (I have verified this countless times), however it will stay on accept() for a period of 1-5 seconds usually. I can even read the ip address of the one trying to connect before the accept() call. This delay wouldnt be a major issue, however since its single threaded the entire app is put on hold for that duration.

If I could have accept() somehow call a function when the connection is made so my app can continue on with its proccessing while accept() is doing its work then that would be perfect...

Last edited by thedevilsjester; 04-22-2005 at 09:40 PM.
 
Old 09-24-2008, 12:41 AM   #6
arun.tayal
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Rep: Reputation: 0
Help required with system tray application

Hi All,

I am also facing the same issue. I am developing a system tray application ,Which will listen to a port for any incoming connection.

Also there is a menu attached with this system tray application, through which user can perform operations.

I have forked a process which will listen to the socket for connection and then perform operations and in the parent thread I am handling the GUI of system tray by calling gtk_main().

What is happening is at the accept() function the code is getting blocked and now even the parent process is not able to function because of this the menu of system tray hangs.

Plz help.


Regards
Arun Tayal
 
  


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
GTK multi-threaded application akhil1 Programming 2 01-20-2009 03:59 AM
multi-threaded sliding window mjl3434 Programming 2 10-24-2005 04:16 PM
pthreads - multi-threaded server elitecodex Programming 1 08-20-2005 03:58 PM
how to debug multi threaded application sibtay Programming 3 10-15-2004 12:02 PM
Multi-Threaded C pragti Programming 1 06-01-2004 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 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