Created
December 24, 2011 07:19
-
-
Save NightBrownie/1516674 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "Discovery.h" | |
Discovery::Discovery(char* addr, int port, DiscoveryListener* l) { | |
listener = l; | |
int n; | |
unsigned int length; | |
struct sockaddr_in server, from; | |
struct hostent *hp; | |
char buffer[256]; | |
sock= socket(AF_INET, SOCK_DGRAM, 0); | |
server.sin_family = AF_INET; | |
hp = gethostbyname(addr); | |
memcpy((char *)hp->h_addr, | |
(char *)&server.sin_addr, | |
hp->h_length); | |
server.sin_port = htons(port+1); | |
length=sizeof(struct sockaddr_in); | |
memset(buffer, 0, 256); | |
sendto(sock,"REQ",4,0,(const struct sockaddr *)&server,length); | |
pthread_create(&thread, NULL, worker, this); | |
} | |
void* Discovery::worker(void* x) { | |
Discovery* d = (Discovery*)x; | |
char buf[1024]; | |
int fromlen = sizeof(struct sockaddr_in); | |
struct sockaddr_in from; | |
while (1) { | |
int n = recvfrom(d->sock, buf, 1024, 0, (struct sockaddr *)&from, &fromlen); | |
char* s = new char[n+1]; | |
memcpy(s, buf, n+1); | |
d->listener->OnNewServer(&from, s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment