如何:Java Server Socket响应和客户端读取

时间:2017-05-23 07:42:58

标签: java sockets client-server

想象一下下一个案例:

客户端 - 服务器连接

客户端向服务器发送请求 服务器回答客户端 客户阅读答案

班主任:

public class Client extends Service{

    private  String IP_ADDRESS;
    private  int PORT;

    public void start(){
        l.info("Starting client for server at: "+IP_ADDRESS+":"+PORT);
        //Initialization of the client
        try {
            cs=new Socket(IP_ADDRESS,PORT);
        } catch (UnknownHostException e) {
            l.error("Unkown host at the specified address");
            e.printStackTrace();
        } catch (IOException e) {


        l.error("I/O error starting the client socket");
                e.printStackTrace();
            }
        }

        //Sends the specified text by param
        public void sendText(String text){
            //Initializa the output client with the client socket data
            try {
                //DataOutputStream to send data to the server
                toServer=new DataOutputStream(cs.getOutputStream());

                l.info("Sending message to the server");

                PrintWriter writer= new PrintWriter(toServer);
                writer.println(text);
                writer.flush();

            } catch (IOException e) {
                l.error("Bat initialization of the output client stream");
                e.printStackTrace();
            }
        //Should show the answers from the server, i run this as a thread
        public void showServerOutput(){
            String message;

            while(true){
                //If there are new messages
                try {
                    BufferedReader br= new BufferedReader(new InputStreamReader((cs.getInputStream())));
                    if((message=br.readLine())!=null){
                        //Show them
                            System.out.println(message);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

   }

showServerOutput()是返回服务器发送的任何答案的方法

然后我的服务器类有以下代码

public class Server extends Service{
    public void startListenner(){
        l.info("Listening at port "+PORT);
        while(true){
            // Waits for a client connection
            try {
                cs=ss.accept();
                l.info("Connection received: "+cs.getInetAddress()+":"+cs.getPort());

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                toClient= new DataOutputStream(cs.getOutputStream());
                PrintWriter cWriter= new PrintWriter(toClient);

                //Send a confirmation message
                cWriter.println("Message received");
                //Catch the information sent by the client
                csInput=new BufferedReader(new InputStreamReader(cs.getInputStream()));

                printData();
                toClient.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

正如您所看到的,我正在向客户发送一条消息:"收到消息"但它从未在客户端控制台中显示。怎么了?

修改

printData()方法打印从控制台

中的客户端收到的消息
public void printData(){
    l.info("Printing message received");
    try {
        System.out.println(csInput.readLine());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

不确定您的printData()方法正在做什么,但是在您打印"收到消息后,您是否错过了服务器端的cWriter.flush()" ? 据我所知,你写了你的信息,但从未发送给你的客户。