Exiting socket gracefully

This is a discussion on Exiting socket gracefully within the Linux Programming forums, part of the Platform Specific Boards category; I am writing a socket ... When I call accept function, it keeps on waiting for the new connection. I ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    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?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #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.

    (=

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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)

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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