aspose file tools
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 Storm Applied this week in the Other Open Source APIs 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: 19972
    
  25

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 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: 9083
    
    9

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.


[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