Click here to Skip to main content
15,887,214 members
Articles / Desktop Programming / MFC
Article

Multithreaded Non-blocking Socket Server and Client, Based on Synchronized Socket

Rate me:
Please Sign up or sign in to vote.
3.75/5 (4 votes)
11 Mar 2002 97.9K   3.9K   33   6
Non-blocking socket class using synchronized socket.

Introduction

Client

Just derive a child CTestClient class from CSyncSocketWorker and override its virtual function ProcessData.

void CTestClient::ProcessData()
{
     const int nBufferSize = 8192;
    char szBuffer[nBufferSize];

    CFile file;
    file.Open(TEXT("recv.dat"), CFile::modeCreate | CFile::modeWrite);
    int nRead;

    DWORD dwStart = GetTickCount();
    while(nRead = Read(szBuffer, nBufferSize))
    {
        file.Write(szBuffer, nRead);
        if(IsShutingdown())
        {
            cout << "shuting down" << endl;
            break;
        }
    }

    cout << "Speed = " << double(file.GetLength()) / 
                  (double(GetTickCount() - dwStart) / 1000.0) / 1024 
         << "KB/s" << endl;
    file.Close();
}

Server

Same as client.

void CEchoWorker::ProcessData()
{
     const int nBufferSize = 4096;
    
    char* pBuffer = new char[nBufferSize];
    if(!pBuffer)
        return;


    CFile file;
    file.Open(TEXT("testfile.dat"), CFile::modeRead);
    
    while(!IsShutingdown() && IsConnected())
    {
        int nRead = file.Read(pBuffer, nBufferSize);
        if(nRead == 0)
            break;

        Write(pBuffer, nRead, FALSE);
    }

    SendRightNow();

    while(!IsSendBufferEmpty() && IsConnected())
        Sleep(500);

    delete [] pBuffer;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralClient data in the Server code Pin
John A. Rose19-Jun-07 18:32
John A. Rose19-Jun-07 18:32 
Questioncannot reach author. license ? Pin
pshomov24-Nov-03 2:52
pshomov24-Nov-03 2:52 
QuestionUDP? Pin
Amit Dey16-Nov-02 10:51
Amit Dey16-Nov-02 10:51 
GeneralBad links have been corrected Pin
Kevin Hua12-Mar-02 15:20
Kevin Hua12-Mar-02 15:20 
GeneralSorry for broken links Pin
Kevin Hua12-Mar-02 15:00
Kevin Hua12-Mar-02 15:00 
Hi, all
I am badly SORRY for the broken links that brought inconvenience to you.

This article is my first one posted on the CodeProject site. I Remember uploading the two Zip files, but maybe some critical steps was skipped, that caused broken links.

I would like to send the two Zip file to you, please send me a mail.

Thank you for reading my code and your comment is also expected.

Kevin
dvhua@263.net
kevin-hua@woncore.com
Generalcan't download! Pin
11-Mar-02 19:40
suss11-Mar-02 19:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.