用于聊天程序的JAVA线程客户端

时间:2014-04-29 16:17:53

标签: java multithreading

我还在努力改善这些帖子的正确性。 但这是我的问题,我知道这是我监督的一件小事...... 我为这个聊天程序启动了我的服务器并运行正常,但我的客户端无法正常工作。我可以写入服务器,没有任何反应......当发送到服务器的下一条消息时,第一条消息的回复就会回来。我会在这里上传我的客户,并且很乐意为您提供任何帮助。

我仍然处于开发状态,这就是为什么我在我的捕获器上使用运行时错误..这将在以后发布:)

我只是试图通过启动我的服务器来启动它,启动两个客户端并尝试让他们使用consol与彼此交谈。

public class ChatClient implements Runnable {

private static final int PORT = 4711;
private static final String HOST = "localhost";
Socket socket = new Socket();
private final PrintWriter out;
private final BufferedReader in;
private final BufferedReader keyboard;
public static boolean running = true;

public ChatClient(Socket socket) throws IOException {
    this.socket = socket;
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream(), true);
    keyboard = new BufferedReader(new InputStreamReader(System.in));
}

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

    Socket socket = new Socket(HOST, PORT);
    ChatClient client = new ChatClient(socket);
    new Thread(client).start();
}

public void writeMessage(String message) {
    out.println(message);
    out.flush();
}

@Override
public void run() {
    String responseLine;
    while (true) {
        try {
            responseLine = in.readLine();
            if (!responseLine.isEmpty()) {
                System.out.println("<< " + responseLine);
            }
        } catch (IOException ioe) {
            throw new RuntimeException("ups", ioe);
        }

        try {
            String output = (String) keyboard.readLine();
            if (!output.isEmpty()) {

                writeMessage(output);
            }
        } catch (IOException ioe) {
            throw new RuntimeException("ups", ioe);
        }
    }
}

}

根据请求,这是我的服务器代码

public class ChatServer implements Runnable {

public static final int PORT = 4711;
public static boolean running = true;
private final BufferedReader in;
private final PrintWriter out;
private final Socket socket;
public static final Map<String, Socket> userMap = new HashMap();

public ChatServer(Socket socket) throws IOException {

    this.socket = socket;
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream(), true);
}

public static void main(String[] args) throws IOException {
    ServerSocket server = new ServerSocket(PORT);
    while (running) {
        System.out.println("Waiting for client to connect " + PORT);
        Socket socket = server.accept();
        ChatServer chat = new ChatServer(socket);
        //userMap.put("prøve", socket);
        new Thread(chat).start();

    }
}   

public void targetMessage(String target, String message) throws IOException {
    System.out.println("Target: " + target);
    System.out.println("KEYS: " + userMap.get(target));
    System.out.println("MAP: " + userMap.toString());
    System.out.println("USERS: " + userMap.keySet());

    if (userMap.containsKey(target)) {
        Socket s = userMap.get(target);
        ChatServer c = new ChatServer(s);
        c.out.println(message);
    } else {
        out.println("User does not exist or user is not online at the moment");
    }
}  

@Override
public void run() {

    try {
        out.println("Please connect to server writing CONNECT and then your username: ");
        String line = in.readLine();

        while (!line.isEmpty()) {
            InputSplit split = new InputSplit(line);
            switch (split.getCommand()) {
                case "CONNECT":
                    if (!split.getAlias().equals("ALL")) {
                        userMap.put(split.getAlias(), ChatServer.this.socket);
                        line = "You are connected as user: " + split.getAlias();
                        out.println(line);
                        System.out.println("user map size: " + userMap.size());
                        break;
                    } else {
                        out.println("Username can not be ALL");
                    }

                case "MESSAGE":
                    line = split.getMessage();
                    targetMessage(split.getAlias(), line);
                    System.out.println("line : " + line);
                    break;

            }
            line = in.readLine();
        }

    } catch (IOException ioe) {
        throw new RuntimeException("hovsa", ioe);
    }

}

}

1 个答案:

答案 0 :(得分:1)

当您致电responseLine = in.readLine();进行阻止呼叫时,这意味着程序将不会继续到下一行,直到服务器将发送以\n结尾的字符串(一行)。 这也意味着只有在客户端从服务器获取内容后才会调用writeMessage(output);

所以如果你想让这个客户端按你的意愿工作,你应该实现2个线程,1个用于从服务器读取,1个用于写入服务器

相关问题