• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket connection problem, need a hand..

 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to our forum devoted to sockets.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kay lin,

Ensure That the ServerSocket program runs first.

Then run your client program seperatly.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to be fine now..
thanks guys

REALLY APPRECIATE IT..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic