• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting the socket timeout?  RSS feed

 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to be able to set the timeout for how long the socket code below waits (tries) to connect to the remote SMTP server.

If I turn the modem off so there's no way to connect to the remote SMTP sever, this code still takes about 10 seconds to return an IOException.

I've tried it with and without the setTcpNoDelay(true) and the setSoTimeout(1).

Is it possible to set the timeout so the code returns more quickly or is this a blocking issue of some kind?

Thanks in advance.

-- Mike


Socket s = new Socket();
s.setTcpNoDelay(true);
s.setSoTimeout(1); // ms

s = new Socket("mail.somedomain.com", 25);

.
.
.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating two different Socket instances. You creat an unconnected socket instance, set the timeout and delay, then replace it with a connected socket instance with the default timeout and delay. You want to invoke connect() on your first socket instance to connect using the timeout and delay values you've specified.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this topic to the "sockets and protocols" forum...
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried to add the connect method call as you suggested in your reply (I'm probably doing it incorrectly), I get an IOException everytime. If I commment it out as below, I either get SMTP Server is alive (if the modem is on) or SMTP server is not alive (based on my messages) if the modem is off.

Is this code below adequate (basic code logic, that is)? I'm not sure I understood what you were getting at in your last reply to get the timeout working. The code below does not timeout as I expect.

Thanks again very much in advance!!!

Mike

try
{
Socket s = new Socket("mail.somedomain.com", 25);

s.setTcpNoDelay(true);
s.setSoTimeout(1); // ms
// s.connect(s.getRemoteSocketAddress());

if ( s.isConnected() )
{
.
.
.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is correct behavior. You are unclear on the state of the socket. This code:

creates a socket and attempts to connect it to a server. You can't change the state of the socket after that point (i.e. setting the timeout) because the constructor won't return until it is connected or an exception is thrown.
This code:

Socket s = new Socket();

creates a socket which is not connected. You can freely manipulate the settings of this instance and connect to the server when you are done configuring it.
Your current code:

Attempts to connect to the same server twice, triggering an exception.
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I got it. I was just trying to add the connect() method after your previous post. But, that's redundant since the new Socket("..."); does the same thing.

Thanks very much!!!

I appreciate your help.

-Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!