• Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket Time Out  RSS feed

 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi @ll

I try to connect to an embedded device server. At this server a tcp server is running at port 2101 and a realport server at port 771.

the last error i got was: java.net.SocketTimeoutException: Read timed out


source code:


import java.net.*;
import java.io.*;

public class TCP
{
public static void main (String[] args) throws UnknownHostException, IOException
{
Socket realport = new Socket(InetAddress.getByName("192.168.115.100").getHostName(),771);
if (realport.isConnected()) System.out.println("realport connection established");
InputStream in = realport.getInputStream();
OutputStream out = realport.getOutputStream();
// Timeout setzen
realport.setSoTimeout(10000);
char com='a';

out.write(com);
out.write('\r');
out.write('\n');
String a="read failed";
try
{
a=Integer.toString(in.read());
}
catch (Exception e)
{
System.out.println(e);
}

System.out.println(a);
realport.close();
if(realport.isClosed())System.out.println("connection closed");
}
}




output, when program is running:

realport connection established
java.net.SocketTimeoutException: Read timed out
read failed
connection closed

What did I do wrong???

Thanks, Stanzi
[ July 28, 2004: Message edited by: Constanze Michaelis ]
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not because you have set the connection to time out after 10seconds of idle time?
 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i don't set time out, nothing happens after establishing connection via the socket
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i have to say I am not too sure - it mayt be taking a long time to connect - so i would suggest increasing the time until timeout. I'd suggest adding a println statement just before the try to make sure that works. Remember to close your in and out streams too. Sorry i cant really help much more
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This belongs in our "Sockets and Internet Protocols" forum. Here, I'll move it for you.
 
Constanze Michaelis
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tom

thx for advising, but it is a hardware problem, which I hope to solve tomorrow.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably too late, but:

The SocketTimeoutException is the way the socket's InputStream tells you that the timeout expired without any data coming over the wire from the remote end. This enables you to poll the socket for new data, while doing other processing in between checking the socket. Your application seems to be running correctly - you're just not getting any data from the other end after connecting.

If you want the thread to just block until data comes in, you can set an infinite timeout by calling setSoTimeout(0).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!