【问题标题】:How to: Java Server Socket response and Client reading如何:Java 服务器套接字响应和客户端读取
【发布时间】:2017-05-23 16:06:44
【问题描述】:

想象下一个案例:

客户端-服务器连接

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

类客户:

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

【问题讨论】:

  • 这个帖子在SO上问了很多次,你有没有尝试检查相关帖子?
  • 是的,我在写这篇文章之前做了一些研究,但没有帮助我。我真的在考虑它,创建一个被问了这么多的线程的事实,但正如我所说,我没有在我的代码中找到错误的东西。我很抱歉这个不必要的线程。

标签: java sockets client-server


【解决方案1】:

不确定您的 printData() 方法在做什么,但您是否在打印“收到消息”后在服务器端丢失了 cWriter.flush() ? 据我了解,您写了您的信息,但从不将其发送给您的客户。

【讨论】:

  • 你是对的,我没有发送任何东西,因为我忘了添加flush
  • 太好了,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多