简体   繁体   English

多线程服务器,它是另一台服务器的客户端

[英]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. 您的smallServer (请参阅类名称约定)应该与BigServer保持持久连接(实际上是BigServer客户端)-您可以在smallServer类中实现它,它应该连接(一次)并向BigServer打开I / O(一次)并关闭所有内容一旦连接终止。
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?) 由于您的smallServer将处理多个客户端并将其请求传递给BigServer ,因此无法保证BigServer响应的顺序-您应该做一些处理(可以将UUID与请求一起传递吗?)
Simplify your smallServer and make sure that it runs... 简化您的smallServer并确保其运行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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