2013-02-02 40 views
0

你好。知道在c#中你可以通過使用bool .pending()來檢查服務器是否有掛起的連接。但有沒有辦法在java中做到這一點? IM詢問原因我不能在網絡上找到相關的東西...感謝 iwanna它是這樣的:TcpListener.pending替代java

ServerSocket srv=new ServerSocket(port); 
if(srv.isPending())//How this can be done???? 
{ 
Socket Cl=srv.accept(); 
while(Cl.isConnected()) 
{ 
//read packet stream.......... 
} 
} 

回答

0

有在java中沒有可用這樣的方法。你必須編寫一個無限循環來等待這樣的客戶端連接。

ServerSocket srv=new ServerSocket(port); 
while(true){ 
    /*Program will wait for a new client connection 
    at this line and then proceeds further*/ 
    Socket client=srv.accept(); 
    // Rest of the code will continue here 
} 
+0

肯定有辦法......因爲在無限循環中做這件事情不是資源高效糾正我,如果我錯了。 – SteppeHawk

+0

在套接字的情況下,我不這麼認爲。服務器只是在監聽客戶端請求。 –