UDP broadcast!!
-
I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!
-
I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!
I too tried the same thing some time back. I did not get help from anywhere. As my project deadline was nearing, I found a 'rude' way. I appended the IP address of the broadcaster to the broadcast message at the very end!:(
-
I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!
You may want to try this, I never tried it with UDP socket but think it should work. BTW let me know if you try it and if it works!
DWORD ReadSocket(SOCKET s, LPBYTE lpBuffer, DWORD dwMaxSize,
DWORD dwTimeout /*=INFINITE*/, SOCKADDR_IN* pAddrIn /*=NULL*/)
{
fd_set fdRead = { 0 };
TIMEVAL stTime;
TIMEVAL *pstTime = NULL;if ( INFINITE != dwTimeout ) {
stTime.tv_sec = 0;
stTime.tv_usec = dwTimeout*1000;
pstTime = &stTime;
}// Set Descriptor
if ( !FD_ISSET( s, &fdRead ) )
FD_SET( s, &fdRead );// Select function returns if data is available
DWORD dwBytesRead = 0L;
int res = select( s+1, &fdRead, NULL, NULL, pstTime );
if ( res > 0)
{
res = recvfrom( s, (LPSTR)lpBuffer, dwMaxSize, 0, pAddrIn, sizeof(SOCKADDR_IN));
dwBytesRead = (DWORD)((res > 0)?(res) : (-1));
}
return dwBytesRead;
}"One good thing about getting older, you don't loose the ages you've been!"
-
I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!
It's in the documentation for recvfrom. The second last parameter can point to something you can get the senders IP from.