Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Socket Constructor

Reply
Thread Tools

Java Socket Constructor

 
 
Terracotta
Guest
Posts: n/a
 
      01-18-2008
Hi All:

We all know that "Socket(String host, int port)" create a client
socket which connecting to
the target host : port

however, which local port does it connect from? I guess it must be a
random port from list of
available ports. but how can we find out which port is currently been
used?

I thought another constructor Socket(InetAddress address, int port,
InetAddress localAddr, int localPort)
might help. but the following code :

" Socket connection = new Socket("www.google.com", 80,
InetAddress.getByName("localhost"), 0);"

doesn't work either. can anyone spot the problem please
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      01-18-2008
Terracotta <(E-Mail Removed)> wrote:
> I thought another constructor
> Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
> might help. but the following code :
> Socket connection = new Socket("www.google.com", 80,
> InetAddress.getByName("localhost"), 0);
> doesn't work either. can anyone spot the problem please


That may be a problem beyond java:
localhost is typically 127.0.0.1, which is the "loopback" address.

It's like you were giving out visit-cards with your name
written as "me", and address given as "my town". You
can't expect to receive any answers that way.

Have you tried using the (seemingly synonymous)
InetAddress.getLocalHost() ? It's docu is not very
clear, but it seems like it would return the loopback-
device only as a fallback under special circumstances,
so it may look like it would return something more
sensible, normally.

 
Reply With Quote
 
 
 
 
Carl
Guest
Posts: n/a
 
      01-18-2008
Terracotta <(E-Mail Removed)> writes:

> Hi All:
>
> We all know that "Socket(String host, int port)" create a client
> socket which connecting to
> the target host : port
>
> however, which local port does it connect from? I guess it must be a
> random port from list of
> available ports. but how can we find out which port is currently been
> used?
>
> I thought another constructor Socket(InetAddress address, int port,
> InetAddress localAddr, int localPort)
> might help. but the following code :
>
> " Socket connection = new Socket("www.google.com", 80,
> InetAddress.getByName("localhost"), 0);"
>
> doesn't work either. can anyone spot the problem please


For starters, are you really expecting to bind at local port 0?
I'd suggest you try a more sane number, as I'd suspect that passing a zero
here causes the auto-assignment of the local port to take place.

Just out of curiousity, why do you want to specify the local port?
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      01-18-2008
Terracotta wrote:
> Hi All:
>
> We all know that "Socket(String host, int port)" create a client
> socket which connecting to
> the target host : port
>
> however, which local port does it connect from? I guess it must be a
> random port from list of
> available ports. but how can we find out which port is currently been
> used?


connection.getLocalPort();
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      01-18-2008
On Fri, 18 Jan 2008 10:05:07 -0800, Terracotta <(E-Mail Removed)>
wrote:

> [...]
> however, which local port does it connect from? I guess it must be a
> random port from list of
> available ports. but how can we find out which port is currently been
> used?


I'm pretty new to Java and haven't used sockets in Java yet. So I could
be wrong about this. But in other socket implementations (BSD, Winsock,
..NET Socket, etc.) once you've bound the socket, you can query the socket
to find out the actual address and port that was used.

It looks to me as though the Socket.getLocalPort() function is how you'd
do this in Java. Have you tried that? (After you've bound the socket to
port 0, of course).

Pete
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      01-19-2008
Terracotta wrote:
> We all know that "Socket(String host, int port)" create a client
> socket which connecting to
> the target host : port
>
> however, which local port does it connect from? I guess it must be a
> random port from list of
> available ports. but how can we find out which port is currently been
> used?


Mark S has already give you the method to get the port.

But what do you need it for ?

The other end can just write to the already opened connection and
they can not even connect to that port you are looking for ?

Arne
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      01-19-2008
Arne Vajh°j wrote:

> But what do you need it for ?
>
> The other end can just write to the already opened connection and
> they can not even connect to that port you are looking for ?


Sometimes it's convenient to provide the port number with out exposing
the Socket itself, or to stuff the port number into the payload/data so
a higher level layer on the other end can retrieve it without having a
low level API exposed.

Those are some reasons why I'd do it. I'll let the OP explain his own
though.
 
Reply With Quote
 
EJP
Guest
Posts: n/a
 
      01-28-2008
