This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Unable to create a Socket object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Unable to create a Socket object" Watch "Unable to create a Socket object" New topic
Author

Unable to create a Socket object

Ganesh Hegde
Greenhorn

Joined: Apr 09, 2008
Posts: 12
Hi,

I was trying to create a socket object using the following code:


But when i run the compiled bytecode (class file) the machine throws an exception with the following stack trace:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ChatServer.main(ChatServer.java:9)


I am running this on a machine that runs the 'Windows Vista Starter' edition. The jvm version number is 1.5.0_15.

I even tried running the above code after disabling both the windows firewall and Norton Antivirus's firewall. But to no avail.


You die but your fame lives...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Is there a server running on that same machine, listening to port 5000?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ganesh Hegde
Greenhorn

Joined: Apr 09, 2008
Posts: 12
Hi Rob,

As far as I know, no server is listening to port 5000. I also tried using other ports like 10008, 2048, 65535. Nothing seems to make it work!

Warm regards,

Gaura
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

You need a server/service/daemon bound to a port in order to connect a socket to it.
Since the name of your class is "ChatServer", I think you are trying to bind to a port in order to receive connections. You do this by creating an instance of ServerSocket.
Have a look at the Java Tutorial on Custom Networking. A little study beforehand will save you a lot of grief down the road.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ganesh Hegde
Greenhorn

Joined: Apr 09, 2008
Posts: 12
Hi Joe,

Your suggestion worked! I was not aware that I had to start a server at the specified port before I could even instantiate a Socket object :-). Here's what I did after I saw your reply.

1. I changed the code of the class ChatServer to this


2. I wrote a small piece of code that would play the client:


Then I opened a terminal and made the jvm execute the bytecode for 'ChatServer' class. I opened another terminal and invoked jvm to execute the bytecode for 'ChatClient' class. And I met with 'Success!'.

Of course the program doesn't do anything useful. But I always start exploring by starting in a small way. I will now build on the above code so that it will do something more useful.

Very kind of you!

Warm regards,

Gaura
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to create a Socket object
 
Similar Threads
Simulating browser in a application
java.net.ConnectException: Connection timed out: connect
Enforcing custom policies using policytool - reg.
Exception while running applets
RMI example code fails