我在解决这个简单的课程时遇到了问题。它基本上连接到IMAP服务器并读取横幅。但看完所有的字符后,它有些悬而未决。在调试器中,我可以逐步执行while循环,看到正在读取所有字符,最终它将从循环中退出。但是永远不会到达最后一个System.out.println语句。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class TestClient
{
/**
@param args
*/
public static void main( String[] args ) throws Exception
{
Socket socket = new Socket( "imap.1and1.com", 143 );
DataOutputStream os = new DataOutputStream( socket.getOutputStream() );
BufferedReader reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
StringBuilder sb = new StringBuilder();
char ch = (char) reader.read();
while ( ch != -1 ) {
sb.append( ch );
ch = (char) reader.read();
}
System.out.println( sb.toString() ); /// <--- never prints anything and program just hangs..
}
}
答案 0 :(得分:2)
如果没有要读取的数据,则读取功能块,仅在readStream关闭或套接字连接断开时才会出现。您应该做的是创建一个协议,在完全读取数据后断开连接。即你应该走出循环
在所有数据出现后以某种方式断开连接 已阅读(为此你必须确定所有数据已被阅读)
或者如果您已经阅读过一些先前同意的内容,请自行离开 “消息结束”字符串..
还有一件事,因为read()阻塞,等待更多数据,建议你应该在后台线程中读取..
答案 1 :(得分:0)
好的,这就是我为解决我的特定问题所做的。注意,使用ready()或available()方法并不总是可靠的。特别是如果服务器需要发送数据,请暂停然后发送更多数据。在我的情况下,它的工作原理是因为我不希望服务器在响应中间停止并重新启动。
代码显然没有针对实际使用进行优化。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
/**
@author Mir Shafiqul Islam <mislam@mirislam.comt>
*/
public class TestClient
{
/**
@param args
*/
public static void main( String[] args ) throws Exception
{
// Open the socket
Socket socket = new Socket( "localhost", 25143 );
// Get a buffered reader
BufferedReader reader = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
// At this point it is too early to read. So it most likely return false
System.out.println( "Buffer Reader ready? " + reader.ready());
// StringBuilder to hold the response
StringBuilder sb = new StringBuilder();
// Indicator to show if we have started to receive data or not
boolean dataStreamStarted = false;
// How many times we went to sleep waiting for data
int sleepCounter = 0;
// How many times (max) we will sleep before bailing out
int sleepMaxCounter = 50;
// Sleep max counter after data started
int sleepMaxDataCounter = 3;
// How long to sleep for each cycle
int sleepTime = 50;
// Start time
long startTime = System.currentTimeMillis();
// This is a tight loop. Not sure what it will do to CPU
while( true ) {
if ( reader.ready() )
{
sb.append( (char) reader.read() );
// Once started we do not expect server to stop in the middle and restart
dataStreamStarted = true;
} else {
Thread.sleep( sleepTime );
if ( dataStreamStarted && (sleepCounter >= sleepMaxDataCounter) ) {
System.out.println( "Reached max sleep time of " + (sleepMaxDataCounter*sleepTime) + " ms after data started" );
break;
} else {
if (sleepCounter >= sleepMaxCounter) {
System.out.println( "Reached max sleep time of " + (sleepMaxCounter*sleepTime) + " ms. Bailing out" );
// Reached max timeout waiting for data. Bail..
break;
}
}
sleepCounter++;
}
}
long endTime = System.currentTimeMillis();
System.out.println( sb.toString() );
System.out.println( "Time " + (endTime-startTime));
}
}