簡體   English   中英

多線程服務器,它是另一台服務器的客戶端

[英]MultiThreaded Server, which is a client to another server

我正在使用服務器套接字來接受主線程上的客戶端,當一個線程被接受時,將客戶端套接字提供給一個在新線程中啟動以處理通信的處理程序。 但是,在我開始運行服務器以訪問客戶端之前,該服務器連接到第二台服務器,該服務器必須列出該服務器,並且能夠響應並傳遞其到達客戶端的消息。

希望這張圖片能說明我的意思:

客戶端服務器客戶端模型的圖像

小型服務器必須不斷偵聽大型服務器的輸入,並且還必須輸出響應。

//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);
}

然后,大型服務器客戶端(在小型服務器上)執行以下操作:

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) {
            }
        }
    }
}

從上面可以看到,為什么有人在發送消息時為何大型服務器客戶端沒有響應大型服務器? 我猜想這與主線程阻塞第二個線程有關,但我不確定...任何幫助將不勝感激。

您在代碼中迷失了我...
簡化它。
您的smallServer (請參閱類名稱約定)應該與BigServer保持持久連接(實際上是BigServer客戶端)-您可以在smallServer類中實現它,它應該連接(一次)並向BigServer打開I / O(一次)並關閉所有內容一旦連接終止。
由於您的smallServer將處理多個客戶端並將其請求傳遞給BigServer ,因此無法保證BigServer響應的順序-您應該做一些處理(可以將UUID與請求一起傳遞嗎?)
簡化您的smallServer並確保其運行...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM