Archived

This topic is now archived and is closed to further replies.

CWizard

UserData using Async Sockets ???

Recommended Posts

I haven''t messed much with WinSock coding, but now I''m in deadly need to code up a simple server, like yesterday . So, I read Gaiiden''s article, Programming with Asynchronous Sockets, and found it valuable. One of the interesting things here is that with this approach you''ll get the socket with the message in WndProc, so you can handle several connections. But... what the hell to do with it? What good is simply a socket? Sure, I can accept/read/send/close it, but I have no way to identify the connection with simply the socket, and associate it with my own connection data, so I know where to read/write from/to! Of course I could search my "connection" array or linked list, untill I find a matching socket, but that would be extremly inefficient. Have I missed something obvious here, or am I just out of luck on this? Isn''t there anyway I can associate a socket with an arbitrary value, like UserData with SetWindowLong()??? Thank you for your input> [CRLF]

Share this post


Link to post
Share on other sites
Try std::map. You can associate a socket with an id, like so:

std::map <SOCKET, DWORD> SocketToIdMap;

or you can associate it with an entire struct:

struct CLIENT_INFO {
DWORD Id;
std::string Name;
// ...
};

std::map <SOCKET, CLIENT_INFO> SocketToClientMap;


---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
Well... I haven''t used that template, but as I figure it can''t work any other way than it performs a search of the entire list. I think that is unacceptable to do on every single message if you have a 1000 connections. I know how I could do this, but not in a good way I just thought when they provided this method, they would have provided a machanism so you directly can identify the connection... but, I guess not.

Share this post


Link to post
Share on other sites
Yes, I do have the Socket with me into WndProc. But that''s not going to help, as I understand it.

Lets say I have an ftp-like server. I have two connections, two sockets (or rather pairs thereof), and two file transfers in progress. When each of these transfers start I open the files and have a stream which I will read from and send through the socket. Ok, I have filled the network buffer(s) and waits for more room, and down comes a window message, handing me the socket that I can send to. How do I know which stream or file to read from? Or, that it is a file transfer going on here?

As I''ve stated; I can of course search, in one way or another, in my connection structures and find out which state that connection is in, but that is a very dirty method I think.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think the only way to do this without doing some sort of searching would be to implement your own async sockets that post a message with a pointer to the struct (that holds all the info about the socket, file, etc.) in question rather than just the socket id.
If you''d rather use the pre-done async sockets, you could keep the sockets in a sorted list and do a binary search, would at least cut down the time from a linear search considerably (and the sort insertion should be fairly free, as generally socket descriptors will be assigned in ascending order afaik??)

Share this post


Link to post
Share on other sites
Ok. Topic over. I guess async sockets (or the ones provided with WinSock) aren't for servers, but rather clients. I can't imaging a performance server which should be able to keep 10,000+ connections can use these. Well, I have dropped the type of server Gaiiden talked about in his article, using async sockets, and opted for a multi-threaded instead.

Thanks for your answers, they helped me come to terms with this!

EDIT: AP, your suggestions are good; both the home-made variant, and the ordered list. Perhaps I'll try to do something like that next time

[edited by - CWizard on July 17, 2002 12:31:51 PM]

Share this post


Link to post
Share on other sites

As an aside std::map<> uses a hashing algorithm so its considerably faster than a linear or even binary search. Alternative is to use std::hashmap<> from one of the better STLs, but I don''t think that would provide any advantage here.

Anyway yeah, doing async sockets which hook in with wndproc is a bit lame and really aimed for single threaded MFC gui developers. Windows messages can get lost if you overload the system, so its not really ideal.

Share this post


Link to post
Share on other sites