Asynchronuous Socket handling

Posted on 2001-06-30
Last Modified: 2007-12-19
i am  working on an IRC Client...till now i am doing Synchronuous I/O...i mean for sending data i am using send() and for recieving data i am using recv() in a differnt is working all right but i want to change this to asynch stuff but cannt find any example...i want to handle send and recieve operations in a single thread Asynchronuously...any help will be greatly appreciated


Question by:newborn
  • 3
  • 2
  • 2
  • +1
LVL 30

Expert Comment

ID: 6240371
What OS, What compiler, What type of project???

Author Comment

ID: 6240377
Compiler:VC++ 6.0
An IRC Client,TCP/IP protocol,stream sockets
LVL 30

Expert Comment

ID: 6240380
Is the project an MFC, Win32-App, or Win32-Console?
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.


Author Comment

ID: 6240382
Win32 i said i am using send() and recv() API's no CSocket or ASyncSocket
LVL 32

Expert Comment

ID: 6240542
So what is your question?  If you're familiar with both blocking socket calls and with Windows message processing, you already understand this.  Rather than perform a network action when you call it the WINSOCK asynch calls queue the request and return to your app.  When the request completes (either sucessfully or with an error) your app will be sent a message about it.

I find that the best way to structure such a program is using a "state machine" to track the network operations as they progress.  This prevents having to have a separate message queue for each type of network event and therefore having to implement a bunch of global vars.
LVL 14

Expert Comment

ID: 6240551
see example of using asynchronously in
  Send Mail (socket 2) "Davide Libenzi"
LVL 14

Accepted Solution

AlexVirochovsky earned 50 total points
ID: 6261993
Next is snippet from Davide code:
int SvrConnectTimeout(SOCKET SockFD, const struct sockaddr * pSockName, int iNameLen,
                        int iTimeout)

    WSAEVENT        hConnectEvent;

    if ((hConnectEvent = WSACreateEvent()) == NULL)
        return (-1);

    WSAEventSelect(SockFD, hConnectEvent, FD_CONNECT);

    int iConnectResult = WSAConnect(SockFD, pSockName, iNameLen, NULL, NULL, NULL, NULL);

    int iConnectError = WSAGetLastError();

    if ((iConnectResult != 0) && (iConnectError == WSAEWOULDBLOCK))
        DWORD           dwWaitResult,
                        dwEntryCount = GetTickCount();

        while ((dwWaitResult = WSAWaitForMultipleEvents(1, &hConnectEvent, FALSE,
                                dwStepTimeout, TRUE)) != WSA_WAIT_EVENT_0)
            if (dwWaitResult == WSA_WAIT_FAILED)

            if ((iTimeout > 0) &&
                    ((iTimeout * 1000) < (int) (GetTickCount() - dwEntryCount)))

            if (CallIdleProc() < 0)
        iConnectResult = (dwWaitResult == WSA_WAIT_EVENT_0) ? 0 : -1;

    WSAEventSelect(SockFD, hConnectEvent, 0);

    return (iConnectResult);

Full project contens many such examples.
I hope, it helps. Alex

Author Comment

ID: 6262017
i have solved my prob , has very good samples i got my thing from wal,ur sample is also good,no need of window to recieve the call back notifications ,events can do, i will try to improve my code,many thanx for ur help

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 71
PDF library for Delphi 2 121
Compile GLUT with Visual Studio 2015 1 148
Arduino EDI - Programming language 3 94
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question