2014-07-04 52 views
0

我想通過我自己的Java客戶端連接到IRC,但有一個問題Java的IRC ping請求

每當IRC服務器發送我像PING ping請求:6E17BFF我必須回到這個PONG:6E17BFF

你可以看到我在下面使用的代碼。我做錯了什麼?

當我連接我收到此

NOTICE AUTH :*** Looking up your hostname... 
NOTICE AUTH :*** Couldn't resolve your hostname; using your IP address instead 
PING :6E17BFF 
PONG :6E17BFF 
ERROR :Closing Link: TestNick[ipaddress] (Ping timeout: 33 seconds) 

我希望你能幫助我每次

代碼

while ((line = reader.readLine()) != null) { 
     System.out.println(line); 
     if(line.substring(0,4).equals("PING")){ 
      writer.write("PONG :"+line.substring(6)+"\n\r"); 
      System.out.println("PONG :"+line.substring(6)); 
     } 
    } 
+1

無法解決您的問題,但如果您想讓生活更輕鬆,則可以使用https://code.google.com/p/pircbotx/中的功能。源代碼也在那裏提供。 – Pphoenix

+0

難道你的PONG消息不是真的被髮送嗎? [flush](http://docs.oracle.com/javase/7/docs/api/java/io/Writer.html#flush%28%29) – Anton

+0

刪除\ n \ r ..你爲什麼假設你必須把\ n \ r放在最後,順序也是\ r \ n,CR LF不是LF CR,試試\ n –

回答

0

這是我用來處理平/傍代碼在教學機器人。正如您將看到的,我將測試響應留在了原來的位置,我用來驗證過程是否正常。

//****************************************************************************** 
//PING PONG Handler and while loop test. Tests to be removed before full implementation 
//****************************************************************************** 
    while((line = in.readLine()) != null) { 
    System.out.println("<--" + line); 
     if (line.startsWith("PING")) { 
     //sendPrivmsg("#Test", "The server just PINGED!"); 
      //sendPrivmsg("#Test", "Yes, and it was answered with a PONG!"); 
      writeLine(line.replace("PING", "PONG")); 
      //sendPrivmsg("#Test", "This is a private message to channel #Test Pong Pong Pong Pong Pong"); 
     }