Click here to Skip to main content
Licence 
First Posted 11 Mar 2002
Views 80,125
Bookmarked 27 times

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

By | 11 Mar 2002 | Article
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

About the Author

Kevin Hua

Web Developer

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralClient data in the Server code PinmemberJohn A. Rose18:32 19 Jun '07  
Questioncannot reach author. license ? PinmemberPetar K. Shomov2:52 24 Nov '03  
QuestionUDP? PinmemberAmit Dey10:51 16 Nov '02  
GeneralBad links have been corrected PinmemberKevin Hua15:20 12 Mar '02  
GeneralSorry for broken links PinmemberKevin Hua15:00 12 Mar '02  
Generalcan't download! PinmemberAnonymous19:40 11 Mar '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Mar 2002
Article Copyright 2002 by Kevin Hua
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid