Search code examples
javasocketsnetwork-programminglanmultiplayer

Connecting to lan network


I am writing a platform game, and i thought it would be cool to add a multi-player mode for people who are playing on the same network. My question is how would i query through all the available computers open on a certain port for connecting to play multi-player, and then how would i establish a connection with them. I thought i could just create a socket and just try to connect on every port, but how would i do that if i dont know the other computer ip address. On google i saw this question get asked several times, however none of the answers actually seemed helpful.


Solution

  • You will propably want to broadcast a message (broadcasts are received by all devices on the network). Then you would have the other machines listening fir such incoming broadcasts.

    Basically in a broadcast you would advertise that a computer is running the program, and is willing to establish a direct connection. Then one of the computers would connect straight to the other, and you would work on from there.

    EDIT: Someones similarily done aproach in java (blog post)