资源简介

应广大朋友要求,这份代码是我博客里的文章《完成端口详解 - 手把手教你玩转网络编程系列之三》的配套代码的客户端部分

资源截图

代码片段和文件信息

#include “StdAfx.h“
#include “Client.h“
#include “MainDlg.h“

#include 

#pragma comment(lib“ws2_32.lib“)

#define RELEASE_HANDLE(x)               {if(x != NULL && x!=INVALID_HANDLE_VALUE){ CloseHandle(x);x = NULL;}}
#define RELEASE(x)                      {if(x != NULL ){delete x;x=NULL;}}

CClient::CClient(void):
m_strServerIP(DEFAULT_IP)
m_strLocalIP(DEFAULT_IP)
m_nThreads(DEFAULT_THREADS)
m_pMain(NULL)
m_nPort(DEFAULT_PORT)
m_strMessage(DEFAULT_MESSAGE)
m_phWorkerThreads(NULL)
m_hConnectionThread(NULL)
m_hShutdownEvent(NULL)

{
}

CClient::~CClient(void)
{
this->Stop();
}

//////////////////////////////////////////////////////////////////////////////////
// 建立连接的线程
DWORD WINAPI CClient::_ConnectionThread(LPVOID lpParam)
{
THREADPARAMS_CONNECTION* pParams = (THREADPARAMS_CONNECTION*) lpParam;
CClient* pClient = (CClient*)pParams->pClient;

TRACE(“_AccpetThread启动,系统监听中...\n“);

pClient->EstablishConnections();

TRACE(_T(“_ConnectionThread线程结束.\n“));

RELEASE(pParams);

return 0;
}

/////////////////////////////////////////////////////////////////////////////////
// 用于发送信息的线程
DWORD WINAPI CClient::_WorkerThread(LPVOID lpParam)
{
THREADPARAMS_WORKER *pParams = (THREADPARAMS_WORKER *)lpParam;
CClient* pClient = (CClient*) pParams->pClient;

char szTemp[MAX_BUFFER_LEN];
memset( szTemp0sizeof(szTemp) );
char szRecv[MAX_BUFFER_LEN];
memset(szRecv0MAX_BUFFER_LEN);

int nBytesSent = 0;
int nBytesRecv = 0;

//CopyMemory(szTemppParams->szBuffersizeof(pParams->szBuffer));

// 向服务器发送信息
sprintf( szTemp(“第1条信息:%s“)pParams->szBuffer );
nBytesSent = send(pParams->sock szTemp strlen(szTemp) 0);
if (SOCKET_ERROR == nBytesSent) 
{
TRACE(“错误:发送1次信息失败,错误代码:%ld\n“ WSAGetLastError());
return 1; 
}
TRACE(“向服务器发送信息成功: %s\n“ szTemp);
pClient->ShowMessage(“向服务器发送信息成功: %s“ szTemp);

Sleep( 3000 );

// 再发送一条信息
memset( szTemp0sizeof(szTemp) );
sprintf( szTemp(“第2条信息:%s“)pParams->szBuffer );
nBytesSent = send(pParams->sock szTemp strlen(szTemp) 0);
if (SOCKET_ERROR == nBytesSent) 
{
TRACE(“错误:发送第2次信息失败,错误代码:%ld\n“ WSAGetLastError());
return 1; 
}

TRACE(“向服务器发送信息成功: %s\n“ szTemp);
pClient->ShowMessage(“向服务器发送信息成功: %s“ szTemp);

Sleep( 3000 );

// 发第3条信息
memset( szTemp0sizeof(szTemp) );
sprintf( szTemp(“第3条信息:%s“)pParams->szBuffer );
nBytesSent = send(pParams->sock szTemp strlen(szTemp) 0);
if (SOCKET_ERROR == nBytesSent) 
{
TRACE(“错误:发送第3次信息失败,错误代码:%ld\n“ WSAGetLastError());
return 1; 
}

TRACE(“向服务器发送信息成功: %s\n“ szTemp);
pClient->ShowMessage(“向服务器发送信息成功: %s“ szTemp);

if( pParams->nThreadNo==pClient->m_nThreads )
{
pClient->ShowMessage(_T(“测试并发 %d 个线程完毕.“)pClient->m_nThreads);
}

return 0;
}
///////////////////////////////////////////////////////////////////////////////////
// 建立连接
bool  CClient::EstablishConnec

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     138752  2011-08-23 23:01  PiggyStressTestClient\Debug\PiggyStressTestClient.exe

     文件       8030  2011-08-26 22:33  PiggyStressTestClient\PiggyStressTestClient\Client.cpp

     文件       3438  2011-08-27 11:43  PiggyStressTestClient\PiggyStressTestClient\Client.h

     文件       5608  2011-08-27 11:43  PiggyStressTestClient\PiggyStressTestClient\MainDlg.cpp

     文件       1226  2009-10-09 00:41  PiggyStressTestClient\PiggyStressTestClient\MainDlg.h

     文件      60320  2009-10-09 17:17  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.aps

     文件       1775  2009-10-07 00:50  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.cpp

     文件        495  2009-10-09 00:18  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.h

     文件       6128  2009-10-09 17:17  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.rc

     文件       5796  2009-10-12 10:55  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.vcproj

     文件       1413  2009-10-14 10:44  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.vcproj.HAOTHINK.Hao Wang.user

     文件       1405  2009-10-07 01:10  PiggyStressTestClient\PiggyStressTestClient\PiggyStressTestClient.vcproj.MYPC.Piggy.user

     文件       2813  2009-10-07 00:50  PiggyStressTestClient\PiggyStressTestClient\ReadMe.txt

     文件      21630  2003-07-23 17:52  PiggyStressTestClient\PiggyStressTestClient\res\PiggyStressTestClient.ico

     文件        411  2009-10-07 00:52  PiggyStressTestClient\PiggyStressTestClient\res\PiggyStressTestClient.rc2

     文件        193  2009-10-10 11:34  PiggyStressTestClient\PiggyStressTestClient\res\vssver2.scc

     文件       1016  2009-10-09 00:30  PiggyStressTestClient\PiggyStressTestClient\Resource.h

     文件        152  2009-10-07 00:50  PiggyStressTestClient\PiggyStressTestClient\stdafx.cpp

     文件       1820  2011-08-27 11:43  PiggyStressTestClient\PiggyStressTestClient\stdafx.h

     文件       1030  2009-10-07 00:50  PiggyStressTestClient\PiggyStressTestClient\targetver.h

     文件        594  2009-09-27 09:25  PiggyStressTestClient\PiggyStressTestClient\XPstyle.manifest

     文件        929  2009-10-12 10:55  PiggyStressTestClient\PiggyStressTestClient.sln

     文件      32768  2009-10-15 16:02  PiggyStressTestClient\PiggyStressTestClient.suo

     目录          0  2011-08-02 00:51  PiggyStressTestClient\PiggyStressTestClient\res

     目录          0  2011-08-24 20:33  PiggyStressTestClient\Debug

     目录          0  2011-08-24 20:33  PiggyStressTestClient\PiggyStressTestClient

     目录          0  2011-08-24 20:33  PiggyStressTestClient

----------- ---------  ---------- -----  ----

               297742                    27



............此处省略0个文件信息

评论

共有 条评论