2

When i convert string ("192.168.0.105") to InetAddress in java (android). I am getting "/192.168.0.105". An extra "/" is coming in InetAddress, which causes the socket not to be created.

How do i get rid of "/".

Regards,

Syed Mustehsan Ikram

4
  • 2
    it would help if you posted the code that isn't quite doing what you want Commented May 17, 2011 at 5:02
  • stackoverflow.com/questions/5571744/… Commented May 17, 2011 at 5:03
  • How do you convert? InetAddress.getByName("192.168.0.105"); ? Commented May 17, 2011 at 5:05
  • InetAddress = serverIP=Inet4Address.getByName(ip); clientSocket = new Socket(serverIP , serverPort); Commented May 17, 2011 at 5:13

2 Answers 2

7

You can use getHostAddress() method of InetAddress to get host address without /.

And if you are using InetSocketAddress then use getAddress().getHostAddress() to get host ip without /.

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());
Sign up to request clarification or add additional context in comments.

6 Comments

my problem is that i need to open Socket after getting InetAddress
@Mustehsan: in that case use java Socket to open and communicate through socket. Read this: download.oracle.com/javase/tutorial/networking/sockets/… and this: zerioh.tripod.com/ressources/sockets.html
I am currently doing it. but my problem is that as InetAddress is wrong thus socket is not being created.
I am getting following exception on socket creation java.net.SocketTimeoutException: Transport endpoint is not connected
@Mustehsan: are you sure ip is reachable?
|
2
myString = myString.replace("/", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.