Advertisement

Are Sockets Unique.

Started by May 14, 2003 05:58 PM
3 comments, last by alchemar 21 years, 8 months ago
I know this is probably a stupid Question. but When I do an ACCEPT in winsock is the socket always going to be unique? Example if 50 people log onto my server will they get 50 different socket number''s? I am tring to use a std::list to hold connections and I want to give each connection an Id that I can look for in the list. if I know that the socket will be unique then I can use that.
Typically, you will have one socket for listening for incoming connections and then each time someone connect, you create a new thread and create a new socket in that thread.

So each client connected to the server has one socket yes.
Advertisement
I am kinda new to socket programming but I''m not using Threads, I am using non-blocking sockets. but SOCKET is just a INT basicly I am wondering if I get a unique int every time a call ACCEPT
> I am tring to use a std::list to
> hold connections

Sockets are like file descriptors (or HANDLE in Win32 parlance). They are unique for the duration of their use only; _BUT_ the OS can decide to recycle the ones you have closed based on available resources. So make sure you remove the socket from the list when you close it.

> if I know that the socket will
> be unique then I can use that

You might want to be careful about using the socket as a key in any sort of internal map because the remote connection may close at any time and the socket be reused before you have the chance to discover the socket connection has been disconnected. You can always check if a newly accept()''d socket ID is already in the list before adding it, but it would be better to design around the uniqueness assumption.

-cb
Thank you, that answered my question. I''ll do something else.

This topic is closed to new replies.

Advertisement