wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes String returned by SocketChannel toString() method is not same format on XP and OX-X Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "String returned by SocketChannel toString() method is not same format on XP and OX-X" Watch "String returned by SocketChannel toString() method is not same format on XP and OX-X" New topic
Author

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

Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Thanks, Rob, off to the Java Docs...
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

Definitely IPv6 addresses.
 
 
subject: String returned by SocketChannel toString() method is not same format on XP and OX-X
 
Similar Threads
Dynamic IP address and socket programming.
Accessing JSP page from network
rmi registry ip address question
RMI Server Configuration
ip addresses