1

I read many example about Java Socket on Network and all of this has same line of this code for client :

sock = new Socket("localhost", PORT);

So, we just can test server and client on the same computer. When in LAN or ad-hoc, if client has no way to know name of the server. (except hard code, input directly name of server to client code, but this is very very ineffective, for example, if I want to write a LAN game, so name of server we cannot know before running game)

I have though about this solution : when the server is created, it will listen who connect to them, and then send back the name of server. But, again, we meet a paradox : the client cannot connect to server to take this data.

So, how to overcome this hard part. (if there is a way using other java library, it's okay,too).

pb2q
  • 58,613
  • 19
  • 146
  • 147
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 4
    possible duplicate of [Network discovery in Java using multicasting](http://stackoverflow.com/questions/3258959/network-discovery-in-java-using-multicasting) – Michael Brewer-Davis Oct 10 '12 at 15:02

1 Answers1

1

No, you can't. That's why there are sort of "brokers", centralized components that can be reached using hard coded information (for example a URL), where the list of available resourcea are available. So you need a central server, that the available game servers register to, and the clients can obtain the list.

(except hard code, input directly name of server to client code, but this is very very ineffective, for example, if I want to write a LAN game, so name of server we cannot know before running game)

Why not just ask the user to which server he/she wants to connect to?

I have though about this solution : when the server is created, it will listen who connect to them, and then send back the name of server.

I think that once the client found the server, this is not of any use.

EDIT: I think I misread the question a bit. If there is no requirement for this to work over multiple network segments (for participants not on the same WiFi, or LAN), then the referenced post about multicasting might be a viable option to find the available servers.

EDIT2: wanted to add as comment, but it didn't fit...

I didn't play LAN games since 10 or more years, so I might be a bit off on this, but I remember, that one player had to act as a host, and the other players joined the game. This qould mean, that that player starts a server (basically listens to a predefined port number), and the other players send out multicast packets to find the servers. Multicasting means that everyone on the network segment will get it, and if a process listens on that port, and is backed by the right software, it will respond.

So in this case, follow the guide @MichaelB suggested: the servers should open a DatagramSocket, and wait until something arrives, and the other players should send the multicast packets. The server should send something back, that would identify what kind of software it is to be able to tell on the client side, if it is a proper server for the client, or something else, that happens to use the same port...

Sending is not, but receiving a DatagramPacket from a socket is a bit counter-intuitive:

byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

After you have the packet, you can call:

SocketAddress remoteHostAddress = packet.getSocketAddress(); 

to find out the address of your host.

This should be of good help: Writing a Datagram Client and Server

ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • because this is a LAN game. should we do, if each time two user connect, they must know each other IP address ? Further, why some LAN game I have played, they can automatically see each other ? can you point me out this point. Thanks :) – hqt Oct 10 '12 at 17:39