Thread: Using a single socket for accept() as well as connect()?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    134

    Using a single socket for accept() as well as connect()?

    Hi,

    How can a single socket be used to accept() as well as to connect().? Is it possible and what is the best way to implement it? Any sample code would be helpful.

    Here is what I want to do.

    Open a socket on a port and accept() on it for a certain amount of time. If a connection is received in that time from the remote peer then send/recv with new socket descriptor. Otherwise, attempt a connection to the remote peer using the same socket and once established, start send/recv.

    I am developing on linux.

    Thanks,

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    what do you mean by attempt a connection using the same socket? You can use the same variable name but you can't create a listener socket and then turn around and try to use it as a client socket with the same socket identifier. Just create a new socket to connect, I don't understand why that would be a problem.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    yeah, creating 2 separate sockets was my way out of this problem. I thought may be someone else might have some better solution than that. I guess not. I have no choice but to go ahead with 2 separate sockets.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Why is that a problem in the first place? Sockets are very lightweight in just about any OS, and if you want 1 socket for 2 things then you are not going to be using the same socket to accept still so you can just close it and it gets freed anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. My socket attempt refuses to connect
    By kzar in forum C Programming
    Replies: 3
    Last Post: 06-01-2005, 04:15 AM
  3. socket connect returning odd
    By WaterNut in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:49 PM
  4. single Socket for listening and sending/receiving, Simultaneously?
    By Aidman in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-01-2003, 11:17 PM
  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