[英]How to know how many users are online using multithreading and tcp socket in java
I'm makeing a game online using java socket and I don't know how to FrmServer
can set label for number of users are logged in. In each EchoThread
will handle for a client and has a public static ArrayList<EchoThread> clients = new ArrayList<>();
解决游戏房间划分问题。
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
FrmServer frmServer = new FrmServer(); // this frame server need to know how many clients are logged in
try {
serverSocket = new ServerSocket(PORT);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
System.out.println("I/O error: " + e);
}
// new thread for a client
EchoThread echoThread = new EchoThread(socket);
echoThread.start();
}
}
如何触发表单服务器知道用户已成功登录并增加 label 值或在用户断开连接时减少它?
注意:我处理了登录和断开连接,并且都在 EchoThread 中解决了
我做到了,我只需frmServer
object 传递给 EchoThread 并从 Echothread 中调用 setLabel function 。
EchoThread echoThread = new EchoThread(socket, frmServer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.