2014-05-12 39 views
-1

的插座編程PrintStream.println()的替代,我不希望使用的println()方法。請給我任何println()方法的替代方法。當我使用print()或write()時,我的套接字程序沒有收到請求,也沒有發送響應。請幫幫我。如何使用PrintStream.print()在Java中

服務器代碼

in = new BufferedReader(new InputStreamReader(
socket.getInputStream())); 
out = new PrintStream(socket.getOutputStream(), true, "UTF-8"); 
s = in.readLine(); 
if (s.equals("")) { 
try { 
    socket.close(); 
    in.close(); 
    out.close(); 
    } catch (IOException e) { 
    } 
    } 
. 
. 
. 
. 
. 
. 
String b; 
if (timeInMin > 0 && timeInMin <= 600000) { 
b = "B:1"; 
out.println(b); // send data to client 
} else { 
b = "B:0"; 
out.println(b); // send data to client 
} 
try { 
} catch (InterruptedException ex) { 
} 
} 
} catch (IOException e) { 
out.println(e); 
} finally { 
try { 
        socket.close(); 
        in.close(); 
        out.close(); 
       } catch (IOException e) { 
       } 
      } 

回答

1

我懷疑你的客戶端閱讀使用readLine()方法的流。

因此,它似乎正確讀取時,服務器實際打印一行。當服務器簡單地打印時,客戶端正在等待該流完成該行,然後檢索剛纔讀取的字符串。

嘗試閱讀與客戶不同的閱讀。