0

I'm using a server socket to accept clients on the main thread, when a thread is accepted,the clients socket is given to a handler which is started in a new thread to process communications. However, before I start running my server to access clients, it connects to a second server which it must list to and be able to respond to and pass on the messages it gets to it's clients.

Hopefully this image illustrate what I mean:

Image of client server client model

The small server must be continuously listening for input from the big server, and also able to output responses.

//Default constructor
private smallServer(){}

//method to initialise and start the server
public static void StartServer(int port) throws IOException {
    smallServer ss = new smallServer();
    ss.bs= new bigServerClient(ss);
    Thread nsc_Thread = new Thread(ss.bsc);
    bsc_Thread.start();
    //accepts clients and starts new thread for them
    ss.ServerRun(port);
        }

private void ServerRun(int port) throws IOException {
    ServerSocket server = new ServerSocket(port);
    server.setSoTimeout(50);
    while (run) {
        Socket client = null;
        try {
            client = server.accept();
        } catch (SocketTimeoutException e) {
        }

        if (client != null) {
            ClientHandler handler = new ClientHandler(client, this);
            Thread handleThread = new Thread(handler);
            handleThread.start();
        }
    }

    if (!run) {
        synchronized (ClientHandler.handlers) {
            for (ClientHandler handler : ClientHandler.handlers) {
                handler.terminateHandler();
            }
        }
        System.exit(0);
    }
}

public void processBigServerCommand(String toProcess) {
    System.out.println("RESEAVED: " + toProcess);
}

The big server client(on the small server) then does this:

public class bigServerClient implements Runnable {

    private smalsServer ss;
    private PrintWriter printer;
    private BufferedReader reader;
    private Socket socket;

    public bigServerClient(smallServer _ss) throws IOException {
        ss = _ss;
        socket = new Socket("Localhost", 5000);
        printer = new PrintWriter(socket.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        printer.flush();
        SendBigServerMessage("Starting String");
    }

    private void SendBigServerMessage(String toSend) {
        printer.print(toSend);
        printer.flush();
    }

    @Override
    public void run() {
        try {
            while (ss.state()) {
                String inputLine;
                while ((inputLine = reader.readLine()) != null) {
                    ss.processBigServerCommand(inputLine);
                    System.out.println(inputLine);
                }
            }
        } catch (IOException e) {
        } finally {
            try {
                socket.close();
            } catch (IOException ex) {
            }
        }
    }
}

From what's above, can anyone see why the big server client isn't responding to the big server when a message is sent? I'm guessing it's something to do with the main thread blocking the second thread, but I'm not sure... Any help would be greatly appreciated.

Peter
  • 113
  • 3
  • 10

1 Answers1

0

You lost me in your code...
Simplify it.
Your smallServer (see class names conventions) should have persistent connection to BigServer (effectively it is BigServer client) - you can implement it in your smallServer class, it should connect (once) and open I/O to BigServer (once) and close everything once the connection is terminated.
As your smallServer will handle multiple clients and pass their requests to BigServer there is no guarantee of the order of BigServer responses - you should do something to handle that (maybe pass UUID with requests?)
Simplify your smallServer and make sure that it runs...

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • Yeah, I think it's overly complicated myself, but it was just the way I managed to hack it together. I made the small server act like a server to all the clients and that works, but while it's doing that, I can't seem to get it to communicate with the big server, I'm not too worried about passing messages between the clients and the big server, I just need the small server to be able to talk to clients and the big server (Sorry about the names, I should of called them bottom middle and top :P – Peter Mar 09 '14 at 16:07
  • Re-edit your code and add missing methods. `SmallServer` should implement Runnable too and so should `ClientHandler`. Make sure that the code compiles and it should run, there are no obvious problems that should prevent it from running... – Germann Arlington Mar 09 '14 at 16:42