如何从服务器向客户端发送消息

时间:2015-08-20 08:34:49

标签: java

我正在尝试用Java创建一个聊天程序,当我在客户端发送消息时,服务器端获取消息。但是当我从服务器端发送到客户端时,它没有收到消息。

我看不出我做错了什么。

服务器端代码:

private void serverStart(){
    textArea.append("Starting server " + " \n");

    try {
        serverSocket = new ServerSocket(4444);
        textArea.append("Waiting for Clients " + " \n");

        //Reading message from the client
        socket = serverSocket.accept();

        textArea.append("Client Connected " + "\n");

        //Send message to client 
        out = new PrintWriter(socket.getOutputStream());
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while (true)
        {        

            messageFromClient = in.readLine();
            whileChat(messageFromClient); 
        }
    } catch(IOException ioExecption) {
        ioExecption.printStackTrace();
    }
}

private void whileChat(String messageFromClient) {
    showMessage(messageFromClient);
    System.out.println("Message from client : " + messageFromClient);
}

protected static void showMessage(final String message) {
    SwingUtilities.invokeLater( 
    new Runnable(){
        public void run()
        {
            Gui.consoleTextArea.append(message + "\n");
        }
    });
}

public static void sendMessage(String message) {
    out.println(message);
    showMessage(name +  " : " + message + "\n");
}

客户方:

private void connectToServer() {

    try {
        socket = new Socket("localhost", 4444);
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Thread clientThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                // attach to socket's output stream with auto flush turned on
                //Send message to the server
                out = new PrintWriter(socket.getOutputStream(),
                        true);

                //Get return message from server
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                messageFromServer = in.readLine();
                whileChatting(messageFromServer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    clientThread.start();
}

private void whileChatting(String messageFromServer) {
    showMessage(messageFromServer);
    System.out.println("Message from server to client " + messageFromServer);
}

public static void Send(String msg) {
    out.println(name + " : " + msg);
    showMessage(name +  " : " + msg + "\n");
}
protected static void showMessage(final String message) {
    SwingUtilities.invokeLater( 
    new Runnable(){
        public void run(){
            Gui.consoleTextArea.append(message);
        }
    });
}

希望有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

您可以在每次println后立即在PrintWriter上执行刷新,甚至更好:使用autoFlush = true实现PrintWriter:

group by

这样,每次调用out = new PrintWriter(socket.getOutputStream(), true); printlnprintf时,PrintWriter都会在最后执行缓冲区刷新。

答案 1 :(得分:1)

 in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

此行始终在等待客户output,您的客户也不会向server发送消息。

注意:您的serverclient都没有读取用户输入。