Andreas Leitgeb wrote:
> localhost is typically 127.0.0.1, which is the "loopback" address.


Only on certain misconfigured Linux distributions. 'localhost' should be
the first non-loopback IP address, not the loopback address.

> Have you tried using the (seemingly synonymous)
> InetAddress.getLocalHost() ?


Better still, have you tried 'null'? This always works.

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      01-28-2008
EJP <(E-Mail Removed)> wrote:
> Andreas Leitgeb wrote:
>> localhost is typically 127.0.0.1, which is the "loopback" address.

> Only on certain misconfigured Linux distributions. 'localhost' should be
> the first non-loopback IP address, not the loopback address.


All the machines within my reach (that is: where I do have an account
on, most of them administered by professionals) which are linux, solaris,
and one NetBSD, have localhost as 127.0.0.1, (the NetBSD machine has "::1")

I think, you mixed that up with the host's name (like "mypc") resolving to
127.0.0.1, which indeed was a common misconfiguration on many linux
installations (mostly those that were installed as standalone machines,
without even an ether-card). Other than home-machines with linux, unix-
machines with no net at all are probably quite rare. So much for the
correlation with linux).

>> Have you tried using the (seemingly synonymous)
>> InetAddress.getLocalHost() ?

> Better still, have you tried 'null'? This always works.


I didn't see it documented anywhere. There is no mention of what happens
for null InetAddress' in the javadoc for java.net.Socket. So, even if it
appeared to work now and here, how can I know that it will still work in a
different JVM, or even a future version of sun's JVM?

On further scanning of the javadoc-page, I saw, that behaviour for "null"
is defined only for those constructors taking String arguments for addresses.
So, this is not applicable to specifying the source-address, which is
never given as String. InetAddress.getByName(null), which is used for
null-String addresses, specifically gives the address of the *loopback-device*,
so even if that was also used for null-InetAddresses, then it would still
be the wrong address.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-28-2008
Andreas Leitgeb wrote:
> EJP <(E-Mail Removed)> wrote:
>> Andreas Leitgeb wrote:
>>> localhost is typically 127.0.0.1, which is the "loopback" address.

>> Only on certain misconfigured Linux distributions. 'localhost' should be
>> the first non-loopback IP address, not the loopback address.

>
> All the machines within my reach (that is: where I do have an account
> on, most of them administered by professionals) which are linux, solaris,
> and one NetBSD, have localhost as 127.0.0.1, (the NetBSD machine has "::1")
>
> I think, you mixed that up with the host's name (like "mypc") resolving to
> 127.0.0.1, which indeed was a common misconfiguration on many linux
> installations (mostly those that were installed as standalone machines,
> without even an ether-card). Other than home-machines with linux, unix-
> machines with no net at all are probably quite rare. So much for the
> correlation with linux).


FWIW, my experience correlates with Andreas's. A quick google for
> linux configuring localhost address

finds:
<http://www.redhat.com/docs/manuals/csgfs/browse/rh-cs-en/s1-hardware-linux.html>
as the first link, in which the gurus state (ss. 2.4.1):
> 127.0.0.1 localhost.localdomain localhost

as the canonical /etc/hosts file's first line.

Likewise, from "Configuring the Domain Name System DNS"
<http://www.linux-tutorial.info/modules.php?name=MContent&pageid=148>:
> We also need a mapping for the node "localhost".
> This is a special name for the local machine and is accessed using a
> special driver called the "loopback driver". Rather than accessing the card,
> the loopback driver knows that this is the local machine and does not need
> to go out to the network card. Certain function [sic] on the system take advantage
> of the capabilities of this driver.
>
> localhost IN A 127.0.0.1


<http://www.debian.org/doc/manuals/network-administrator/ch-bind.html>
> This allows mapping to and from your localhost which is 127.0.0.1.


<http://www.oracle.com/technology/tech/php/htdocs/inst_php_apache_linux.html>
> You may need to replace "localhost" with the IP address 127.0.0.1
> or your machine's DNS name if you are behind a firewall
> or if localhost does not resolve for some other reason.


<http://faqs.org/faqs/linux/faq/part4/>
> Caution: Do not change the "localhost" entry in /etc/hosts, because
> many programs depend on it for internal message-passing.


bash $ man hosts
....
> EXAMPLE
> 127.0.0.1 localhost


--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM



Advertisments