Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Winsock: Broadcasting

This topic is 5187 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm working on a Client/Server LAN network-application. I want to be able to click on a button and get the servers, which are currently available. My app uses mainly TCP/IP for sending data and it uses UDP for broadcasting. When the client wants to know which servers are available, it uses the broadcast mechanism:
SOCKADDR_IN Address;
Address.sin_family = AF_INET;

Address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Address.sin_port = htons(3012);
	
unsigned int BroadcastID=889911;
	if(sendto(m_BroadcastSocket,reinterpret_cast<char*>(&BroadcastID),sizeof(BroadcastID),0,reinterpret_cast<SOCKADDR*>(&Address),sizeof(SOCKADDR))==SOCKET_ERROR)
		Throw(cException::E_NETWORK_ERROR,"sendto failed");		
On the server-side, I have an Update member function:
//Check for broadcast messages

Set.fd_count=1;
Set.fd_array[0]=m_BroadcastSocket;
Return=select(0,&Set,0,0,&TimeOut);
if(Return>=1)
{
	SOCKADDR_IN Addr;

		if((Return=recvfrom(m_BroadcastSocket,m_Buffer,65535,0,reinterpret_cast<SOCKADDR*>(&Addr),&SOCKADDR_Size))==-1)
			Throw(cException::E_NETWORK_ERROR,"recvfrom failed: "+string_cast(WSAGetLastError()));
		
		//Send a message to the client who probably wants to connect

		sSendPackage	Package;
		unsigned int	EnumId=121212;
		Package.Message=3;
		Package.Length=sizeof(unsigned int);
		Package.Data=&EnumId;		

		memcpy(m_Buffer,&Package,sizeof(sSendPackage));
		memcpy(m_Buffer+sizeof(sSendPackage),Package.Data,Package.Length);
		
		Return=sendto(m_UDPSocket,m_Buffer,sizeof(sSendPackage)+Package.Length,0,reinterpret_cast<SOCKADDR*>(&Addr),sizeof(SOCKADDR_IN));
		
		if(Return==SOCKET_ERROR)
			Throw(cException::E_NETWORK_ERROR,"send failed: "+string_cast(WSAGetLastError()));
		
		cout<<"Send response"<<endl;
	}
And in the client's Update member function:
fd_set	Set;
timeval	TimeOut;
TimeOut.tv_sec  = 0;
TimeOut.tv_usec = 0;

Set.fd_count=1;
Set.fd_array[0]=m_UDPSocket;
	
long Return=select(0,&Set,0,0,&TimeOut);
if(Return>=1)
{
 cout<<"Got UDP Data"<<endl;
}
Now there's a problem: If I run both, client and server, off the same machine, the client doesn't get the response of the server! What's the problem? [edited by - SlimTimmy on March 6, 2004 4:53:53 PM] [edited by - SlimTimmy on March 6, 2004 4:54:50 PM] [edited by - SlimTimmy on March 6, 2004 4:56:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Things that could be wrong:

1) your client also needs a listening socket
2) you bind the clients listening socket on the same port as the server socket (should generate an error)

You should be able to capture broadcasting messages over the localhost channel, so that''s not the problem. I''ve done it before on a Windows machine. My initial guess is you forgot to create a listening socket for the client.

Share this post


Link to post
Share on other sites
Hello.

I just learnt this a few days ago here is what I did.

For the client.
1. Init Winsock
2. Create a socket
3. Set the socket to broadcast with setsockopt
4. Send a message to the broadcast address

For the Server.
1. Init Winsock
2. Create a socket
3. Bind the socket to a port
4. recvfrom with the socket and address INADDR_ANY
5. Send some server info back to the address I just recvfrom

It works on my pc fine. The client finds the server and you can get some info from it.
Hopefuly some of that helps

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!