C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2009, 10:38 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 10
Exiting socket gracefully

I am writing a socket ...

When I call accept function, it keeps on waiting for the new connection.

I want to know, can I call accept function in some manner so that it doesn't completely hang the execution. I don't want to use Ctrl+C to exit the server everytime.

I know threading is an option, but some other better option?
vkaushal21 is offline   Reply With Quote
Old 09-07-2009, 10:53 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Use non-block.
Quote:
Originally Posted by GNU C Manual
The accept function waits if there are no connections pending, unless the socket socket has nonblocking mode set. (You can use select to wait for a pending connection, with a nonblocking socket.)
You set non-block like this:
Code:
#include <fcntl.h>
fcntl(socket_fd,F_SETFL,O_NONBLOCK);
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-09-2009, 03:20 AM   #3
Registered User
 
Join Date: Nov 2007
Posts: 11
Quote:
Originally Posted by vkaushal21 View Post
I am writing a socket ...

When I call accept function, it keeps on waiting for the new connection.

I want to know, can I call accept function in some manner so that it doesn't completely hang the execution. I don't want to use Ctrl+C to exit the server everytime.

I know threading is an option, but some other better option?
Google for "non-blocking socket". You should find a lot of examples about it. I had the same doubt.

(=
Tesctassa is offline   Reply With Quote
Old 09-09-2009, 11:06 AM   #4
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
Try using select() on it. Configure an internal pipe that you can use to break out of the select wait state (if you need to break out for some reason --- like your main tread is exiting)
Kennedy is offline   Reply With Quote
Reply

Tags
socket accept

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call from another .c module Ali.B C Programming 14 08-03-2009 11:45 AM
Problem with socket descriptors McKracken C Programming 1 07-22-2009 08:51 AM
socket programming question, closing sockets... ursula Networking/Device Communication 2 05-31-2009 05:17 PM
Socket Help - Multiple Clients project95talon C Programming 5 11-17-2005 02:51 AM
socket newbie, losing a few chars from server to client registering Linux Programming 2 06-07-2003 11:48 AM


All times are GMT -6. The time now is 12:18 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22