Re: [問題] 請問關於Socket

看板java作者 (板橋是個好地方)時間15年前 (2010/12/17 20:07), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《polomoss (小澤)》之銘言: : 標題: [問題] 請問關於Socket : 時間: Mon Dec 13 01:34:23 2010 : getInetAddress : public InetAddress getInetAddress() : Returns the address to which the socket is connected. : Returns: : the remote IP address to which this socket is connected, or null if the : socket is not connected. : 為什麼我使用上面那個函數, : 回傳值是/127.0.0.1 : 就是為什麼會多一個"/" : 我從API裡面沒看出來有敘述? : ps.要把"/"處理掉很容易,只是想知道原因 : 還是跟IDE有關係呢? : >如果問的問題很蠢請見諒,沒有goo到...< : -- : ※ 發信站: 批踢踢實業坊(ptt.cc) : ◆ From: 220.136.104.228 : → sbrhsieh:回傳值是 InetAddress,並沒有說是 IP "字串" 12/13 01:43 : → sbrhsieh:請再去看看 InetAddress API doc. 12/13 01:45 : : **找到這個→The string returned is of the form: hostname / literal IP address. : : 是因為InetAddress覆寫的toString()把ip加上/輸出嗎? yes : 剛測試了其他,也都會帶"/" : System.out.println(client.getInetAddress()); : System.out.println(client.getLocalAddress()); : System.out.println(client.getLocalPort()); : System.out.println(client.getLocalSocketAddress()); : System.out.println(client.getRemoteSocketAddress()); 因為你這幾行實際上都是呼叫 println(Object x) = println(x.toString()) : : 另外想問,getLocalPort()這個回傳的port : 是用在哪裡的? 看本地端是用哪個 port 來連接這個 socket : 最後謝謝大大回答, : 好久沒寫了,怎麼記得以前回傳ip不會帶"/" InetAddress ia = InetAddress.getLocalHost(); System.out.println(ia.getHostAddress()); : >< : ※ 編輯: polomoss 來自: 220.136.104.228 (12/13 02:00) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.33
文章代碼(AID): #1D2rAN3J (java)
討論串 (同標題文章)
文章代碼(AID): #1D2rAN3J (java)