This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Socket connection problem, need a hand.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket connection problem, need a hand.." Watch "Socket connection problem, need a hand.." New topic
Author

Socket connection problem, need a hand..

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Hi:
In my app, I am having problem connecting via the socket, here is a portion of my code



basically i tried a couple of values of timeout, i tried to make timeout bigger, but that didn't help , apparently....I kept on getting
java.net.ConnectException: Connection refused: connect


I am wondering if i could call Socket.bind() before connect?

Or I should just try

Socket socket = new Socket(host, port) ;
instead of Socekt socket = new Socket();

Please let me know.

Many thanks
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Kay lin,

I suspect The ServerSocket(this.port) Is not listening.

try this to establish ServerSocket to listen:

ServerSocket ss = new ServerSocket(this.port);
while(true){
Socket s = ss.accept();
// deal with socket streams process.
}


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11145
    
  16

I'm going to move this to our forum devoted to sockets.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Hi,
so you are suggesting the server port is not listening?

could i try the following then



it is ok if i call bind() before accept()right?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Kay lin,

Ensure That the ServerSocket program runs first.

Then run your client program seperatly.
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
Ah!!!
it turns out that I cann't call bind() before accept, it throws BindException..damn..

The thing is my server is running on another box...

so if I just try ServerSocket ss = new ServerScoket(port);

what if something is running on the same port locally...?? well..i dont have anything running locally..so i am gonna try this one anyways..

sigh **

I need help!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The bind exception on the ServerSocket means that port is in use. You can start your server socket on another unused port and connect your client to that new port number.

To run a server and client on the same machine they'll need to be in separate threads or processes started from the OS. Outside your IDE you can open two command windows and say "java MyServer" in one and "java MyClient" in the other. Even in an IDE like Eclipse you can start the server's main() then the client's main().

Before you do all that, try a completely bogus port number against the server running on the other machine. See if the error message is the same or different.

Have you run through the Sun Networking Tutorial? It steps you through building a little client and server and might answer some questions for you.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
here is the thing though.
the server is starting at port 3000 on another host, a separate box..
i don't think anythign else is using port 3000...if it is , it shouldn't.

I just did netstat -a | grep 3000, and it is only my server that is on port 3000


Here is the Server portion of the code that is running on the server box


So, what should the client portion of the code be ? shouldn't it be something like this



My question is , should i call socket.connect() at all??
because in the tutorial http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
they did not call the connect on the client..they just get the IO streams directly..

Please let me know what I am doing wrong...

All helps are greatly appreciated...!!
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
ok, i ran the app with socket.connect(addr,port)

i got the following error

java.net.SocketException: already connected
at java.net.Socket.connect(Socket.java:497)



So does that mean that connect is reduandent???
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

If you look at the API documentation for Socket, you'll see there are a number of constructors. The no-argument one creates an unconnected socket. If one used this constructor, one would later need to call connect. The constructor you are using is documented as "Creates a stream socket and connects it to the specified port number on the named host", so yes, calling connect() is redundant.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
seems to be fine now..
thanks guys

REALLY APPRECIATE IT..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket connection problem, need a hand..
 
Similar Threads
Need a simple Java LDAP client
how to set timeout to throw an exception if the server is down
Socket connect timed out and connection refuse
FTP client not working behind proxy
problem streaming with mms:// protocol