问题
I am building a multiplayer game, so once the server started i want to broadcast server name continuously so that client can know that there are some server is running. I don't want to give IP address and port number to connect to server. can someone help me to broadcast server name.
- its an app not an web app.
回答1:
There is an auto-responded question in SO already talking about this: How to make a server discoverable to LAN clients
You can use the same technique to ask for servers in the local network.
The approach (YMMV) in video games is:
- Servers start and bind to broadcast.
- Clients start and bind to broadcast.
- Clients send a broadcast message asking for servers.
- Servers receive broadcast messages from clients and reply with another broadcast about them (they maybe resend that message a second or two later, if they feel paranoid).
- Clients wait a few seconds and record the information of all servers responding.
- Clients present a list with the servers and ask the players to choose one.
- Maybe they will want to keep doing the previous step in parallel, during the player visit to the server list page.
- Clients unbind from broadcast and try to connect to the player selected server.
- Usually the port can be selected too, but a default port for this connection is available by default in the game. This way you do not even need the server identity to be broadcasted in the message if the port is not changed...
- Everyone stops saturating the network with broadcasting messages !!! :P
To discover server over Internet, you need some kind of notification service. You have plenty of options in the network that work as generic discovery services, popular since the android networking games era.
回答2:
If the location of your server is constant, why wouldn't you just define the server ip address in your code and have your script connect to it? The user would never have to see the ip address of your server.
来源:https://stackoverflow.com/questions/19032609/broadcasting-socket-server-in-python