Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

socket operation  RSS feed

 
Chennarao Marvatu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

how to set the timeout for the operation?
my question is i have send a request to the server and
waiting for resonse. how to come out from that and continuew my operation
if the server is not responding.

How do you prevent Java from hanging on socket = new Socket(addr, port) line if the machine on the other end is not responding?

Is this possible in Java?

Thanks, Otis
 
Premil Jacob
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chennarao,

I guess you cannot do that in one step. But you can break it up as such

int timeOut = 10;
int portNumber=80;
Socket socket = new Socket();
socket.connect(new InetSocketAddress("www.yahoo.com",portNumber),timeOut);
 
Chennarao Marvatu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for ur reply ...

in my case i am successfully connected with servre with
the socket and waiting for some response from the server
if the server is failed OR some other problem for the server
i should come out ..how this will possible
chennarao
 
Premil Jacob
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess since you are creating a TCP/IP connection , if any one of the end points breaks down the connection will be broken automatically giving you an Exception.

So if your server crashed while u were waiting for some data from the server, your connection will be broken. So you need only worry about how to handle the exception. Does that make sense???
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread belongs in our Sockets and Internet Protocols forum. I'll move it there for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!