C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 10-18-2005, 07:43 PM   #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,
nkhambal is offline   Reply With Quote
Old 10-19-2005, 10:05 AM   #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.
rockytriton is offline   Reply With Quote
Old 10-19-2005, 12:23 PM   #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.
nkhambal is offline   Reply With Quote
Old 10-20-2005, 05:43 AM   #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.
orbitz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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