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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
int port = 7171;
if (param)
port = reinterpret_cast<short>(param);
SOCKET s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if (s == -1)
{
closesocket(s);
return 1;
}
sockaddr_in addr_in;
addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = INADDR_ANY;
addr_in.sin_port = htons(port);
int bind_ret = bind(s, (sockaddr*)&addr_in, sizeof(addr_in));
if (bind_ret == -1)
{
closesocket(s);
return 1;
}
while (true)
{
char buf[1024];
sockaddr_in client_addr;
int len = sizeof(client_addr);
ClientInfo info;
int n = recvfrom(s, buf, 1024, 0, (struct sockaddr *)&client_addr, &len);
info.addr = inet_ntoa(client_addr.sin_addr);
if (n<0)
{
perror("recvfrom");
}
n = sendto(s, "Got your message\n",17,0,(struct sockaddr *)&client_addr,len);
if (n<0)
{
perror("sendto");
}
{
Mutex<CriticalSection>::Lock lock(client_cs);
clients.push_back(info);
}
}
closesocket(s);
return 0;
}
|