1

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

我希望客户端在我输入字符串“退出”时停止,但我不明白我该怎么做。此外,当客户端断开连接时,我需要从连接的昵称列表中删除客户端。

服务器

/* 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();
    }

}

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

4

2 回答 2

1

现在,只有当他的名字是我之前创建的图表的一部分时,才能对客户进行身份验证。

这似乎写在代码中。在您的Process()方法中,您在 中查找作者,authorsgraph如果未找到,则返回错误。这不是你想要的吗?如果找不到作者,是否应该添加它们?如果您还没有找到它们以及将作者添加add_nice()到?elseauthorsgraph

在调试器中处理新连接可能会对您有所帮助。消息的自由使用System.out.println()也可能是有用的。

我希望客户端在我输入字符串“退出”时停止,但我不明白我该怎么做。

那么这段代码有一个错误。您必须使用.equals()来检查是否String相等。 !=只是测试msg没有相同的String 引用,不同的字符串内容

// can't use != on a `String` to check contents
while ((msg = stdIn.readLine()) != "quit\r\n") {

它应该是:

while (true) {
   msg = stdIn.readLine();
   if (msg == null || msg.equals("quit")) {
      break;
   }
   ...

另请注意,我没有检查“\r\n”。该readLine()方法返回字符串(引用 javadocs)“不包括任何行终止字符”。此外,您应该测试null套接字是否关闭。您的客户端代码中也有 2 个位置存在相同的错误。

当客户端断开连接时,我需要从连接的昵称列表中删除客户端。

while如果客户端套接字关闭或它们进入,您的循环将退出"quit"。然后你打电话close()。打电话后,close()您可以打电话protocol.remove_nicks()将他从循环中删除。但你没有"nick" String那里。

真的,维护nicks 里面的列表ChatServerProtocol可能不是合适的地方。我会自己ChatServerProtocol加入ClientConn课堂,因为每个连接都有一个。无论哪种方式,某些对象都需要存储"nick"作者登录时使用的字符串,然后在输入或连接关闭remove_nick()后使用该字符串调用。"quit"

最后,HashTable从 Java 5 开始不推荐使用 of。我会Collections.synchronizedMap(new HashMap<...>())改用。

希望这可以帮助。

于 2012-05-20T14:13:51.533 回答
1

我希望客户端在我输入字符串“退出”时停止,但我不明白我该怎么做。

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

此外,当客户端断开连接时,我需要从连接的昵称列表中删除客户端。

那更难。请记住,断开连接可能是由于客户端终止以外的原因而发生的,例如因为网络电缆被拔出。也就是说,客户端不能可靠地通知服务器它不再存在,服务器必须自己检测到这一点。我希望服务器操作系统的 tcp 实现能够检测到客户端不再存在(取决于实现的速度可能有多快),并使用该套接字向应用程序抛出异常,您可以捕获并从列表。

于 2012-05-20T10:46:31.733 回答