简体   繁体   中英

MultiThreaded Server, which is a client to another server

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:

客户端服务器客户端模型的图像

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM