网络编程的练习1

类别:编程语言 点击:0 评论:0 推荐:
 

这里写的是一个客户端的类,调用InitNetClient成功后连接服务器,接收的数据通过消息返回窗口.其它功能以后再加上来.
/*
**************************************************************
 2004 - 11 - 30 参照于MSDN
 stephen
**************************************************************
*/

#include <Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")

#define WM_READDATA WM_USER+1001

class CNetClient
{
public:
 
 CNetClient(char* hostname,int iport);
 ~CNetClient();
 inline void SethWnd(HWND hwnd)
 {
  m_hWnd = hwnd;
 }
 bool InitNetClient();
 bool ConnectToServer();
 bool SendDataToServer(char* data,int len);
 char* GetLastError();
 static DWORD WINAPI ThreadToRead(LPVOID n);

private:
 char hostName[256];
 int port;
 char szError[256];
 char getData[256];
 int nReceive;

 SOCKET serverSocket;
 SOCKADDR_IN destination;
 PHOSTENT pHostent;

 WSADATA wsData;

 HANDLE hreadthread;
 HWND m_hWnd;
};

CNetClient::CNetClient(char* hostname,int iport)
{
 serverSocket = INVALID_SOCKET;
 pHostent   = NULL;
 strcpy(hostName,hostname);
 port = iport;
 hreadthread = NULL;
 nReceive = 0;
}
// 关闭网络
CNetClient::~CNetClient()
{
 DWORD exitCode=0;
 if(hreadthread != NULL)
 {
  GetExitCodeThread(hreadthread,&exitCode);
  ExitThread(exitCode);
 }
 if(serverSocket !=INVALID_SOCKET)
  closesocket(serverSocket);
 WSACleanup ();

}
// 初始化客户端
bool CNetClient::InitNetClient()
{
 if(WSAStartup(MAKEWORD(1,1),&this->wsData) != 0)
 {
  wsprintf(this->szError,TEXT("WSAStartup failed.Error:%d"),WSAGetLastError());
  return false;
 }
 if((serverSocket = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
 {
  wsprintf(this->szError,TEXT("socket failed.Error:%d"),WSAGetLastError ());
  return false;
 }
 destination.sin_family = AF_INET;

 if((pHostent= gethostbyname(hostName)) == NULL)
 {
  wsprintf(this->szError,TEXT("gethostbyname failed.Error:%d"),WSAGetLastError ());
  closesocket(serverSocket);
  return false;
 }

 memcpy((char FAR*)&(destination.sin_addr),pHostent->h_addr,pHostent->h_length);
 destination.sin_port = htons(port);


 return true;
}
// 连接服务器
bool CNetClient::ConnectToServer()
{
 if(serverSocket == INVALID_SOCKET)
  return false;
 if(connect(serverSocket,
    (PSOCKADDR)&destination,
    sizeof(destination))
    == SOCKET_ERROR)
 {
  wsprintf(this->szError,TEXT("connect failed.Error:%d"),WSAGetLastError ());
  closesocket(serverSocket);
  return false;
 }
 hreadthread = CreateThread(NULL,0,CNetClient::ThreadToRead,
        this,0,NULL);
 return true;
}
// 发送数据
bool CNetClient::SendDataToServer(char* data,int len)
{
 if(serverSocket == INVALID_SOCKET)
  return false;
 if(send(serverSocket,data,len,0) == SOCKET_ERROR)
 {
  wsprintf(this->szError,TEXT("send failed.Error:%d"),WSAGetLastError ());
  closesocket(serverSocket);
  return false;
 }
 return true;
}
// 返回错误信息
char* CNetClient::GetLastError()
{
 return this->szError;
}
// 读取数据的线程
DWORD WINAPI CNetClient::ThreadToRead(LPVOID n)
{
 CNetClient* pnetClient = (CNetClient*)n;
 while(1)
 {
  pnetClient->nReceive = recv(pnetClient->serverSocket,pnetClient->getData,sizeof(pnetClient->getData),0);
  if( pnetClient->nReceive == SOCKET_ERROR)
  {
   wsprintf(pnetClient->szError,TEXT("recv failed.Error:%d"),WSAGetLastError ());
   closesocket(pnetClient->serverSocket);
   break;
  }
  else if( pnetClient->nReceive == 0)
  {
   wsprintf(pnetClient->szError,TEXT("Finished receiving data"));
   closesocket(pnetClient->serverSocket);
   break;
  }
  else
  {
// 测试用屏蔽了sendmessage
//   ::SendMessage(pnetClient->m_hWnd,WM_READDATA,0,(LPARAM)pnetClient->getData);
   MessageBox(NULL,(LPCTSTR)pnetClient->getData,"getdata",0);
  }
 }
 return 0L;
}
/*
***************************************************************************************
            test

***************************************************************************************
*/
int _tmain(int argc, _TCHAR* argv[])
{
 cout<<"-------------socket------------------"<<endl;
 char send[] = "user";
 CNetClient netClient("ftp.27h.com",80);
 if(netClient.InitNetClient() == false)
  cout<<netClient.GetLastError()<<endl;
 if(netClient.ConnectToServer() == false)
  cout<<netClient.GetLastError()<<endl;
 if(netClient.SendDataToServer(send,sizeof(send))== false)
  cout<<netClient.GetLastError()<<endl;
 else
  cout<<" Send Data to Server Successfuly"<<endl;
}


本文地址:http://com.8s8s.com/it/it24642.htm