2011-05-17 56 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("/", ""); 
相关问题