有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何停止多线程聊天客户端

我正在尝试使用Java创建一个多线程聊天客户端服务器。我用这个教程作为开始:http://pguides.net/java-tutorial/java-tcp-clientserver-chat/

当我输入字符串“quit”时,我希望客户端停止,但我不明白该如何做。此外,当客户端断开连接时,我需要将客户端从已连接的nick列表中删除

服务器

/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.util.Hashtable;

public class ChatServer {
    private static int port = 1001; 

    public static void main (String[] args) throws IOException {

        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port);
            System.err.println(e);
            System.exit(1);
        }

        Socket client = null;
        while(true) {
            try {
                client = server.accept();
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.err.println(e);
                System.exit(1);
            }
            /* start a new thread to handle this client */
            Thread t = new Thread(new ClientConn(client));
            t.start();
        }
       }
}

class ChatServerProtocol {
    private String nick;
    private ClientConn conn;

    private static Hashtable<String, ClientConn> nicks = new Hashtable<String, ClientConn>();

    private static final String msg_OK = "OK";
    private static final String msg_INVALID = "INVALID COMMAND";
    private static final String msg_SEND_FAILED = "FAILED TO SEND";

    private static boolean add_nick(String nick, ClientConn c) {
        if (nicks.containsKey(nick)) {
            return false;
        } else {
            nicks.put(nick, c);
            return true;
        }
    }

    private static boolean remove_nick(String nick, ClientConn c) {
        if (!(nicks.containsKey(nick))) {
            return false;
        } else {
            nicks.remove(nick);
            return true;
        }
    }

    public ChatServerProtocol(ClientConn c) throws IOException {
        nick = null;
        conn = c;
    }


    private boolean sendMsg(String recipient, String msg) {
        if (nicks.containsKey(recipient)) {
            ClientConn c = nicks.get(recipient);
            c.sendMsg(nick + ": " + msg);
            return true;
        } else {
            return false;
        }
    }


    public String process(String msg) {
        if (msg.startswith("Nick"){
        String output = "";
        if(add_nick(tryauthor, this.conn)) {
            this.nick = tryauthor;
            output = "Welcome "+tryauthor;
            } else {
                    output = "Nick already in";
                }
            }

        }
        else if (msg.startsWith("msg")) {
            String[] msg_parts = msg.split(":");
            for(int i=0; i<msg_parts.length; i++){
                System.out.println(msg_parts[i]);
            }
            String msg_type = msg_parts[0];
            if(msg_type.equals("msg")) {
                if(msg_parts.length < 3) output = msg_INVALID;
                if(sendMsg(msg_parts[1], msg_parts[2])) output = msg_OK;
                else output = msg_SEND_FAILED;
            } else {
                output = msg_INVALID;
            }
        }

        return output;
     }
    }

class ClientConn implements Runnable {
    private Socket client;
    private BufferedReader in = null;
    private PrintWriter out = null;

    public ClientConn(Socket client) {
        this.client = client;
        try {
            /* obtain an input stream to this client ... */
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            /* ... and an output stream to the same client */
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            System.err.println(e);
            return;
        }
    }

    public void run() {
        String msg, response;

        try {
             ChatServerProtocol protocol = new ChatServerProtocol(this);
            /* loop reading lines from the client which are processed 
             * according to our protocol and the resulting response is 
             * sent back to the client */
            while ((msg = in.readLine()) != "quit\r\n") {
                response = protocol.process(msg);
                out.println("SERVER: " + response);
            }
            this.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void sendMsg(String msg) {
        out.println(msg);
     }

    public void close() throws IOException {
        in.close();
        out.close();
        client.close();
    }

}

客户

/* ChatClient.java */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient {
private static int port = 1001; 
private static String host = "localhost"; 

private static BufferedReader stdIn;

public static void main (String[] args) throws IOException {

    Socket server = null;

    try {
        server = new Socket(host, port);
    } catch (UnknownHostException e) {
        System.err.println(e);
        System.exit(1);
    }

    stdIn = new BufferedReader(new InputStreamReader(System.in));

    PrintWriter out = new PrintWriter(server.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));

    System.out.print("Nick: ");
    String auth = stdIn.readLine();
    out.println("Nick: " + auth);
    String serverResponse = in.readLine();
    System.out.println(serverResponse);

    if (serverResponse.startsWith("SERVER: welcome")) {
         /* create a thread to asyncronously read messages from the server */
        ServerConn sc = new ServerConn(server);
        Thread t = new Thread(sc);
        t.start();

        String msg;
        /* loop reading messages from stdin and sending them to the server */
        while ((msg = stdIn.readLine()) != "quit\r\n") {
            out.println(msg);
        }
        sc.close();
        System.out.println("Exit.");
        System.out.println("---Client Error---");
    }
    else {
        System.out.println("Exit.");
        System.out.println("---Client Error---");
    }


  }
}

class ServerConn implements Runnable {
    private BufferedReader in = null;
    private Socket server;

    public ServerConn(Socket s) throws IOException {
        server = s;
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    }

    public void run() {
        String msg;
        try {
            /* loop reading messages from the server and show them 
             * on stdout */
             while ((msg = in.readLine()) != "quit\r\n") {
                System.out.println(msg);
             }
             this.close();
        } catch (IOException e) {
            System.err.println(e);
        }
      }

    public void close() throws IOException {
        in.close();
        server.close();
    }

}

我应该在哪里以及如何关闭连接


共 (1) 个答案

  1. # 1 楼答案

    I want the client to stop when I enter the String "quit" but I don't understand how I could do it.

    您可以使用System.exit(status)立即终止当前的Java程序。由于操作系统会在进程终止时自动释放进程获取的所有资源(如tcp套接字),因此也会关闭连接

    Also, I need to remove the client from the connected nicks list when the client disconnects.

    这更难。请记住,发生断开连接的原因可能不是客户端终止,例如,因为网线已拔出。也就是说,客户端无法可靠地通知服务器它已经不在了,服务器必须自己检测到这一点。我希望服务器操作系统的tcp实现能够检测到客户机已不在那里(速度可能取决于实现),并向使用该套接字的应用程序抛出异常,您可以捕获该套接字并将其从列表中删除