消息未通过Java发送回客户端

时间:2012-03-19 11:08:43

标签: java sockets client-server

客户端中声明的字符串将发送到服务器并成功打印到控制台。没有消息被发送回客户端。没有要报告的错误消息。应用程序只是等待。我出错的任何想法?

ClientWorker线程

import java.io.*;
import java.net.*;

public class HTCPCPClientWorker extends Thread {

    Socket cwsocket = null;

    public HTCPCPClientWorker(Socket cwsocket) {
        super("ClientWorker");
        this.cwsocket = cwsocket;
    }

    @Override
    public void run() {

        String clientSentence = null;
        // Create input and output stream
        BufferedReader inFromClient = null;
        PrintWriter outToClient = null;

        try {

            do {  // wait for 'QUIT'              
                // Create input stream
                inFromClient = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));

                System.out.println("\nMESSAGE FROM CLIENT:");

                do {
                    clientSentence = inFromClient.readLine();
                    System.out.println("\t" + clientSentence);

                    if (clientSentence.equals("@@") == true) {

//                      send message to client
                        if (clientSentence.contains("BREW")) {

                             outToClient = new PrintWriter(cwsocket.getOutputStream(), true);

                            outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
                            outToClient.println("Content-length:  " + clientSentence.length()); 
                            outToClient.println("@@"); // terminating chars
                            outToClient.flush();
                        }
                        break;
                    }
                } while (true);

            } while (!clientSentence.contains("QUIT"));

            cwsocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    } // end run


} // end HTCPCPClientWorker.java

客户端

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class HTCPCPClient {

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

        HTCPCPClient client = new HTCPCPClient();
        System.out.println("WELCOME TO THE APPLICATION!");
        client.startClient();
    }

    private void startClient() throws IOException {
        final String HOST = "localhost";
        final int PORT_NUMBER = 4444;
        Socket clientSocket = null;
        PrintWriter outToServer = null;
        BufferedReader in = null;
        String serverSentence = null;
        String clientSentence = null;
        BufferedReader inFromServer = null;

        // create new socket
        clientSocket = new Socket(HOST, PORT_NUMBER);
        outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        do {  // wait for 'QUIT'              
            // Create input stream
            inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            // message to send.  Will accept user input later
            clientSentence = "BREW coffee://127.0.0.1/pot-1";

            // Send clientSentence to server
            outToServer.println(clientSentence);
            outToServer.flush();

            System.out.println("\nMESSAGE FROM SERVER:");

            do {
                serverSentence = inFromServer.readLine();
                System.out.println("\t" + serverSentence);

                if (serverSentence.equals("@@") == true) {
                    break;
                }
            } while (true);
            // read and print message from server

        } while (!clientSentence.contains("QUIT"));

        // close connections 
        outToServer.close();
        in.close();
        inFromServer.close();
        clientSocket.close();
    }
}

提前致谢

2 个答案:

答案 0 :(得分:2)

我不懂代码。特别是,这部分:

if (clientSentence.equals("@@") == true) {
//   send message to client
     if (clientSentence.contains("BREW")) {
         // do stuff
     }
break;
}

永远不会调用“do stuff”部分,因为clientSentence不能同时为“@@”并且包含“BREW”。

您也可能想稍微重构一下代码。看起来很复杂。但那只是我的意见。

答案 1 :(得分:1)

我认为在显示来自客户端的消息后,ClientWorker类会检查以下两个条件

if (clientSentence.equals("@@") == true)

if (clientSentence.contains("BREW"))

因此,当这些条件为真时,只有控件才会进入内部块并发送消息。 所以你必须确保满足这两个条件。

在第一个条件中,您使用的是equals()方法,因此如果此条件为真,则永远不会满足内部if条件。所以你必须考虑这些条件。