简体   繁体   English

与服务器的Java受限连接

[英]Java Limited connections to server

I'm new to java and especially to java networking, but what I'm trying to do is set up a server (for a game). 我是Java的新手,尤其是Java网络的新手,但是我想做的是设置服务器(用于游戏)。 When TWO clients are connected to the server, I want to refuse any other connection. 当两个客户端连接到服务器时,我想拒绝任何其他连接。 Am I able to close the ServerSocket? 我可以关闭ServerSocket吗? And if the ServerSocked is closed, those two connections which has been ran as threads and stored in a collection are still alive and able to cummunicate with the server? 如果ServerSocked已关闭,那么这两个作为线程运行并存储在集合中的连接仍然有效,并且能够与服务器进行通信吗? I hope you've got my point. 我希望你明白我的意思。

Here's the source code: 这是源代码:

//Server
public synchronized List<ClientThread> getClients(){
    return clients;
}


public void run() {
    try {
        while(true){

            Socket clsock = srvsock.accept();

            if(getClients().size() == 2){
                System.out.println("Too many connections!");
                clsock.close();
                continue;
            }

            ClientThread clt = new ClientThread(clsock);
            clients.add(clt);
            clt.start();
            System.out.println("Connection accepted.");

        }
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
    }
}

And with this code, I'm not able to detect on the Client if the connection is still alive, or the server has closed the connection. 并使用此代码,我无法在客户端上检测连接是否仍然有效,或者服务器已关闭连接。 Thanks in advance. 提前致谢。

Code for test client: 测试客户端代码:

Socket s = new Socket("localhost", 8932);               
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

for (int i=0; i<20; i++) {
    bw.write(String.valueOf(i));
    bw.newLine();
    bw.flush();
    Thread.sleep(1000);
}
bw.close();

And for the ClientThread: 对于ClientThread:

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true) {
    String misc = in.readLine();
    System.out.println(misc);
    if (misc==null || misc.length()==0)
        break;
}
in.close();

Output: 输出:

Connection accepted.
0
Connection accepted.
0
1
Too many connections!
1
2
2
3

So it works as you intended. 因此,它可以按您的预期工作。 By the way, it is usually better to implement Runnable rather than extend Thread - see "implements Runnable" vs. "extends Thread" 顺便说一句,通常最好实现Runnable而不是扩展Thread-参见“实现Runnable”与“ extends Thread”

Every time a connection is established, the server returns a new socket ( when you use srvsock.accept() ) for the next connection. 每次建立连接时,服务器将为下一个连接返回一个新的套接字(当您使用srvsock.accept()时)。 So you can close "srvsock" without affecting "clsock". 因此,您可以关闭“ srvsock”而不影响“ clsock”。 To test if they are alive, check this post How do I check if a Socket is currently connected in Java? 要测试它们是否还活着,请查看这篇文章。 如何检查Java中当前是否已连接Socket? . Hope you can solve your problem. 希望您能解决您的问题。

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

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