java 客户端结束连接或服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9963743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
client ends connection or server?
提问by lonesome
I'm programming a client server app. I'm wondering to end the connection, which side should decide to end it, client or server?
我正在编写客户端服务器应用程序。我想知道结束连接,哪一方应该决定结束它,客户端还是服务器?
Then if for example the client did it, should the server still keep open or not?
那么如果例如客户端这样做了,服务器是否应该保持打开状态?
Suppose we have the below codes for each client and server:
假设我们对每个客户端和服务器都有以下代码:
socket = new Socket("127.0.0.1",3000);
........
socket.close();
or
或者
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
......
serverSocket.close();
EDIT: according to the matt's answer, i pot my code here and let see why my code doesnt work:
编辑:根据马特的回答,我把我的代码放在这里,看看为什么我的代码不起作用:
generally, i have a Jframe program as client which will connect to a server and while its open, i want the connection being alive, since it send info to the server and server should response for the result. but if i dont use closing the socket from server it gives an error and if i use, after once calculation, it closes the connection:
通常,我有一个 Jframe 程序作为客户端,它将连接到服务器,当它打开时,我希望连接处于活动状态,因为它向服务器发送信息,服务器应该响应结果。但是如果我不使用从服务器关闭套接字它会给出一个错误,如果我使用,经过一次计算,它会关闭连接:
Server
服务器
private static PrintWriter toClient;
private static BufferedReader fromClient;
public static void run() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
{
System.out.println("Server is waiting for connections...");
while(true)
{
openStreams();
processClient();
closeStreams();
}
}
OpenStream:
开放流:
public static void openStreams() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
{
serverSocket = new ServerSocket(3000);
socket = serverSocket.accept();
System.out.println("Connected");
toClient = new PrintWriter(socket.getOutputStream(),true);
fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
Closestearms:
亲密接触:
public static void closeStreams() throws IOException
{
fromClient.close();
toClient.close();
socket.close();
serverSocket.close();
System.out.println("Disconnected");
}
the error i receive if i remove the closestream();
如果我删除了我收到的错误 closestream();
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
采纳答案by Mat
Depends on your application's logic. Both are valid, and you must make sure your code is correct in both cases, since if the connection is broken "involuntarily" (like a network drop), it will appear to both sides as if the other side had closed the connection.
取决于您的应用程序的逻辑。两者都是有效的,并且您必须确保您的代码在这两种情况下都是正确的,因为如果连接“非自愿”中断(如网络中断),那么双方看起来就好像另一方关闭了连接。
So your code must handle both cases gracefully.
所以你的代码必须优雅地处理这两种情况。
Your closing code is suspicious: you're closing the listening socket, which is unusual. You should only close the listening socket when you don't want any more incoming connections (i.e. usually for a server, when you're shutting down the server).
Only close the socket you got from accept
to end that "conversation", and don't re-open the server listening socket. (The exception you're getting might be an "Address already in use" in this case. See How to set reuse address option for a datagram socket in java code?for an example of how to avoid that specific problem.)
您的关闭代码是可疑的:您正在关闭侦听套接字,这是不寻常的。您应该只在您不想再有任何传入连接时关闭侦听套接字(即通常用于服务器,当您关闭服务器时)。
仅关闭您获得的套接字accept
以结束该“对话”,并且不要重新打开服务器侦听套接字。(在这种情况下,您得到的异常可能是“地址已在使用中”。有关如何避免该特定问题的示例,请参阅如何在 Java 代码中为数据报套接字设置重用地址选项。)
回答by Greg Kopff
Either end may close the connection - neither is "more correct".
任何一端都可能关闭连接 - 两者都不是“更正确”。
However, it is important to clear up one (seeming) misunderstanding in your code: closing the ServerSocket is not closing the socket that represents the connection between the two end points.
但是,清除代码中的一个(看似)误解很重要:关闭 ServerSocket 并不是关闭代表两个端点之间连接的套接字。
The ServerSocket is a listening socket on a port. When a client makes a connection to the ServerSocket on that port, a (normal) Socket is returned by the ServerSocket.accept() method. Communication between your two end points uses the input and output streams associated with this (normal) Socket.
ServerSocket 是端口上的侦听套接字。当客户端与该端口上的 ServerSocket 建立连接时,ServerSocket.accept() 方法会返回一个(正常的)Socket。两个端点之间的通信使用与此(普通)套接字关联的输入和输出流。
Closing the ServerSocket just stops listening on the well known port and doesn't relate to the established sockets.
关闭 ServerSocket 只是停止侦听众所周知的端口,与已建立的套接字无关。
回答by Harbeer Kadian
The socket is present in both the client and server program. Closing any socket will throw exception on the other side.
套接字存在于客户端和服务器程序中。关闭任何套接字都会在另一端抛出异常。