// UDPSender.cpp // A simple example for sending UDP packets #include #include #define PORTNUM 50000 /* random port number, we need something */ int main(int argc, char* argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return 1; } struct sockaddr_in sa; struct hostent *hp; SOCKET s; hp = gethostbyname("localhost"); if (hp == NULL) /* we don't know who this host is */ return INVALID_SOCKET; memset(&sa,0,sizeof(sa)); memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length); /* set address */ sa.sin_family = hp->h_addrtype; sa.sin_port = htons((u_short)PORTNUM); s = socket(hp->h_addrtype, SOCK_DGRAM, 0); if (s == INVALID_SOCKET) return INVALID_SOCKET; char str[] = "hello world"; int ret; ret = sendto( s, str, sizeof(str), 0, (struct sockaddr *)&sa, sizeof(sa)); return 0; }