简体   繁体   English

Java Socket Bug:从 Socket 的 InputStream 中读取字符串

[英]Java Socket Bug: Reading Strings from Socket's InputStream

I'm using a Socket to communicate with a ServerSocket.我正在使用 Socket 与 ServerSocket 进行通信。 Strings are being sent from the server to my Socket.字符串正在从服务器发送到我的套接字。 Each distinct line is a message that, when parsed, contains information.每个不同的行都是一条消息,在解析时包含信息。 To read these lines of text, a Scanner is used.要阅读这些文本行,需要使用扫描仪。

The problem is that data is coming in "spurts."问题是数据是“突飞猛进”的。 Although the Server is sending data continuously and evenly, the data read by the Scanner on the client side seems to pause, read in a bunch of messages (30-40) at once, and then pause again.虽然Server是连续均匀的发送数据,但是client端的Scanner读取的数据似乎暂停了,一下子读入了一堆消息(30-40),然后又暂停了。 It repeats this cycle indefinitely.它无限期地重复这个循环。

If I increase the rate at which data is sent, the duration of the pauses decreases;如果我提高发送数据的速率,暂停的持续时间会减少; if I slow down the data (to 1 message per second), the bug persists and the pauses become very long.如果我减慢数据速度(每秒 1 条消息),错误仍然存在并且暂停变得很长。 It's almost as if the Socket waits for its buffer to overflow before it sends any data to the Scanner;就好像 Socket 在向 Scanner 发送任何数据之前等待其缓冲区溢出一样; then flushes everything and waits for the overflow again.然后刷新所有内容并再次等待溢出。 However, if I decrease the size of the Socket's buffer, nothing changes at all.但是,如果我减小 Socket 缓冲区的大小,则根本没有任何变化。

It should be noted that I've used Scanner and Socket in this method before - from the Server side - and everything worked as desired.应该注意的是,我以前在这种方法中使用过 Scanner 和 Socket - 从服务器端 - 一切都按预期工作。 Additionally, I a) tried this with a BufferedReader like the Java Tutorials (no change in the bug) and b) printed a list of Server transmissions into a file, and read from the file in the same way, and the program worked as expected (constant rate of message reception, etc) so the problem seems to be in the Socket itself.此外,我 a) 使用像 Java 教程这样的 BufferedReader 进行了尝试(错误没有变化),b) 将服务器传输列表打印到文件中,并以相同的方式从文件中读取,程序按预期工作(消息接收的恒定速率等)所以问题似乎出在 Socket 本身。

So: How do I fix this behavior?那么:我该如何解决这种行为? I'm out of ideas atm and I really don't know what's going on.我没有想法atm,我真的不知道发生了什么。

CODE (As requested):代码(根据要求):

// In try block
// Makes the connection
Socket connection = new Socket(TARGET_MACHINE, PORT_NUMBER);
Scanner reader = new Scanner(connection.getInputStream());

// In new Thread
// In run()
while(!finished) // Boolean exit strategy
{
    if(reader.hasNextLine())
        Sring message = reader.nextLine();
}

That's how I connect and retrieve the Strings.这就是我连接和检索字符串的方式。

Also, the Strings that I am receiving are usually about 20-40 characters long.此外,我收到的字符串通常长约 20-40 个字符。

Are you maybe using readLine to read data?您是否可能使用 readLine 来读取数据?

Here is excerpt from javadoc which might be interesting to you:以下是 javadoc 的摘录,您可能会感兴趣:

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索要跳过的行的输入。

Can you attach client and server code to see what is wrong?你能附上客户端和服务器代码来看看有什么问题吗? It would be good that you use very simple byte-per-byte reader to check is the stream from server with pauses in transport, maybe the problem is with the way how you write data into server's socket... without code is hard to guess:)最好使用非常简单的逐字节读取器来检查来自服务器的 stream 传输暂停,也许问题在于您将数据写入服务器套接字的方式......没有代码很难猜测:)

Do you flush the stream every time you write to it from the server side?每次从服务器端写入 stream 时,是否都会刷新它? It could be buffering the output and not writing it to the stream until it reaches a certain threshold.它可能正在缓冲 output 并且在达到某个阈值之前不将其写入 stream。

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

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