• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Why won't my socket connect?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a program from another developer that just opens a very simple socket. It won't work on my machine (running Windows XP pro), but works fine on his and another developers machine (running win2k). I'm not sure if it is the OS that is the problem, but that is about the only difference we can think of.

I've tried a number of different combinations to create the socket:
InetAddress host = InetAddress.getLocalHost();
try {
sock = new Socket(host, port);
} catch(ConnectException ce){
...
many different port #'s and host combinations (such as IP, "localhost"), so I don't think I am using a port that is already in use (tried 3 random ones off the top of my head)

I always get a connection refused exception. Are there any settings to check? Is there anything that could give me more error info than just connection refused.

thank you
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your firewall turned on?
 
N Goldsmith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't configured a firewall on this machine. Actually, it is a hand me down from another developer. Where should I check if something was already setup?

I did change it to a ServerSocket and it opened it fine.

//sock = new Socket(host, port);
ss = new ServerSocket(port);

no idea why that would work though.
 
N Goldsmith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just checked if the Internet Connection Firewall is enabled and it was not. I don't have any other firewalls installed. I am on a VPN, but I disconnected to test if that was the cause and it wouldn't connect either way.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short lesson in the socket abstraction:

A ServerSocket "listens" on a specific port on the local machine. You can listen on any port you'd like, and unless some other process on the same machine is already listening on that same port (or unless it's a UNIX machine and the port number is less than 1024 and you're not root) it will succeed.

A Socket, on the other hand "contacts" a specific port on a remote machine. If there's no ServerSocket listening on the specified port on the remote machine, you'll see a "connection refused" error.

So in any event, it appears that on your friend's machine, there's a server process listening on the port that he used, and the code you show below successfully contacts that local server socket. But now you take the same code and run it on your own machine, it tries to contact a server process at the given port on your machine, and it fails, as you haven't got that server process running.

So what number is "port" in the original?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic