服务器代码: // tcp_server.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "winsock2.h" #include "stdio.h" #include "SockConnect.h" #pragma comment(lib,"Ws2_32") SOCKET sockSrv; SOCKET sockConn; SOCKADDR_IN addrSrv;//定义变量 SOCKADDR_IN addrClient; int len; void Closed(){ closesocket(sockConn);//关闭套接字 WSACleanup( ); } int setConnection() { //*********加载套接字库 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return 0; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return 0; } //*********//加载套接字库 sockSrv=socket(AF_INET,SOCK_STREAM,0);//创建套接字 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000);//端口号选择需在1024以上的 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//bind 绑定到本地地址和端口上 listen(sockSrv,5);//listen将套接字设置为监听模式 int len=sizeof(SOCKADDR); while(1) { sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);//accept函数,等待和接受客户端请求 CSockConnect * con = new CSockConnect(); con->setConnectSock(sockConn); } return 1; } int _tmain(int argc, _TCHAR* argv[]) { setConnection(); return 1; } 线程处理: #include "StdAfx.h" #include "./sockconnect.h" CSockConnect::CSockConnect(void) { m_socket = NULL; m_bThreadSart = FALSE; } CSockConnect::~CSockConnect(void) { } void CSockConnect::setConnectSock( SOCKET s ) { if (!s) { return; } m_socket = s; m_bThreadSart = TRUE; StartThread(); } void CSockConnect::ThreadProcMain() { while (m_bThreadSart) { if (m_socket) { char recvBuf[100]; int len=recv(m_socket,recvBuf,100,0);//接收客户端数据 if (len<=0) { continue; } printf("%s/n",recvBuf); // 接着发送客户端信息返回给客户端 // char sendBuf[100]; send(m_socket,recvBuf,strlen(recvBuf)+1,0);//向客户端发送数据 } } printf("Client connect close"); closesocket(m_socket); delete this; } 客户端的代码: #include "StdAfx.h" #include "./client.h" #include "TcpSocket.h" CTcpClient* CTcpClient::_instance = NULL; ITcpConect * GetTcpConnection() { return (ITcpConect*)CTcpClient::Instance(); } CTcpClient* CTcpClient::Instance() { if(_instance == NULL) { _instance = new CTcpClient(); } return _instance; } CTcpClient::CTcpClient(void) { } CTcpClient::~CTcpClient(void) { } int CTcpClient::open(UINT uSocketPort,const char* pszSockAddr) { m_sock=new CTcpSocket(this); if (!m_sock) { return -2; } int ret=m_sock->Create(uSocketPort,pszSockAddr); if (ret==-1) { delete m_sock; m_sock=NULL; return -3; } return 1; } int CTcpClient::close() { //if (m_sock) // { // send(m_sock,"end",strlen("end")+1,0); // closesocket(m_sock); // WSACleanup(); // } return 1; } int CTcpClient::RegistListen(ITcpListen* pConnListen) { return 1; } int CTcpClient::UnRegistListen(ITcpListen *pConnListen) { return 1; } int CTcpClient::SendData(const char* data,int len) { if (m_sock) { m_sock->SendDirect(data,len); } return 1; } void CTcpClient::ThreadProcMain() { } uint32 CTcpClient::Name2Inet(LPCSTR szAddress) { if(szAddress==NULL||strlen(szAddress)<1) { return htonl(INADDR_ANY); } BOOL name=FALSE; for (int i=0;szAddress[i];i++) { if((!isdigit(szAddress[i]))&&szAddress[i]!='.') { name=TRUE;break; } } //不是计算机名,是IP if(!name) return inet_addr(szAddress); //是计算机名 else { struct hostent*host=gethostbyname(szAddress); if(host==NULL) { return htonl(INADDR_ANY); } return *(long*)host->h_addr; } } 客户端线程处理: // TVThread.cpp: implementation of the CTVThread class. // // #include "stdafx.h" #include "TVThread.h" // // Construction/Destruction // CTVThread::CTVThread() { m_dwThreadID=0; m_hThread=NULL; m_evStop = CreateEvent(NULL,TRUE,TRUE,NULL); SetEvent(m_evStop); } CTVThread::~CTVThread() { CloseHandle(m_evStop); m_evStop = NULL; } bool CTVThread::StartThread() { //如果线程已经创建,则不需要再创建 if (!m_hThread){ //创建并启动线程函数 m_hThread = CreateThread( NULL, //the handle cannot be inherited. 0, //the default Thread Stack Size is set InitThreadProc, //线程函数 this, //线程函数的参数 0, //使线程函数创建完后立即启动 &m_dwThreadID //receives the thread identifier ); }//end if (!m_hThread... if (m_hThread) { ResetEvent(m_evStop); } return m_hThread != NULL; } void CTVThread::WaitForStop() { WaitForSingleObject(m_evStop,INFINITE); // 返回线程句柄 HANDLE hThread = (HANDLE)InterlockedExchange((LONG *)&m_hThread, 0); if (hThread) { // 等待线程终止 WaitForSingleObject(hThread, INFINITE); // 关闭线程句柄 CloseHandle(hThread); m_hThread = NULL; }// end if (hThread... } unsigned long CTVThread::ThreadProc() { ThreadProcMain(); SetEvent(m_evStop); return 0; } 测试程序: // TestTcp.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "ICTcp.h" int _tmain(int argc, _TCHAR* argv[]) { ITcpConect * tcpCon = GetTcpConnection(); tcpCon ->open(6000,"192.168.1.106"); int i = 0; while (true) { char data[1024]; sprintf(data,"client data :%d",i); ++i; tcpCon->SendData(data,strlen(data)+1); Sleep(1000); } return 0; }