#include <winsock2h>
#define MAX_SIZE 200
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrSrv;
addrSrvsin_addrS_unS_addr = htonl(INADDR_ANY);
addrSrvsin_family = AF_INET;
addrSrvsin_port = htons(6000);
bind(sockSrv, (SOCKADDR )&addrSrv, sizeof(SOCKADDR));
SOCKADDR addrClient;
char recvBuffer[MAX_SIZE];
memset(recvBuffer, 0, sizeof(recvBuffer));
int len = sizeof(SOCKADDR);
recvfrom(sockSrv, recvBuffer, sizeof(recvBuffer), 0, (SOCKADDR )&addrClient, &len);
printf("%s\n", recvBuffer);
closesocket(sockSrv);
}
#include <stdioh>
#include <winsock2h>
void main(void) {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
return;
}
/ Confirm that the WinSock DLL supports 22/
/ Note that if the DLL supports versions greater /
/ than 22 in addition to 22, it will still return /
/ 22 in wVersion since that is the version we /
/ requested /
if ( LOBYTE( wsaDatawVersion ) != 1 ||
HIBYTE( wsaDatawVersion ) != 1 ) {
/ Tell the user that we could not find a usable /
/ WinSock DLL /
WSACleanup( );
return;
}
/ The WinSock DLL is acceptable Proceed /
SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);
SOCKADDR_IN addrClient;
addrClientsin_addrS_unS_addr = inet_addr("127001");
addrClientsin_family = AF_INET;
addrClientsin_port = htons(6000);
sendto(sockClient, "你若此生若只如一瞬", strlen("你若此生若只如一瞬"), 0, (SOCKADDR )&addrClient, sizeof(SOCKADDR));
closesocket(sockClient);
WSACleanup();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)