完整代码如下
类声明
- #ifndef CTCPMAP_H
- #define CTCPMAP_H
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- class client
- {
- public:
- client();
- ~client();
- //初始化网络
- bool InitnewWork();
- //销毁网络
- void DeleteWork();
- //发送数据
- bool sendDate(char*szbuf,int nLen);
- //接收数据
- void recvDate();
- //线程函数
- static DWORD WINAPI ThreadWork(LPVOID lpvoid);
- private:
- SOCKET m_client;
- HANDLE m_hThread;
- bool bFlagQuit;
- };
-
- #endif // CLIENT_H
类定义
- #include "client.h"
-
- client::client()
- {
- m_client=0;
- m_hThread=NULL;
- bFlagQuit=true;
-
- }
-
- client::~client()
- {
-
- }
-
- bool client::InitnewWork()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
-
- wVersionRequested = MAKEWORD(2, 2);
-
- err = WSAStartup(wVersionRequested, &wsaData);
- if (err != 0) {
-
- printf("WSAStartup failed with error: %d\n", err);
- return false;
- }
-
-
-
- if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
-
- DeleteWork();
- return false;
- }
- else
- printf("The Winsock 2.2 dll was found okay\n");
-
-
- //创建套接字
- m_client=socket(AF_INET,SOCK_STREAM,0);
- if(m_client==INVALID_SOCKET)
- {
- DeleteWork();
- return false;
- }
-
-
-
- //connect()
-
-
- //绑定IP
- sockaddr_in addrserver;
- addrserver.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
- addrserver.sin_port=htons(8899);
- addrserver.sin_family=AF_INET;
- if(SOCKET_ERROR==connect(m_client,(sockaddr*)&addrserver,sizeof(addrserver)))
- {
- DeleteWork();
- return false;
-
- }
- //创建线程
- m_hThread=CreateThread(0,0,&ThreadWork,this,0,0);
- return true;
-
-
-
- }
-
- DWORD WINAPI client::ThreadWork(LPVOID lpvoid)
- {
- client* pthis=(client* ) lpvoid;
-
-
-
-
- while(pthis->bFlagQuit)
- {
-
-
- }
- return 0;
- }
- void client::DeleteWork()
- {
-
- bFlagQuit=false;
- if(m_hThread)
- {
- if(WAIT_TIMEOUT==WaitForSingleObject(m_hThread,100))
- TerminateThread(m_hThread,-1);
-
- CloseHandle(m_hThread);
-
- m_hThread=NULL;
-
- }
-
-
- if(m_client)
- {
- closesocket(m_client);
- m_client=0;
-
- }
-
- WSACleanup();
-
- }
- bool client::sendDate(char*szbuf,int nLen)
- {
-
- if(!szbuf||nLen<=0)
- return false;
- if(send(m_client,(char*)&nLen,sizeof(int),0)<=0)
- return false;
- if(send(m_client,szbuf,nLen,0)<=0)
- return false;
-
-
- return true;
-
- }
- void client::recvDate()
- {
- int nRecvNum;
- int nPackNum;
- char *szbuf;
- int noffest;
- while(bFlagQuit)
- {
- nRecvNum=recv(m_client,(char*)&nPackNum,sizeof(int),0);
- if(nRecvNum<=0)
- {
- DeleteWork();
- break;
- }
- if(nPackNum<=0) continue;
- szbuf=new char[nPackNum];
- noffest=0;
- while(nPackNum)
- {
- nRecvNum=recv(m_client,szbuf+noffest,nPackNum,0);
- if(nRecvNum>0)
- {
- nPackNum-=nRecvNum;
- noffest+=nRecvNum;
- }
- }
-
- }
-
-
-
- }
主函数的测试
- #include
- #include
- #include
- #include"client.h"
- using namespace std;
- int main(int argc, char *argv[])
- {
- client cli;
- if(cli.InitnewWork())
- {
- cout<<"connect sucess"<
- char szbuf[1024];
- cin>>szbuf;
- cli.sendDate(szbuf,sizeof (szbuf));
- }
-
-
- }