使用java多线程服务器/客户端,如何判断客户端线程是否关闭?
我正在用 Java 编写一个服务器/客户端程序。服务器等待连接,然后为该连接创建一个新线程。然后服务器返回并侦听其他端口上的连接。
sck = srvrSckt.accept();
server.numConnections++;
System.out.print("Connection was made on " + server.port[i] + ".\n");
Connections conn = new Connections(sck);
server.threads[i] = new Thread(conn);
server.threads[i].start();
建立连接后,我想返回并检查之前的连接是否已关闭。然后我会将这些端口标记为可供将来建立连接。有办法做到这一点吗?
我读过,线程之间通信的最佳方式是使用共享变量。但是,我找不到这方面的好例子。另外,我不确定共享变量是否有效,因为我不仅想检查已成功关闭的连接,还想检查突然断开连接的连接。
您能提供的任何帮助将不胜感激。谢谢。
编辑:
这是代码的较大部分。这可能会让我的问题更清楚。
if (server.isAvailable[i] == true)
{
availablePort = server.allPorts[i];
server.isAvailable[i] = false;
ServerSocket srvrSckt = new ServerSocket(availablePort);
System.out.println("Waiting for connection on port " + availablePort + "...");
srvrSckt.setSoTimeout(5000);
try {
sck = srvrSckt.accept();
server.numConnections++;
System.out.print("Connection was made on " + server.port[i] + ".\n");
Connections conn = new Connections(sck);
server.threads[i] = new Thread(conn);
server.threads[i].start();
} catch (Exception e) {
server.isAvailable[i] = true; // no connection was made
srvrSckt.close();
}
I am making a Server/Client program in Java. The server waits for a connection then makes a new thread for that connection. The server then goes back and listens for connections on other ports.
sck = srvrSckt.accept();
server.numConnections++;
System.out.print("Connection was made on " + server.port[i] + ".\n");
Connections conn = new Connections(sck);
server.threads[i] = new Thread(conn);
server.threads[i].start();
After a connection has been made, I want to go back and check if any previous connections have closed. Then I will mark these ports as available for future connections to be made on. Is there a way to do this?
I have read that the best way to communicate between threads is with a shared variable. However, I cannot find a good example of this. Also, I am not sure if a shared variable would work because I not only want to check for connections that were successfully closed, but also for connections that were disconnected abruptly.
Any help you can offer will be greatly appreciated. Thank-you.
EDIT:
Here is a larger portion of the code. That may make my question more clear.
if (server.isAvailable[i] == true)
{
availablePort = server.allPorts[i];
server.isAvailable[i] = false;
ServerSocket srvrSckt = new ServerSocket(availablePort);
System.out.println("Waiting for connection on port " + availablePort + "...");
srvrSckt.setSoTimeout(5000);
try {
sck = srvrSckt.accept();
server.numConnections++;
System.out.print("Connection was made on " + server.port[i] + ".\n");
Connections conn = new Connections(sck);
server.threads[i] = new Thread(conn);
server.threads[i].start();
} catch (Exception e) {
server.isAvailable[i] = true; // no connection was made
srvrSckt.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您完全无法控制为传入远程 TCP 连接分配端口。这就是操作系统的工作。当 Socket 时,套接字将关闭。 isClosed 返回 true 或当 Socket.getInputStream.read 返回 -1 表示流结束。
To my understanding, you have absolutely no control over assigning ports to incoming remote TCP connections. That's the job of the operating system. A socket is closed when Socket.isClosed returns true or when Socket.getInputStream.read returns -1 indicating end of stream.
通常,在服务器上检测与客户端的会话结束的最佳方法是在之间设置“再见协议”客户端和服务器。客户端将固定令牌发布到服务器,服务器通过关闭通道进行确认。如果客户端突然终止,那么服务器了解这一情况的唯一方法是设置心跳协议,每隔一定时间不活动,服务器将 ping 客户端,并且客户端必须通过 pingback 进行响应。如果没有响应,服务器必须终止客户端会话。
至于线程间共享变量,可以参考:http://download .oracle.com/javase/tutorial/essential/concurrency/sync.html
Normally , the best way to detect on the server the end of a session with a client is to set up of the "goodbye-protocol" between client and server. A fixed token is posted by the client to server and server acknowledges by closing the channel. If the client terminates abruptly , then the only way on server to know about this is to setup a heart-beat protocol, every certain time of inactivity , the server will ping client and the client must respond by pingback. If there is no response , server must terminate the client session.
As for sharing a variable between threads , you may refer : http://download.oracle.com/javase/tutorial/essential/concurrency/sync.html