UserData using Async Sockets ???

Started by
6 comments, last by CWizard 21 years, 9 months ago
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]
Advertisement
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
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.
when you get your message in your wndproc you have the associated socket in the wparam, don''t you?

May The Gzoo Be With You!
~Lord Gzoo
--Amir
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.
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??)
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]

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.

This topic is closed to new replies.

Advertisement