Jump to content
  • Advertisement


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


Winsock: Broadcasting

This topic is 5368 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:
Address.sin_family = AF_INET;

Address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Address.sin_port = htons(3012);
unsigned int BroadcastID=889911;
		Throw(cException::E_NETWORK_ERROR,"sendto failed");		
On the server-side, I have an Update member function:
//Check for broadcast messages


			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.Length=sizeof(unsigned int);

			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;

long Return=select(0,&Set,0,0,&TimeOut);
 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
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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!