My Server program waits for the Client for long time But I want the Server Socket to wait for the Client for only specified time limit, say 2 minutes.How can i set this time limit.I have used timeout on timeout on Server Socket which blocks the connection for the specified timeout Which is not desired.
You can use following method to set timeout in server socket : setSoTimeout(int timeout)
For more information read the documentation : ServerSocket
Related
I have a Java socket connection established with a server A. Server A randomly restarts during the day and my Java socket connection infinitely waits because it thinks that is receiving data.
I have set the soTimeout but it is only kicking in while read is still going on.
How do I set force a timeout on the socket connection? I want to break the socket connection after a set period of time.
Socket mySocket = new Socket(host, port) ;
mySocket.setKeepAlive(true);
mySocket.setSoTimeout(timeout);
You don't need to break the connection periodically. You may just use TCP Keepalive mechanism to send small "ping" requests in defined intervals. Whenever one of parties does not respond, TCP connection is closed. More info here: https://en.wikipedia.org/wiki/Keepalive
There is the connecting timeout value passed to connect method, and there is the reading timeout set using setSoTimeout method. I was wondering why there is no method to set the "writing timeout"? I think there is the writing timeout concept in the TCP Protocol.
It wouldn't be much use.
In general TCP sending is asynchronous to the application. All that send() does is put the data into the socket send buffer. It then returns, while the send buffer is emptied to the network asynchronously. So there is nothing to timeout. And the absence of a timeout does not denote that the data has been sent to the peer.
send() blocks while the send buffer is full, and it would be possible to implement a timeout on that, and indeed you can do that yourself in non-blocking mode with select(), but the problem is that what timed out could be either the current send or a prior one. So delivering a timeout would be rather confusing. Instead what is delivered when all the TCP send timers time out internally is a connection reset.
I think there is the writing timeout concept in the TCP Protocol.
There is indeed, but that's at the level where TCP is asynchronously emptying the socket send buffer. It isn't under application control.
you can first try to connect...if connect fails catch exception
InetSocketAddress sockAdr = new InetSocketAddress(serveradres, 2222);
Socket newsok = new Socket();
int timeout = 2000;
newsok.connect(sockAdr, timeout);
I am trying to create a java http server using tcp sockets. HTTP 1.1 has a timeout value that will enable the connection to be persistent and wait for a short while for possible data from the client. I am trying to implement this timer in my program by using:clientSocket.setSoTimeout(). Even though this will help to leave the connection open for a certain amount of time, but it will wait for that exact amount of time before allowing the next request to be read.
For example:
If timeout is set to 5 seconds,
Request 1 is read. Then the socket hangs and wait until 5 seconds is over.
Request 2 is read. The socket waits until 5 seconds is up again.
This proves to be a problem if my timeout is set to big values. This should not be the case as the request should be processed once it is received and the timeout should only expire only if no data is received throughout the specified duration.
Can anyone advise me on how I could resolve this?
Edit:
For people who face a similar problem, here is my solution:
Since the client waits until the timeout before receiving all the data, I guessed that the client does not know that all the data from the server has been received. Hence, I added a content-length field to the HTTP response packet. Now, my client no longer hangs after receiving the data. The setSoTimeout does indeed work as stated!
Ok, when you receive a connection, then please start a new Thread like this:
class ClientService extends Thread {
private final Socket clientSocket;
public ClientService(Socket clientSocket) {
this.clientSocket=clientSocket;
}
public void run() {
// do your work with the Socket clientSocket here
}
}
this is how then your server code should look like:
while (true) {
Socket clientSocket = server.accept();
new ClientService(clientSocket).start();
}
It will allow you to process responses without waiting for one another till it timeouts.
HTTP 1.1 has a timeout value that will enable the connection to be persistent and wait for a short while for possible data from the client.
Not really. It has a connection: keep-alive setting, which is the default behaviour, and it allows endpoints to close connections that aren't in use after a period of idleness, but it doesn't have a timeout property itself.
I am trying to implement this timer in my program by using:clientSocket.setSoTimeout().
This has nothing whatsoever to do with HTTP. It is a socket read timeout.
Even though this will help to leave the connection open for a certain amount of time, but it will wait for that exact amount of time before allowing the next request to be read.
No it won't. It will cause read methods to throw SocketTimeoutException if no data arrives within the timeout period. Nothing else.
For example:
If timeout is set to 5 seconds,
Request 1 is read. Then the socket hangs and wait until 5 seconds is over.
No it doesn't.
Request 2 is read. The socket waits until 5 seconds is up again.
No it doesn't. You've made all this up. It is fantasy.
This proves to be a problem if my timeout is set to big values.
It isn't a problem with any timeout values whether large or small, because it simply does not happen.
This should not be the case as the request should be processed once it is received and the timeout should only expire only if no data is received throughout the specified duration.
That is exactly what Socket.setSoTimeout() already does.
Your question is founded on a fallacy.
I am programming in java using Sockets and facing this below metioned issue.
I programmed a Server program which sends a plain text to the client side.When I start the server the server program waits for a long time for the client socket to get connected.But i want the server program to wait only for a specified time say 5 minutes and then report the user that the Client is not connected within the Specified time.
I am unable to understand how to implement it.I have gone through Timer and TimerTask classes but its a bit confusing.
Use ServerSocket.setSoTimeout(int timeOut) to wait for the client. Set it to 0 if you need to wait infinitely.
Note: Java doc says:
The option must be enabled prior to entering the blocking operation to
have effect.
As java API doc describes 'Socket.accept()':
Throws:
IOException - if an I/O error occurs when waiting for a connection.
SecurityException - if a security manager exists and its checkAccept method doesn't allow the operation.
SocketTimeoutException - if a timeout was previously set with setSoTimeout and the timeout has been reached.
See
Java API Doc Socket
I am using http servlet on my server.
My game client uses the next http connection:
InetAddress inteAddress = InetAddress.getByName(server);
SocketAddress socketAddress = new InetSocketAddress(inteAddress, port);
// create a socket
socket = new Socket();
// this method will block no more than timeout ms.
int timeoutInMs = 10*1000; // 10 seconds
socket.connect(socketAddress, timeoutInMs);
Time socket connection = 10 seconds... but I need to keep the connection
What a client connection can I use for the game client? (Looking for best practice)
As soon as your socket connection is established, you can keep it around and reuse it as you see fit.
The value for the timeout parameter only has effect during establishment of the connection. If it's 10 seconds, as in your example, the implementation will try to establish a connection for 10 seconds (the method call is blocked during that time). If the implementation fails to establish a connection within those 10 seconds, it will fail.