Asynchronous Windows Socket Class

The async_socket class represents a Windows Socket. It is similar CAsyncSocket MFC class, but it differs that does not use a message queue of the application. It uses overlapped operations. Therefore it can be used in the console applications.

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

  1. Derive class from async_socket.
  2. 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.
  3. Override on_read_complete function, if you will receive data.
  4. Override on_write_complete function, if you will transmit data.
  5. For listening socket you must override on_accept, which notifies that it can accept pending connection requests by calling accept function.
  6. 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.

Downloads

Download demo project - 97 Kb


Comments

  • ???

    Posted by greateagle on 07/10/2004 11:55am

    How do you process the messages?

    Reply
  • How to thread socket to have multiclient application

    Posted by Legacy on 04/08/2003 12:00am

    Originally 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
    Thanx for any help any answer any move in my way

    Reply
  • Too Many Threads

    Posted by Legacy on 01/28/2003 12:00am

    Originally 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.....

    Reply
  • on_close never invoked

    Posted by Legacy on 12/13/2002 12:00am

    Originally 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

    Reply
  • Asynchronous Windows Socket Class

    Posted by Legacy on 04/22/2002 12:00am

    Originally posted by: soniya

    how to connect more than one client ot a server.

    • good

      Posted by sdbus on 06/28/2007 02:37am

      ths

      Reply
    Reply
  • How to use ::select function

    Posted by Legacy on 02/05/2002 12:00am

    Originally 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?

    Reply
  • ERROR_UNEXP_NET_ERR from async_socket.write()

    Posted by Legacy on 11/21/2001 12:00am

    Originally 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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds