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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds