-1

I have below code which throws SocketTimeoutException.

Socket socket = new Socket(localhost, 9978);
socket.setSoTimeout(10000);
OutputStream oStream= socket.getOutputStream();
byte[] data = new byte[] {'h', 'e', 'l', 'l', 'o'};
oStream.write(data);
oStream.flush();
DataInputStream iStream = new DataInputStream(socket.getInputStream());
final byte[] received = new byte[data.length];
data.readFully(received);

At readFully, I get SocketTimeoutException. So, certainly i have wrong code but I am not sure what.

Thanks for the help.

Arjun Patel
  • 345
  • 6
  • 22
  • How is the variable `localhost` declared and initialized? Where is the code for the server process that's listening on 9978? How do you know it's returning the same number of bytes as you sent? This question is rather incomplete. – Jim Garrison Dec 18 '13 at 00:56

2 Answers2

1

You don't necessarily have 'wrong code'; maybe you have a slow network connection, or you're talking to a slow server. Try a longer timeout. Or maybe the server isn't sending you five bytes in reply.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

How do you know that the server is sending you 5 bytes in return?

Maybe try using a normal read and see what happens.

Even a very slow server / network should be able to do 5 bytes in 10 seconds.

try this code

for (int x = 0; x < 5; x++) {
  byte[] received = new byte[1];
  data.readFully(received);
  System.out.println (new String (received));
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • @ArjunPatel In that case maybe the server isn't sending anything at all, or it sends single bytes at intervals longer than 10 seconds. What was the value of `i` when the exception was thrown? or if that isn't convenient, how many times if any was a byte displayed? – user207421 Dec 18 '13 at 00:45
  • @user2310289 I assume you meant value of x. It was 0. I think the readFully is blocking call. When i looked the implementation of org.apache.harmony.luni.net.PlainSocketImpl it says – Arjun Patel Dec 18 '13 at 00:50
  • int read(byte[] buffer, int offset, int count) throws IOException { if (shutdownInput) { return -1; } int read = netImpl.read(fd, buffer, offset, count); // Return of zero bytes for a blocking socket means a timeout occurred if (read == 0) { throw new SocketTimeoutException(); } // Return of -1 indicates the peer was closed if (read == -1) { shutdownInput = true; } return read; } – Arjun Patel Dec 18 '13 at 00:50
  • @ArjunPatel Of course it's a blocking call. All `java.io` read methods are blocking calls. If the server doesn't send you five bytes within ten seconds, you will get this exception. It isn't, so you are. I can't see any evidence that you're actually *using* `org.apache.harmony.luni.net.PlainSocketImpl` here, but if you are somehow you should have stated it in your question. In Java, and C, 'return of zero for a blocking socket' does ***not*** mean a timeout occurred. It means you provided a zero length. This doesn't have any relevance. The point is, the server isn't sending anything. – user207421 Dec 18 '13 at 00:53
  • 1
    @EJP Thanks for your comments. Arjun, to re-iterate what EJP is saying - If you are not getting any output from the above code, then the server is not sending you any data period. – Scary Wombat Dec 18 '13 at 00:56
  • @EJP thanks for the insight. But as you can see I am writing on that socket 5 bytes, and trying to read from it. So, why would it read only 0 bytes ? – Arjun Patel Dec 18 '13 at 00:56
  • 1
    You are connecting to a `server` at `Socket socket = new Socket(localhost, 9978);` this other process is not sending you any data. – Scary Wombat Dec 18 '13 at 00:57
  • @ArjunPatel It didn't 'read only 0 bytes'. It threw a `SocketTimeoutException,` and, for the seventh time, it did that ***because the server isn't sending you any data.*** `x == 0.` You proved it yourself. I don't know why you keep asking the same question when it's already been answered six times. – user207421 Dec 18 '13 at 00:58