无连接套接字时序:
服务器 客户机
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);
}
}
本文地址:http://com.8s8s.com/it/it391.htm