简体   繁体   English

客户端关闭后保持套接字服务器打开

[英]Keep Socket Server Open After Client Closes

I have implemented a socket with a server and single client. 我已经实现了带有服务器和单个客户端的套接字。 The way it's structured currently, the server closes whenever the client closes. 按照当前的结构方式,只要客户端关闭,服务器就会关闭。 My intent is have the server run until manual shutdown instead. 我的意图是让服务器运行,直到手动关机为止。

Here's the server: 这是服务器:

public static void main(String args[]) { ; 公共静态无效main(String args []){;

    try
    {
         ServerSocket socket= new ServerSocket(17);

         System.out.println("connect...");
         Socket s = socket.accept();
        System.out.println("Client Connected.");


        while (true)
            {

                work with server

            }

}
catch (IOException e)
    {
        e.getStackTrace();
     }

} }

I've tried surrounding the entire try/catch loop with another while(true) loop, but it does nothing, the same issue persists. 我尝试用另一个while(true)循环包围整个try / catch循环,但是它什么也没做,同样的问题仍然存在。 Any ideas on how to keep the server running? 关于如何保持服务器运行的任何想法?

It looks like what's going to happen in your code there is that you connect to a client, infinitely loop over interactions with the client, then when someone disrupts the connections (closes clearning, or interrupts it rudly - eg, unplug the network cable) you're going to get an IOException , sending you down to the catch clause which runs and then continues after that (and I'm guessing "after that" is the end of your main()?)... 看起来您的代码中将要发生的事情是,您连接到客户端,无限循环与客户端的交互,然后当有人破坏连接(关闭清理或不加中断地进行连接-例如,拔下网络电缆)时,您将要获得IOException ,将您带到运行的catch子句,然后在那之后继续(我猜“那之后”是您main()的结尾吗?)...

So what you need to do is, from that point, loop back to the accept() call so that you can accept another, new client connection. 因此,从这一点出发,您需要做的是循环回到accept()调用,以便您可以接受另一个新的客户端连接。 For example, here's some pseudocode: 例如,下面是一些伪代码:

create server socket
while (1) {
    try {
        accept client connection
        set up your I/O streams

        while (1) {
            interact with client until connection closes
        }
    } catch (...) {
        handle errors
    }
} // loop back to the accept call here

Also, notice how the try-catch block in this case is situated so that errors will be caught and handled within the accept-loop. 另外,请注意在这种情况下try-catch块的位置,以便可以 accept-loop中捕获并处理错误。 That way an error on a single client connection will send you back to accept() instead of terminating the server. 这样,单个客户端连接上的错误将使您返回到accept()而不是终止服务器。

Keep a single server socket outside of the loop -- the loop needs to start before accept(). 将单个服务器套接字保留在循环之外-循环需要在accept()之前开始。 Just put the ServerSocket creation into a separate try/catch block. 只需将ServerSocket创建放入单独的try / catch块中。 Otherwise, you'll open a new socket that will try to listen on the same port, but only a single connection has been closed, not the serverSocket. 否则,您将打开一个新的套接字,该套接字将尝试在同一端口上进行侦听,但只关闭了一个连接,而不是serverSocket。 A server socket can accept multiple client connections. 服务器套接字可以接受多个客户端连接。

When that works, you probably want to start a new Thread on accept() to support multiple clients. 在这种情况下,您可能想在accept()上启动一个新线程以支持多个客户端。 Simplest way to do so is usually to add a "ClinentHandler" class that implements the Runnable interface. 最简单的方法通常是添加一个实现Runnable接口的“ ClinentHandler”类。 And in the client you probably want to put reading from the socket into a separate thread, too. 并且在客户端中,您可能还希望将套接字读取内容放入单独的线程中。

Is this homework / some kind of assignment? 这是作业/某种作业吗?

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

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