网络编程的练习3

无连接套接字时序:
    服务器                                客户机
    socket()                                 socket()   
    bind()                                    bind()
    readfrom()                            
    阻塞等待客户                    sendto()
    处理服务        
    sendto()                                readfrom()
    close()                                  close()
    

/*
*******************************************************
 2004 12 7
 stephen
*******************************************************
*/
#include <Winsock2.h>
#pragma comment(lib,"wsock32.lib")

#define MAXSIZE 50

#define WM_RECEIVEDATA WM_USER+1002

class CNetServer
{
public:
 CNetServer();
 CNetServer(int port);
 ~CNetServer();
 inline void SetWnd(HWND hWnd)
 {
  this->m_hWnd = hWnd;
 }
 //
 bool InitNetServer();

 static DWORD WINAPI ThreadServer(LPVOID n);
 bool SendMsg(char* data,int len,char* host,int port);
 char* getLastError();
private:
 WSADATA wsData;
 HWND m_hWnd;
 HANDLE m_hThread;

 SOCKET listenSocket;
 sockaddr_in servAddr;
 sockaddr_in clientAddr;
 int port;

 char szError[1024];
 char szBuff[1024];
};

CNetServer::CNetServer()
{
 m_hWnd = NULL;
 listenSocket = INVALID_SOCKET;
 if(WSAStartup(MAKEWORD(1,1),&wsData) !=0)
 {
  wsprintf(this->szError,TEXT("WSAStartup failed.Error:%d"),WSAGetLastError());
 }
 port = 9999;
 memset(szBuff,0,sizeof(szBuff));
}
CNetServer::CNetServer(int port)
{
 m_hWnd = NULL;
 listenSocket = INVALID_SOCKET;
 if(WSAStartup(MAKEWORD(1,1),&wsData) !=0)
 {
  wsprintf(this->szError,TEXT("WSAStartup failed.Error:%d"),WSAGetLastError());
 }
 this->port = port;
}
CNetServer::~CNetServer()
{
 if(listenSocket != INVALID_SOCKET)
  closesocket(listenSocket);
 WSACleanup();
}
char* CNetServer::getLastError()
{
 return this->szError;
}
bool CNetServer::InitNetServer()
{
 listenSocket = socket(AF_INET,SOCK_DGRAM,0);
 if(listenSocket == INVALID_SOCKET)
 {
  wsprintf(this->szError,TEXT("SOCKET failed.Error:%d"),WSAGetLastError());
  return false;
 }
 servAddr.sin_family = AF_INET;
 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servAddr.sin_port = htons(port);
 if(bind(listenSocket,(struct sockaddr*)&servAddr,sizeof(servAddr)) !=0)
 {
  wsprintf(this->szError,TEXT("bind failed.error:%d"),WSAGetLastError());
  return false;
 }
 m_hThread = CreateThread(NULL,0,CNetServer::ThreadServer,(LPVOID)this,0,NULL);
 return true;
}
DWORD WINAPI CNetServer::ThreadServer(LPVOID n)
{
 CNetServer* pServer = (CNetServer*)n;
 int len = sizeof(pServer->clientAddr);
 int rcv = 1;
 char buff[1024];
 memset(buff,0,sizeof(buff));
 while(rcv)
 {
  rcv = recvfrom(pServer->listenSocket,
        buff,
        sizeof(buff)-1,
        0,
        (struct sockaddr*)&pServer->clientAddr,
        (int*)&len);
  if(rcv == SOCKET_ERROR)
  {
   wsprintf(pServer->szError,TEXT("recvfrom failed.Error:%d"),WSAGetLastError());
   return -1;
  }
  if(rcv>0)
  {
   wsprintf(pServer->szBuff,TEXT("host:%s,port:%i,msg:%s"),inet_ntoa(pServer->clientAddr.sin_addr),
      ntohs(pServer->clientAddr.sin_port),buff);
   //::SendMessage(pServer->m_hWnd,WM_RECEIVEDATA,0,(LPARAM)pServer->Buff);
   MessageBox(NULL,pServer->szBuff,"getdata",0);
  }

 }
 return 0L;
}
bool CNetServer::SendMsg(char* data,int len,char* host,int port)
{
 int sent;
 hostent *hostData;
 if(atoi(host))
 {
  long ip = inet_addr(host);
  hostData = gethostbyaddr((char*)&ip,sizeof(ip),AF_INET);
 }
 else
 {
  hostData = gethostbyname(host);
 }
 if(!hostData)
 {
  wsprintf(this->szError,TEXT("Host can not be NULL"));
  return false;
 }
 sockaddr_in dest;
 dest.sin_family = AF_INET;
 dest.sin_addr = *(in_addr*)(hostData->h_addr_list[0]);
 dest.sin_port = htons(port);

 sent = sendto(this->listenSocket,data,len,0,(sockaddr*)&dest,sizeof(dest));
 if(sent !=len)
 {
  wsprintf(this->szError,TEXT("sendto msg failed.Error:%d"),WSAGetLastError());
  return false;
 }
 return true;
}
/*
************************************************************************
                test
************************************************************************
*/
int _tmain(int argc, _TCHAR* argv[])
{
 CNetServer net;
 if(net.InitNetServer() == false)
  cout<<net.getLastError()<<endl;
 char send[1024];
 char host[32];
 while(1)
 {
  cin>>send;
  if(stricmp(send,"exit") ==0)
   break;
  cin>>host;
  net.SendMsg(send,strlen(send),host,9999);
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值