Jump to content
  • Advertisement
Sign in to follow this  
PhlashStudios

Polling for other Computers on a LAN in Java

This topic is 4090 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

Ok so for this program I'm writing, I want to poll all the other computers that are connected to the same network as I am. Right now I'm using a for loop to just take my i.p address and go through each different possible local variation and try to connect to it and get a list of the computers on the network that way (the ones that are running my program ne way). This method seems sort of brute force though and is pretty slow. Is there a better way to do this? (I know how to do this using DirectPlay in c++ so it can be done...just not sure how) Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
I have no experience with networking in Java, but in general what you need is broadcasting. Basicly you setup each machine to listen on the same address (the broadcast address). When your client machine then asks for all other machines it sends out the broadcast packet. When the other machines read the packet they can respond accordingly. A small link http://en.wikipedia.org/wiki/Broadcast_address.

Share this post


Link to post
Share on other sites
Is there any reason that your remote clients can't register with a central controller when they are invoked? That would seem the easiest to me. If they registered their availabilty, you wouldn't have to go hunting blind for them.

Share this post


Link to post
Share on other sites
Well the whole thing about this is not having a centralized server...the broadcast address seems really good I'm just not sure how to get a server to listen to another address like 192.168.1.64.255 or something.
So far i have this code:

public class BroadcastRunnable implements Runnable{
public void run(){
int port = 5000;
try{
System.out.println("Starting server on port: " + port);
ServerSocket socket = new ServerSocket();
SocketAddress adr = socket.getLocalSocketAddress();
//I just dont know how to stick the extra .255 on there
socket.bind(adr);

while (true) {

}
}catch(Exception ex){ex.printStackTrace();}
}
}

/

Share this post


Link to post
Share on other sites
You dont want to add .255 to the end of the address. You want to get the bitwise or result of your ip address and the bit complement of your subnet mask(basicly the inverse). So if your subnet mask is 255.255.255.0 you want 0.0.0.255 (setting every 1 bit to 0 and every 0 bit to 1). Then you or the address 192.168.1.64 | 0.0.0.255 will give you 192.168.1.255 this is your broadcast address. In most cases to send/recv on the broadcast address you need to use specific functions because that address is reserved for broadcast only. You may want to browse around any documentation, there might be functions to handle all of this for you.

Share this post


Link to post
Share on other sites
You can't broadcast TCP traffic, TCP is a point to point protocol. Instead, create a DatagramSocket to listen at a given port. Have clients create a DatagramSocket and send a DatagramPacket (or whatever the class is) to the broadcast address. I think the address 255.255.255.255 will work. See the note from the DatagramSocket documentation page:
Quote:

UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

Example: DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888)); Which is equivalent to: DatagramSocket s = new DatagramSocket(8888); Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.


Clients send their packet and listen for responses. Add response packet InetAddresses to a list and then attempt to initiate your TCP connection to it. (ServerSockets are TCP... right? [smile] )

Share this post


Link to post
Share on other sites
Quote:
Original post by PhlashStudios
What if the ip i get is incomplete?


Incomplete?

Server will receive DatagramPacket. That one has getSocketAddress(), which is where you respond to.

Share this post


Link to post
Share on other sites
Quote:
Original post by PhlashStudios
Ok so i set the address of the DatagramPacket to 255.255.255.255 and send it there on port 8888 and that should work?


Yes, all hosts in the same LAN segment listening on UDP port 8888 should hear the packet.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!