如何停止新连接?

时间:2021-04-08 07:26:19

I am trying to search a Server and once the client finds it, he stops searching for new Servers. But now the Client finds the Server, can get messages from the Server but trys connecting to other once so I cant write the Server.

我正在尝试搜索服务器,一旦客户端找到它,他就会停止搜索新的服务器。但是现在客户端找到服务器,可以从服务器获取消息但是尝试连接到其他服务器,因此我无法编写服务器。

Can u guys tell me what I need to do with my Code?

你能告诉我我的代码需要做什么吗?

ty Bono

for (pcip = 0; pcip <= 253; pcip++) {
            clientVerbindung = new ChatSocket(client.getUserName(), /*
                                                                     * Die IP an
                                                                     * welche
                                                                     * der
                                                                     * Client
                                                                     * verbindet
                                                                     */
            "10.4.0." + pcip, /* Der Port welcher verwendet wird */3005, client);
            clientVerbindung.start();
        }

ChatSocket:

public class ChatSocket extends Thread {

    private String nameDesUsers;
    private String ip;
    private int port;
    private P2P client;
    private String user;
    private String path;

    private DataInputStream inStream;
    private DataOutputStream outStream;

    public ChatSocket(String nameDesUsers, String ip, int port, P2P client) {
        this.nameDesUsers = nameDesUsers;
        this.port = port;
        this.ip = ip;
        this.client = client;
    }

    public void run() {
        try {
            Socket clientSocket = new Socket(ip, port);

            InputStream in = clientSocket.getInputStream();
            OutputStream out = clientSocket.getOutputStream();

            inStream = new DataInputStream(in);
            outStream = new DataOutputStream(out);

            // Intepretiert den erhaltenen Text. Passiert nur bei diesem Socket
            // und nicht umbedingt auch beim sender
            outStream.writeUTF(nameDesUsers);
            while (true) {
                String utf = inStream.readUTF();

                client.setMessage("Server", utf);
            }

            // Mögliche Fehlermeldungen
        } catch (UnknownHostException uhe) {
            System.out.println("Client Fehler: " + uhe.toString());
        } catch (IOException ioe) {
            System.out.println("Client Fehler: " + ioe.toString());
        }
        System.out.println("Die Verbindung wurde geschlossen.");
    }

    public void sendText(String message) {
        try {
            if (message != null) {
                // Nimmt den aktuellen Text aus dem clientText und schickt ihn
                // an den Server
                outStream.writeUTF(message);
            }

            // Mögliche Fehlermeldungen
        } catch (IOException ioe) {
            System.out.println("Fehler beim senden im Client: "
                    + ioe.toString());
        } catch (NullPointerException npe) {
            System.out.println("Fehler beim senden im Client: "
                    + npe.toString());
        }
    }
}

1 个解决方案

#1


If there isn't a valid connection you will get a ConnectException.

如果没有有效的连接,您将获得ConnectException。

Ergo, if you don't get that exception, you have a valid connection.

如果您没有得到该例外,那么您就拥有了有效的连接。

Hard to see what the mystery is.

很难看出这个谜团是什么。

#1


If there isn't a valid connection you will get a ConnectException.

如果没有有效的连接,您将获得ConnectException。

Ergo, if you don't get that exception, you have a valid connection.

如果您没有得到该例外,那么您就拥有了有效的连接。

Hard to see what the mystery is.

很难看出这个谜团是什么。