wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Connection refused, Socket Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Connection refused, Socket Help" Watch "Connection refused, Socket Help" New topic
Author

Connection refused, Socket Help

Benjamin Rohrer
Greenhorn

Joined: Jun 01, 2011
Posts: 7
I apologize in advance if this has already been asked, im sure it has. But I've been searching and cant get any answers. I'm learning how to make simple Client/Server Connections using Sockets and ServerSockets. I started out with a client and a server both running on the same machine, the IP address the client was using was the local "127.0.0.1" loopback address. However, when I ran the server from my desktop, and the client from my laptop , it did not work. The first time I used my external IP address and I received a connection timed out error. So I changed the IP address in the client to the internal "192. etc." and this time received a connection refused error. I do have a firewall, however i've changed the settings on both machines to allow connections from the appropriate java programs.

I'm starting to think that my problem may be my router? As in, i need to change a setting in my router to allow the connection, but I honestly have no idea. If i need to post the code i will, its very short. But i think I may just have a problem with the addresses. Let me know. Thanks in advance.

Ben
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Benjamin,

the reason for timeout errors is most probably your firewall silently dropping packets. It's hard to tell what exactly is the problem without knowing more about your firewall and network setup.

A connection refused error usually means that the server socket (protocol+IP+port) is not opened and listening for incoming connections. On Windows and UNIX/Linux you can use the "netstat" utility to check for open sockets. If there's no socket in listening mode for the address your client tries to connect to the problem is on the server side. If that's not the case the problem could indeed be your router/firewall. But here again it's hard give better advices without having more details.

Marco

P.S: Welcome to JavaRanch
Benjamin Rohrer
Greenhorn

Joined: Jun 01, 2011
Posts: 7
Thanks Marco. I appreciate it.

I was actually typing out a plethora of more questions for you when I got the client to connect! Unfortunately, I still have quite a few questions if you don't mind. The problem was the address I typed into the client. Which address am I supposed to use? My router has an IPv4 address, and my computer has an IPv4 address as well as an IPv6 address. I'm assuming I should use the IPv4 address of my computer, as this is the one that worked, but is this true in all cases? Also, will I be able to use the same IP address for computer's outside of my Home Network?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

For now use an IPv4 ip-address. The ip that is assigned to your computer is most likely assigned by an DHCP server and isn't reachable from the outside world.
Search for port forwarding when you need your app to be reachable from the outside world.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Benjamin Rohrer
Greenhorn

Joined: Jun 01, 2011
Posts: 7
Thanks Wouter. I'm sure that time's approaching, i'll soon be wanting to go outside of my own network and I'm sure I'll have questions about port forwarding. Look for me then! Thanks for the help.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Benjamin,

as Wouter already pointed out it's probably easier to test with the IPv4 address, although it should work with the IPv6 address too, if you're client, server and router are IPv6 enabled.

The default setup of most home routers/firewalls is to allow all outgoing traffic from your LAN to the internet and to block all traffic from the outside except the packets that are answers to your requests coming from the LAN inside. That means by default all internal address are not reachable from the public network (usually the internet). For this you have to configure port forwarding (as Wouter pointed out, too) which tells the firewall to forward traffic arriving from the outside for a specific port to an internal machine which should be the one with the server side of your application. Just be aware that this _COULD_ be a security problem if someone manages to misuse your server application

Marco


Benjamin Rohrer
Greenhorn

Joined: Jun 01, 2011
Posts: 7
Thanks Marco. I'll start on the port forwarding soon. Will there be a security risk if i don't distribute the sever side of the application? As in, I'm only ever going to have one server program running from one of my computers.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Benjamin,

connecting any computer to any network is always a potential security problem. To be really safe you should cut your network cable and turn off your computer

Seriously, if you are connected to the internet and you expose a server application to the whole internet someone could use your server application to attack your computer. But I think you don't have to worry too much about that. Most attacks are launched by script kiddies who use existing exploits for well-known server applications like web servers, mail servers etc. It's unlikely (but not impossible) that someone tries to attack your private computer and manages to find a security whole inside your self-written server application.

Besides that potential problem it's probably easier to experiment inside your private LAN so that you don't have to deal with routing or firewalls issues. Of course there's no other way than to expose your server to the internet if you want to use it from the internet later.

Marco
Benjamin Rohrer
Greenhorn

Joined: Jun 01, 2011
Posts: 7
Thanks a lot, that makes sense. Now that you mention it, i probably will stick to my home network (especially since they are technically my parents computers!). But I'll be sure to ask if I try anything beyond this. Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection refused, Socket Help
 
Similar Threads
trying to connect to another ip adress
client/server/etc. question
SocketException
connecting to RMI server through a router, problem.
Strange problem in RMI