简体   繁体   English

使用writeBytes的Java客户端套接字

[英]Java client socket using writeBytes

I'm reading a string from a buffer and writing it to a server. 我正在从缓冲区读取字符串并将其写入服务器。 The problem I'm having is that the string never gets received by the server when I leave the socket open and write in a loop. 我遇到的问题是,当我保持套接字打开并在循环中写入时,该字符串永远不会被服务器接收。 When I use this: 当我使用这个:

    try {       
        Socket send = new Socket("localhost", 1490);
        DataOutputStream out = new DataOutputStream(send.getOutputStream());
        String message = null;
        while ((message = buffer.get()) != null){
            out.writeBytes(message);
        }
        out.close();
        send.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

the server doesn't receive the string, but when I do this it works properly: 服务器没有收到字符串,但是当我这样做时,它可以正常工作:

    try {       

        String message = null;
        while ((message = buffer.get()) != null){
            Socket send = new Socket("localhost", 1490);
            DataOutputStream out = new DataOutputStream(send.getOutputStream());
                    out.writeBytes(message);
            out.close();
            send.close();
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Obviously I don't want to keep opening and closing the socket, though. 显然,我不想一直打开和关闭套接字。 What is the problem? 问题是什么?

You need to flush your socket every time you want to send a data packet. 每次要发送数据包时,都需要刷新套接字。 Closing a socket forces an automatic flush and that explains why your data is getting sent on socket close. 关闭套接字会强制执行自动刷新,这说明了为什么在套接字关闭时发送数据的原因。

The data is not being written to the socket even when you close it? 即使关闭数据也不会将其写入套接字? (in your first snippet that is) (在您的第一个片段中)

Also, have you tried to use the flush method? 另外,您是否尝试过使用flush方法? You can read about it here: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html#flush () and your code will look like: 您可以在这里阅读有关它的信息: http : //docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html#flush (),您的代码如下所示:

try {       
    Socket send = new Socket("localhost", 1490);
    DataOutputStream out = new DataOutputStream(send.getOutputStream());
    String message = null;
    while ((message = buffer.get()) != null){
        out.writeBytes(message);
        out.flush();
    }
    out.close();
    send.close();
} catch (IOException ex) {
    ex.printStackTrace();
}

Let me make a guess. 让我猜一猜。

Does the buffer.get() method block? buffer.get()方法会阻塞吗? If so, then the problem is that out.writeBytes(message) does not guarantee that the entire byte representation to be pushed to the server. 如果是这样,那么问题就在于out.writeBytes(message)不能保证将整个字节表示形式推送到服务器。 Instead. 代替。 there is a good chance that your client has buffered bytes waiting to be flushed through to the server. 您的客户端很有可能已经缓冲了等待刷新到服务器的字节。

If this is what is going on, then calling flush after each call to writeBytes will fix the problem. 如果是这种情况,那么在每次调用writeBytes之后调用flush都可以解决此问题。

But if the buffer.get() method doesn't block, then calling flush won't make any difference. 但是,如果buffer.get()方法没有阻塞,则调用flush不会有任何区别。 In fact, it will just increase the network traffic. 实际上,这只会增加网络流量。 So adding the flush "just in case" is a bad idea. 因此,“以防万一”添加冲洗功能是一个坏主意。


Another possibility is that there is something wrong with the server-side code. 另一种可能性是服务器端代码有问题。

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

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