SOCKET收发数据报

WORD wVertionRequest;
WSADATA wsaData;
int err;
wVertionRequest=MAKEWORD(1,1);
err=WSAStartup(wVertionRequest,&wsaData);
if (err!=0)
{
return 0;
}
if (LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion!=1))
{
WSACleanup();
return 0;
}
SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);//端口
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100];
// char sendBuf[100];
// char tempBuf[100];
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while (1)
{
recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
}
closesocket(sockSrv);
WSACleanup();////////////////////////收



WORD wVertionRequest;
WSADATA wsaData;
int err;
wVertionRequest=MAKEWORD(1,1);
err=WSAStartup(wVertionRequest,&wsaData);
if (err!=0)
{
return 0;
}
if (LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion!=1))
{
WSACleanup();
return 0;
}
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
while (1)
{
addrSrv.sin_addr.S_un.S_addr=inet_addr("192.168.1.15");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
sendto(sockClient,"vonline",strlen("vonline")+1,0,(SOCKADDR*)&addrSrv,len);
}
closesocket(sockClient);
WSACleanup();
return 0;/////////////////发

你可能感兴趣的:(socket)