Asynchronous Windows Socket Class
Posted
by Roman Ukhov
on November 28th, 2000
For usage of this class you probably should override some virtual
notification functions. Such as:
//Called when connection closed by remote host //err - WinSock error code virtual void on_close(int err); //Called when new connection request is received //err - WinSock error code virtual void on_accept(int err); //Called when socket is ready for receiving data //err - WinSock error code virtual void on_read(int err); //Called when socket is ready for sending data //err - WinSock error code virtual void on_write(int err); //Called when socket is completely created and // watching thread is started //err - WinSock error code virtual void on_attach(); //Called when overlapped read operation is done //err - Win32 error code virtual void on_read_complete(DWORD err, DWORD len); //Called when overlapped write operation is done //err - Win32 error code virtual void on_write_complete(DWORD err, DWORD len); //Called before watching thread will be destroyed virtual void on_clear(); virtual void on_timeout();
To create connected node, following tasks should be performed
- Derive class from async_socket.
- Override on_attach function, which called first when socket is created. In this function you can start read and/or write operation by calling bool read(char* pBuff, DWORD len) and/or bool write(char* pBuff, DWORD len) functions.
- Override on_read_complete function, if you will receive data.
- Override on_write_complete function, if you will transmit data.
- For listening socket you must override on_accept, which notifies that it can accept pending connection requests by calling accept function.
- Also you should call select (usually from on_attach) with appropriate flags for obtaining the notifications (if needed).
Important! Do not forget about multitasking and safety of data, all notification functions are called from watching thread.
The demo project contains implementation of the HTTP/1.1 server based on the async_socket class. There some auxiliary classes also are contained.

Comments
???
Posted by greateagle on 07/10/2004 11:55amHow do you process the messages?
ReplyHow to thread socket to have multiclient application
Posted by Legacy on 04/08/2003 12:00amOriginally posted by: Albert Smith
I am workin' on application in WINAPI which includes some of chat room and everythin' is ok when only one client is connected to the socket how can i thread socket or duplicate i read somethin' about WSADuplicateSocket but i do not know about how make it work good
ReplyThanx for any help any answer any move in my way
Too Many Threads
Posted by Legacy on 01/28/2003 12:00amOriginally posted by: MikeAThon
The class works by opening a new thread for each socket, and using the thread to call your (virtual) functions like OnRead() and OnWrite(). As a result, if you are trying to support (for example) 100 simultaneous socket connections, then you end up with 101 threads. Way too many.
Moreover, if you're going to start a thread for each socket anyway, I don't understand why you would put up with the complexities introduced by asynchronous non-blocking sockets. So long as you have one thread per socket, you might as well just use blocking sockets, which are way easier to implement.
Now, if he had written a socket manager class that managed all sockets from one thread, then that might have been something. Maybe I'll play with that architecture.....
Replyon_close never invoked
Posted by Legacy on 12/13/2002 12:00amOriginally posted by: River
I plann to write an NT Service program which makes use of socket programming. At first, MFC class CasyncSocket was used but it could not work properly in the console mode and it's lucky for me to find this async_socket class. And it really works in my service program. One ServerSock class and one ClientSock are derived from async_socket class and the ServerSock is used to listen and accept client request and the ClientSock is used to communicate . But now a bug is found: the on_close method in the ClientSock can not be invoked in the programm. I override the on_close notification function, but it still could not work.
I debugged into the socket_func and added a break point after WSAEnumNetworkEvents set at _on_close(netEvents.iErrorCode[FD_CLOSE_BIT]); I found when the client programm calls closesocket functions, the WSAEnumNetworkEvents breaks and with netEvents.lNetworkEvents equals to 0.
Anyone can help
Thanks very much
ReplyAsynchronous Windows Socket Class
Posted by Legacy on 04/22/2002 12:00amOriginally posted by: soniya
how to connect more than one client ot a server.
-
Replygood
Posted by sdbus on 06/28/2007 02:37amths
ReplyHow to use ::select function
Posted by Legacy on 02/05/2002 12:00amOriginally posted by: Water Shao
I have seen the source code of your class async_socket
You use event, but I want to use ::select how to use?
ReplyERROR_UNEXP_NET_ERR from async_socket.write()
Posted by Legacy on 11/21/2001 12:00amOriginally posted by: David Streeter
The async_socket class is exactly what I need for the project I am working on. However, after a successful create() from my client software I issue a write() and it always fails with ERROR_UNEXP_NET_ERR. Anybody got any ideas?
tia
Dave
Reply