Click here to Skip to main content
Click here to Skip to main content

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

By , 11 Mar 2002
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralClient data in the Server codememberJohn A. Rose19 Jun '07 - 18:32 
Maybe I'm missing something...
 
where can I access the data that the client sent while inside the server side code?
 
I'm trying to see if it's in scope inside the CEchoWorker code...
 

Thanks a bunch.
 
-John
Questioncannot reach author. license ?memberPetar K. Shomov24 Nov '03 - 2:52 
I tried to reach the author of the code but both e-mail addresses are unreachable(user does not exist).
 
Anyways, I wanted to ask what is the license for the code. Can anyone use it in non-commercial products ? What about commercial products ?
QuestionUDP?memberAmit Dey16 Nov '02 - 10:51 
Can this be used for a UDP client/server?
 


'My capacity for happiness', he added, 'you could fit into a matchbox without taking out the matches first'.

- Marvin, the robot.

Amit Dey

sonork:
100:18407
msn: visualcdev


GeneralBad links have been correctedmemberKevin Hua12 Mar '02 - 15:20 
sorry for inconvenies to you all
GeneralSorry for broken linksmemberKevin 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!memberAnonymous11 Mar '02 - 19:40 
hgjhj

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

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