Solved

Asynchronuous Socket handling

Posted on 2001-06-30
8
208 Views
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 thread...it 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

Regards

Newborn
0
Comment
Question by:newborn
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6240371
What OS, What compiler, What type of project???
0
 

Author Comment

by:newborn
ID: 6240377
OS:win95/98/NT/2000
Compiler:VC++ 6.0
An IRC Client,TCP/IP protocol,stream sockets
0
 
LVL 30

Expert Comment

by:Axter
ID: 6240380
Is the project an MFC, Win32-App, or Win32-Console?
0
 

Author Comment

by:newborn
ID: 6240382
Win32 app...as i said i am using send() and recv() API's no CSocket or ASyncSocket
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:jhance
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.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6240551
see example of using asynchronously in
  Send Mail (socket 2) "Davide Libenzi"
http://www.xmailserver.org/davide.html
0
 
LVL 14

Accepted Solution

by:
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)
                break;

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

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

    WSAEventSelect(SockFD, hConnectEvent, 0);
    WSACloseEvent(hConnectEvent);

    return (iConnectResult);

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

Author Comment

by:newborn
ID: 6262017
i have solved my prob ,www.sockets.com 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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now