single Socket for listening and sending/receiving, Simultaneously? [Archive] - C Board

PDA

View Full Version : single Socket for listening and sending/receiving, Simultaneously?


Aidman
09-15-2003, 10:02 AM
Hi,

Is it possible to use the same network Socket simultaneously, for listening (for incoming connections) and communicating with a non-connected client (using the sendto() and recvfrom() functions)? Or must I have two separate sockets, and if so must the separate sockets also be bound to separate network ports? Just for the info, I am using WinSock 2 API functions.

High thanks in advance :)

Thantos
09-15-2003, 10:36 AM
If using accept() then you'll need two sockets. And yes they can both use the same port.

Aidman
09-15-2003, 11:31 AM
Yes I know... but is it possible to use the same listning socket for communication with a non-connected client simultaneously? In a multithread app for ex:

* socket S is created.
* bind S to network address with specific port.
* Create thread T.
* set S to listen for incoming connections.
* meanwhile on thread T, socket S is sending and receiving messages.

Is the above situation possible? :confused:

Thantos
09-15-2003, 01:32 PM
I don't think so.

However I think one of socket gurus should come answer it for sure.

Hammer
09-15-2003, 04:11 PM
How would you tell the difference between incoming data and a new connection request?

SyntaxBubble
09-15-2003, 05:29 PM
I think threads would be the right solution. I made a two-person chat application not too long ago, that I haven't finished yet, and I had to use threads, to control both sending and receiving data. Try going to http://msdn.microsoft.com/library and searching for _beginthread() or _beginthreadex(). I just used a function called Server(), declared as 'void Server()', that accepted connections and listened for incoming data. For multi-connection support is jus a little more complicated, but not by means of threads. You just have to create an array of listening sockets. For example:


// These are global variables
SOCKET listening[10]; // Where ten is the maximum number of sockets that can be connected at once


I don't know if this is EXACTLY what you need, but I think it should help you.

Aidman
09-16-2003, 07:53 AM
Originally posted by Hammer
How would you tell the difference between incoming data and a new connection request? If it where possible, then the listening function would return when a connection is requested by another comp. And the incoming data would be returned from the receiving function. Although I don't know how it is handled on a low-level, it is probably up to the protocol used.


Thanks for the info SyntaxBubble, it's not really what I was looking for but still useful. May I ask if you used threads that always listened for connections or message-based notification?

_Elixia_
09-16-2003, 02:32 PM
I think this may be possible, but is strongly discouraged as you can run into all sorts of problems. It would be much better to use a completely seperate socket for listening whilst doing other i/o, and would make your code more readable & understandable.

Hunter2
09-23-2003, 11:51 AM
For multi-connection support is jus a little more complicated, but not by means of threads. You just have to create an array of listening sockets.Depending on what you mean, I may or may not disagree.


//Global variables
SOCKET listening;
(do something to bind it to a port and get it to listen)
std::vector<SOCKET> clients;

//In your thread procedure
while(!quit)
{
clients.push_back(accept(listening, ...));
}
If you mean that in order to get more than 1 client to connect you need to create more listening sockets, I disagree. One listening socket can accept connections from any number of clients < the system's max number of sockets. If you mean that to get clients connecting on more than 1 port you need more listening sockets, I agree.

rinin_farina
10-01-2003, 11:08 PM
If you mean that to get clients connecting on more than 1 port you need more listening sockets, I agree.

hello,

I'm a newbie in c++. Anyway, do you have any idea on connecting clients thru different ports?

I appreciate any help from you guys out there. thanks

Hammer
10-01-2003, 11:17 PM
Originally posted by rinin_farina
hello,

I'm a newbie in c++. Anyway, do you have any idea on connecting clients thru different ports?

I appreciate any help from you guys out there. thanks
Read through some of the tutorials mentioned in the "good links" thread.

To get a client connecting on different ports, simply create more sockets and call connect on them. Obviously a server will need to be listening on each port you call to.