【问题标题】:Java Socket not sending message to the serverJava Socket不向服务器发送消息
【发布时间】:2013-04-30 11:09:03
【问题描述】:

我有以下用于向服务器发送字符串的客户端套接字。服务器没有收到消息。可能是什么问题?

public void startClient() throws IOException {

    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    InetAddress host = null;
    BufferedReader stdIn = null;

    try {
        host = InetAddress.getByName("172.16.2.97");
        socket = new Socket(host, 52000);

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

        stdIn = new BufferedReader(new InputStreamReader(System.in));
        String fromServer;
        String fromUser = null;
        //ClientHelper.unpackISO();
        fromUser = ClientHelper.createISO();
        if (fromUser != null) {
            //System.out.println("Client - " + fromUser);
            out.write(fromUser);
            System.out.println("Sent message");
        }


        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server - " + fromServer);
            if (fromUser != null) {
                //System.out.println("Client - " + fromUser);
                out.println(fromUser);
            }
        }
    } catch (ISOException ex) {
        Logger.getLogger(ClientDemo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnknownHostException e) {
        System.err.println("Cannot find the host: " + host.getHostName());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't read/write from the connection: " +            e.getMessage());
        System.exit(1);
    } finally { //Make sure we always clean up
        out.close();
        in.close();
        stdIn.close();
        socket.close();
    }
}

ClientHelper.createISO() 方法返回一个应该发送到服务器的字符串。不幸的是,服务器没有得到任何字符串。问题可能是代理设置。如果是这样怎么解决。还是我的代码有另一个问题? 我的代码有什么问题?

【问题讨论】:

  • 你有什么例外吗?可以从服务器接收吗?
  • 什么是你得到任何异常堆栈跟踪的错误。
  • 服务器怎么知道它已经收到了所有的字符串?
  • 我没有得到任何异常。我只是在服务器上没有收到任何消息

标签: java socket.io


【解决方案1】:

您必须在写入流后对其进行flush()。套接字缓冲直到你得到一个完整的数据包,否则

【讨论】:

  • 通过使用:out = new PrintWriter(socket.getOutputStream(), true); true 表示 printwriter 将被自动刷新
  • 不自​​动刷新 write() 方法....来自文档: autoFlush - 布尔值;如果为 true,println、printf 或 format 方法将刷新输出缓冲区
【解决方案2】:

检查下面的第 5 行,您需要刷新输出流。否则服务器将不会收到任何数据包,您将卡在第一个 in.readLine() 上,因为它会阻塞。

    fromUser = ClientHelper.createISO();
    if (fromUser != null) {
        //System.out.println("Client - " + fromUser);
        out.write(fromUser);
        out.flush(); // FLUSH IT HERE, packet wont be sent until you flush your stream
        System.out.println("Sent message");
    }

还可以在循环内的 out.write(fromUser) 之后添加刷新。

【讨论】:

  • 我试过了,但还是不行。此套接字仅在我将本地计算机用作服务器时才有效。但是如果是另一台机器就不行
  • 您是否在客户端程序上收到“已发送消息”消息?
  • "Sent message" 永远不会被打印为fromUser 在从控制台读取之前将始终是null。我在 try 块中看不到 if 语句的意义。您还说“我只是在服务器上没有收到任何消息”,您是否验证了客户端确实连接到服务器?另外,在out.write(fromUser);out.flush(); 之前,有out.write("\r\n")
猜你喜欢
  • 1970-01-01
  • 2013-11-10
  • 2023-04-03
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
相关资源
最近更新 更多