help with sockets...

hello everybody, i need a litle help with sockets.

when i use the constructor Socket(String host, int port, InetAddress localAddr, int localPort), what does it mean with binding the localAddr and localPort to the socket? , i asume the port must be open in the local machine....

Why do i get "address in use" when i try to make the socket in linux?

could it be that the port is closed? (it's a secure linux server)

what port should i use to connect to an external server in the internet (telnet)?

[527 byte] By [Mackleina] at [2007-11-26 18:29:54]
# 1

> when i use the constructor Socket(String host, int

> port, InetAddress localAddr, int localPort), what

> does it mean with binding the localAddr and localPort

> to the socket? , i asume the port must be open in the

> local machine....

It means the port must be free.

> Why do i get "address in use" when i try to make the

> socket in linux?

Because it isn't free.

> could it be that the port is closed? (it's a secure

> linux server)

No

> what port should i use to connect to an external

> server in the internet (telnet)?

Any port you like, unless your firewall has limitations on outgoing port numbers, the point of which has always escaped me.

If you don't know for certain that you have this problem, there's no need to use this constructor.

ejpa at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 2
If your host has only one IP address, local address an local port are irrelevant for client socket creation. Use (host, port) type of constructor.> it's a secure linux serverWhat do you mean?
hiwaa at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 3
> If your host has only one IP address, local address> an local port are irrelevant for client socket> creation. No it's not, see above.
ejpa at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 4

> > If your host has only one IP address, local

> address

> > an local port are irrelevant for client socket

> > creation.

>

> No it's not, see above.

Does what situation necessitate the use of the four argument Socket constructor?

hiwaa at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 5

> > when i use the constructor Socket(String host, int

> > port, InetAddress localAddr, int localPort), what

> > does it mean with binding the localAddr and

> localPort

> > to the socket? , i asume the port must be open in

> the

> > local machine....

>

> It means the port must be free.

>

Ok, but i what is the local address for?

> Any port you like, unless your firewall has

> limitations on outgoing port numbers, the point of

> which has always escaped me.

>

> If you don't know for certain that you have this

> problem, there's no need to use this constructor.

i used this constructor because i was getting a time out error trying to create the socket, so i wanted to use an specific local port, but now i think the machine has a firewall(not iptables) blocking almost all ports

Mackleina at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 6

The timeout error can't possibly be helped by naming the local port, so just specify zero. It might be helped by naming the local IP address but only if you've got some sort of modem. Otherwise you have a misconfigured routing table and you need to solve that problem using the usual tools: ping, telnet, traceroute, ...

For Hiwa: firewalls can be configured to limit the target IP and port but also the outgoing ports permitted. I never understood the point of the latter, it just causes more problems such as restricting the number of instances of applications you can run, but there you are. When you have such a firewall configuration, you have to use a fixed local port, and that's one of the reasons for using the 4-arg constructor. Another reason, as you pointed out, is if you have multiple NICs or multiple IP addresses somehow and you want to use a specific one for the outgoing connection (e.g. a modem).

ejpa at 2007-7-9 6:04:03 > top of Java-index,Core,Core APIs...
# 7
thank you very much for your help, i'll check for the firewall....
Mackleina at 2007-7-9 6:04:04 > top of Java-index,Core,Core APIs...
# 8

Hello again, it turns out the machine has the ports blocked, now i'm a litle confused, if my aplication connects to the internet, and also creates a proccess that comunicates with another aplication (in the same machine), then i need to open 3 ports? i mean , one for the internet comunication, one for my aplication to listen, and another for the other aplication to connect wiht my application?

or maybe is there a better way to comunicate the two local appilcations?

(excuse me for my english)

Mackleina at 2007-7-9 6:04:04 > top of Java-index,Core,Core APIs...
# 9

It depends if the other application is a server or a client. If it's a server it's just the same as whatever is at the other end of the Internet, you just create a Socket to it. If it's a client, you have to be the server, so you create a ServerSocket and accept Sockets from it.

EIther way, this only uses two ports not three, including your Internet connection.

ejpa at 2007-7-9 6:04:04 > top of Java-index,Core,Core APIs...