Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String returned by SocketChannel toString() method is not same format on XP and OX-X

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really, really new to Java but do know C and C++. I've a project at work that needs to be written in Java so I'm trying to learn reading both a beginner book and then looking at topics I'm going to need but know nothing about.

I'v been able to use nio selectors (from an example in the Java NIO book) to make an applet which I've modified. I test it by using telnet both at home on my good Mac and at work on my XP notebook. I need to keep track of which machines connect so I decided to use toString(0 to get data on the channel and parse out the IP address and here's what I get in XP:
java.nio.channels.SocketChannel[connected local=/127.0.0.1:6666 remote=/127.0.0.1:4652]
and here's OS-X
java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1%0:6666 remote=/0:0:0:0:0:0:0:1%0:55786]

The IP address from XP is useful but whatever I'm getting from OS-X is not and I wonder what is going on?
Will this be different when I actually have another computer connect rather than use localhost? This project will be done under Linux but I don't have the machines yet. Will different flavors of Linux give different results?

Thanks
Nate


 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the API clearly specifies the format of the String returned by toStirng() you can't depend on it to be the same between different JVMs. If you need to get info on the SocketChannel's end points, use socket() to retrieve its matching Socket. You can then get the end points from that using getInetAddress() and getLocalAddress(), or getRemoteSocketAddress() and getLocalSocketAddress().
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob, off to the Java Docs...
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working under OS-X

I implemented getRemoteSocketAddress and it returned this:
/0:0:0:0:0:0:0:1%0:56575

I implemented getInetAddress and it returned this:
/0:0:0:0:0:0:0:1%0

All the methods are pretty similar results.

I guess the question remains - how is the string /0:0:0:0:0:0:0:1%0 interpreted? If I had the means to actually connect from a second computer would some of the zeros have other values?

Ah-ha, I think that this may be the IPV6 address for localhost, would you agree.

Yup that's it I think. Learned something today.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely IPv6 addresses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic