1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
//Server
bool Server() {
WSADATA WSA;
SOCKET Socket;
SOCKADDR_IN LocalAddr;
if (WSAStartup(0x0202,&WSA)) return false;
if (WSA.wVersion != 0x0202) return false;
Socket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_port = htons(7777);
LocalAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(Socket,(SOCKADDR*)&LocalAddr,sizeof(SOCKADDR))) return false;
SOCKADDR_IN ClientAddr;
int i = sizeof(SOCKADDR);
char data[256];
// recieve first packet and get client address
if (recvfrom(Socket,data,256,0,(SOCKADDR*)&ClientAddr,&i) <= 0) return false;
// send response
if (sendto(Socket,data,256,0,(SOCKADDR*)&ClientAddr,sizeof(SOCKADDR_IN)) > 0) printf("Sent!");
return true;
}
|