Avatar of tricks801
tricks801

asked on 

Sockets

why does socket.getLocalAddress() put a slash in front of the adress
ex. /192.168.1.100
Java

Avatar of undefined
Last Comment
Mick Barry
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of GrandSchtroumpf
GrandSchtroumpf

because there is no defined protocol.
with a defined protcol such as ftp or http, you would get
ftp:/192.168.1.100 or http:/192.168.1.100
so without the protocol, you still keep the "/".
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> because there is no defined protocol.

An IP address doesn't have a protocol does it?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

The slash is actual used to seperate hostname and address. But in your case the name is unknown so it is blank.
note that when you define a protocol and a port,
you use something like this http:/192.168.1.100:80
you can see that the separator is ":"
so, again, if you dont specify neither the protocol nor the port, you just get "/192.168.1.100"
> An IP address doesn't have a protocol does it?
i agree, but we're not talking about an ip address here, we're talking about an instance of SocketAddress...  and its toString() method as you mentioned before
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> note that when you define a protocol and a port,

An IP address does not have a protocol or a port.

> you use something like this http:/192.168.1.100:80

That is not an IP address, 192.168.1.100 is the IP address

i was just explaining why the toString method leaves the slash.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> but we're not talking about an ip address here

I think we actually are

> we're talking about an instance of SocketAddress

Even if we were the same applies.
oops, it's not an instance of SocketAddress but an instance of java.net.InetAddress.
SocketAddress is for ServeSocket.
javadoc says about InetAddress:
"This class represents an Internet Protocol (IP) address."
which talks about "Protocol" as well as "(IP) address".
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Yes the *Internet* Protocol :) Thats what IP stands for.
> The slash is actual used to seperate hostname and address. But in your case the name is unknown so it is blank.
ok, that's fine with me.
i was reading the wrong javadoc page.  :-)
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

There are several layers of protocols. The Internet Protocol (as in IP) sits on the lower levels of the protocol stack and handles routing of packets. The TCP part of TCP/IP handles packet ordering, and connections for connection based communication.

An IPv4 address is x.y.z.w, where each x,y,z and w can hold values same as an unsigned byte. A port, however is local "routing" information, dependent on a protocol sitting on top of TCP/IP. You need both to make a connection to another computer over TCP/IP.

http, which is another protocol, but an application layer protocol, sits on top of TCP/IP, but is separate from TCP/IP.
> http, which is another protocol, but an application layer protocol, sits on top of TCP/IP, but is separate from TCP/IP.
i agree, http has nothing to do with this question.
sorry for all this mess i created.
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

No worries mate  ;-)
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> sorry for all this mess i created.

sok :)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo