2011-05-17 105 views
1

當我將字符串("192.168.0.105")轉換爲java(android)中的InetAddress。我得到"/192.168.0.105"。一個額外的"/"即將在InetAddress,這導致套接字不被創建。將字符串轉換爲IP地址(android)

我該如何擺脫「/」。

問候,

賽義德Mustehsan伊克拉姆

+2

它會幫助,如果你發佈代碼是不是很想做你想要的 – 2011-05-17 05:02:41

+0

你如何轉換? 'InetAddress.getByName(「192.168.0.105」);'? – 2011-05-17 05:05:57

+0

InetAddress = serverIP = Inet4Address.getByName(ip); clientSocket = new Socket(serverIP,serverPort); – 2011-05-17 05:13:28

回答

6

您可以使用InetAddressgetHostAddress()方法,無需/獲取主機地址。

如果您使用的是InetSocketAddress,則使用getAddress().getHostAddress()來獲取主機ip,而不使用/

InetAddress inetAddress = InetAddress.getByName("192.168.0.105"); 
System.out.println(inetAddress.getHostAddress()); 

InetSocketAddress address = new InetSocketAddress("192.168.0.105", 5555); 
System.out.println(address.getAddress().getHostAddress()); 
+0

我的問題是,我需要在獲得InetAddress後打開Socket Socket @ – 2011-05-17 05:18:36

+0

@Mustehsan:在這種情況下使用Java Socket來打開並通過套接字進行通信。閱讀此:http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html和這個:http://zerioh.tripod.com/ressources/sockets.html – 2011-05-17 05:23:21

+0

我目前正在做它。但我的問題是,因爲InetAddress是錯誤的,因此套接字未被創建。 – 2011-05-17 05:59:40

2
myString = myString.replace("/", "");