13

I have a local network with DHCP and a few PCs. One of these should be my Server and get automatically connected to all others (clients). My idea was this: First, I create a server on every client (CServer) that is listening for a client programm from the server (SClient). When the SClient connects to a CServer, the SClient sends the CServer his IP, so he knows there will be the server on this IP. Then after trying all IPs in his IP range (e.g. 192.168.1.xxx), he starts the real server and all the clients connect to the known server IP. But when I try the following, the SClient just freezes at the first IP, when trying to connect to 192.168.1.0. How can i define a timeout or something similar that lets the SClient drop the unsuccessful connection and going on with 192.168.1.1?

import java.lang.*;
import java.io.*;
import java.net.*;

class SClient {
    public SClient() {
        for(int i = 120; i < 125; i++){
            try{
                InetAddress addr = InetAddress.getLocalHost();
                String addrs = addr+"";
                String ip = addrs.substring(addrs.indexOf("/")+1);
                Socket s1 = new Socket("192.168.1." + i, 1254);

                OutputStream s1out = s1.getOutputStream();
                DataOutputStream dos = new DataOutputStream (s1out);
                dos.writeUTF(ip);
                dos.close();
                s1out.close();
                s1.close();
            }catch(IOException e){}
        }
    }
}

and

import java.lang.*;
import java.io.*;
import java.net.*;

class CServer {
    public CServer() throws IOException{
        ServerSocket s = new ServerSocket(1254);

        while(true){
            Socket s1=s.accept();
            InputStream s1In = s1.getInputStream();
            DataInputStream dis = new DataInputStream(s1In);
            String st = new String (dis.readUTF());
            System.out.println(st);
            dis.close();
            s1In.close();
            s1.close();
    }
}

}

2
  • 1
    It may be because you are starting with 192.168.1.0 . If I remember correctly, an IP address will never end in a 0, just subnet masks will. Try starting at 1 and see if it works any better. It should time out fairly quickly if it is going to a non-existent IP. Commented Mar 2, 2012 at 0:07
  • in this case, I started at 192.168.1.120, because i know I got an existent IP at 123, but it won't time out. Commented Mar 2, 2012 at 13:31

2 Answers 2

38

I've found a solution for my problem. It was just initializing the Socket not with

Socket s1 = new Socket("192.168.1." + i, 1254);

but with

Socket s1 = new Socket();
s1.setSoTimeout(200);
s1.connect(new InetSocketAddress("192.168.1." + i, 1254), 200);

Thanks anyway!

Sign up to request clarification or add additional context in comments.

2 Comments

If this solves your problem, then accept your own answer. You won't get any points for it (of course!) but it helps other people reading this question, because they'll know it was the solution.
I wanted to accept my own answer, but I have to wait another 16 hours. Then I will do.
0

It's much easier to do this with UDP. The general logic would be:

  1. Identify a well known port for 'discovery'
  2. Any machine that starts up sends out a 'Query Master Server' message
  3. If a response is not received to that message within a time frame you define, then the machine that sent it automatically designates itself as being the server.
  4. Henceforth, any machine that sends out a 'Query Master Server' message will get a response back from the master, with its IP address and a 'communication port'
  5. Connect from the new machine to the server on the communication port and start sending messages.

You might run into situations where more than one server thinks it is the master in this scenario, and then you would need a conflict resolution process, but the outline should give you a general idea of a process that will work for you.

5 Comments

You would need to do multicast and account for race conditions.
Yes, he would indeed need to use multicast. What race conditions do you refer too?
Two machines come up within seconds of each other, both send out a query for a master and find no master (neither is master yet). Each names itself master. You now have 2 masters. I guess you can solve this by opening the multicast port first and use a deterministic, absolute, and transitive election algorithm to deal with the two (or more) master problem. What about established master vs. new master issue? It is a bit of a head ache, especially because the original question was about a scenario with a single master and a lot of clients (all on DHCP).
Yes, this is the conflict resolution process I referred to but did not outline. Mainly because it is a serious headache - infact it tends to be the most complicated part of distributed, dynamic server protocols. A naive voting process involves sending out packets between the competing servers with their startup time. All servers with a more recent startup time then de-designate themselves back to being regular clients.
True, that is a reasonable algorithm. Perhaps also generate a UUID at start up for tie breaking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.