简体   繁体   English

Java套接字未向服务器发送消息

[英]Java Socket not sending message to the server

I have the following client socket for sending a string to the server. 我有以下用于将字符串发送到服务器的客户端套接字。 The server is not getting message. 服务器未收到消息。 What could be the prob;em? 可能是什么问题?

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

The method ClientHelper.createISO() return a string which is supposed to be sent to the server. 方法ClientHelper.createISO()返回应该发送给服务器的字符串。 Unfortunately the server is not getting any string. 不幸的是服务器没有得到任何字符串。 Could the problem be proxy settings. 问题可能出在代理设置上。 If so how can solve it. 如果是这样,怎么解决呢。 Or is it another problem with my code? 还是我的代码有另一个问题? What is the problem with my code? 我的代码有什么问题?

You must flush() the stream after writing to it. 写入流后,必须冲洗该流。 Sockets buffer until you get a full packet otherwise 套接字缓冲区,直到获得完整的数据包为止

Check the 5th line below, you need to flush your output stream. 检查下面的第5行,您需要刷新输出流。 Otherwise server will not get any packet and you will stuck on your first in.readLine() because its blocking. 否则,服务器将不会获得任何数据包,并且由于阻塞,您将停留在第一个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");
    }

Also add flush after your out.write(fromUser) inside the loop. 还要在循环内的out.write(fromUser)之后添加flush。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM