我在客户端有一个套接字侦听器线程,它从服务器接收数据。目前我只是从套接字获取数据输入流并从流中读取数据。我希望修改我的侦听器线程,以便阻止对该线程的处理,直到在套接字中收到数据,而不是始终检查我的输入流。
我的套接字侦听器线程在下面给出
public void run() {
listenFlag = true;
try {
//instream is a global variable
inStream = new DataInputStream(clientSocket.getInputStream());
while (listenFlag) {
messageType = -1;
if (readHeader()) {// Check for correct header
Application.getLogger().log(Level.SEVERE, "<<< passed readHeader() ----");
if (readType()) {// Check for valid Type
Application.getLogger().log(Level.SEVERE, "<<< passed readType ----");
readAndProcessMessage();// Read and process the correct message
Application.getLogger().log(Level.SEVERE, "<<< passed readAndProcessMessage ----");
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
} catch (InterruptedException e) {
Application.getLogger().log(Level.SEVERE, "Interrupted socket communication - closing socket" + e.getMessage());
} catch (InterruptedIOException e) {
Application.getLogger().log(Level.SEVERE, "Interrupted IOException socket communication - closing socket" + e.getMessage());
} catch (IOException ioe) {
Application.getLogger().log(Level.SEVERE, "Socket read error : " + ioe.getMessage());
} catch (Exception e) {
Application.getLogger().log(Level.SEVERE, "Unknown exception on SocketListenThread:"+ e.getMessage());
} finally {
Application.getLogger().log(Level.SEVERE, "SocketListenThread: listenFlag : "+ listenFlag);
Application.getLogger().log(Level.SEVERE, "Quiting SocketListnerThread Thread");
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.NOT_ALIVE;
closeInputStream();
}
}
//Read header called from above.
private boolean readHeader() throws Exception {
Application.getLogger().log(Level.INFO, "Reading header..");
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.WAITING;
int val = inStream.read();
DRISMCMStatus.socketListenerStatus = DRISMCMStatus.RUNNING;
Application.getLogger().log(Level.INFO, "[READ]: " + val);
if (val == -1) {
throw new IOException();
}
if ( val == SYNCCHAR) {
return true;
}
return false;
}