I'm working on an Android application that requires very quick responses from our server. We communicate using HttpURLConnections. In an attempt to reduce latency, I'd like to "warm up" the connection by opening the socket when the user is actively using the application so that when we do get a request to send data, the connection will already be established (assuming the socket wasn't closed server side due to timeout). I believe that URL.openConnection() does not actually open this socket (despite its name) until data is actually sent. Is this correct? If so, is there a better way to simply open a socket and pass that to another HTTPURLConnection later without sending data and have that socket stay open for the keep-alive time?
i think this migth help you
https://github.com/Gottox/socket.io-java-client
its an IO Socket, mantains a conection with the server recieving data on the method "on", and you can send as well data trough "emit".
Related
In a client-server application, I send a string to the server in a separate thread:
dataOutputStream.writeUTF(_dos);
dataOutputStream.flush();
But I get in another thread java.net.SocketException: Connection reset in the line:
mes = dataInputStream.readUTF();
At the same time, the server does not receive anything (I checked this in wireshark) and continues to listen to the socket.
The question is: is it possible, if the client socket is damaged, but the server socket is working, to restore the operation of the client socket and thus restore the client-server connection, without restarting both?
The connection is gone. On the client side, you will need to connect again.
Generally speaking, TCP implementations do not allow you to issue a connect on a socket that has become disconnected. You can try, but it's safest to make a new socket object. Even if it works for you, the result may be non-portable.
The server of course also has a socket on a defunct connection, so it needs to close its end. A new socket will be created on 'accepting' the new connection from the client.
I have developed a very simple Client and a Server softwares using Java socket. The client generates a "Draw object" and I'm able to send it to the server correctly trough a socket (I'm using Gson to convert it to a String, since my client will be an Android device and RMI is not available natively).
The point is that I need to send many "Draw objects" to my server (one object per second, for N minutes).
My code currently does this:
Open socket connection
Capture the Draw Object
Send object to Server
Close socket connection
On the server side a have a Thread that loops (inside the run method) that receives the data and manipulates it.
My question is: Is there a way to send them (continuously) without need to close the client socket connection or is this the correct pattern?
To be more specific. Is there a way to have the following instructions?
Open socket connection
Loop
2.1. Capture draw object & send it to server
When draws are over, close connection
I have written a Client-Server program using java Sockets and it works just fine. Now I'm trying to move on to HTTPS for communicating between client and server.
My code is based on java built-in Sockets (Socket and ServerSocket) and it works just fine. But when I use HTTPS Sockets (SSLSocket and SSLServerSocket) without changing the rest of the code, unfortunately won't work anymore.
The program needs persistent connection between server and client to let them send and receive data for minutes. The problem seems to be that HTTPS connection has been closed in server or client side after first transaction has been completed.
To be more precise, This is what happens:
1. Server creates a SSLServerSocket and calls "accept()" on it.
2. Client creates a SSLSocket and connects to the server. Then writes to server through "DataOutputStream"
3. Server reads the client through "DataInputStream" and sends back its response through "DataOutputStream".
4. Client reads the server through "DataInputStream".
Everything is OK till now! But after that when the client sends another stream, on the server side no data would be "available()" on the server through the same method used before.
Is there a way to keep this connection alive?
Tnx for ur helps in advance!
InputStream.available() isn't a reliable way of detecting socket input. You therefore have no evidence that the connection isn't alive. Just block in a read method. This implies a dedicated thread for reading per client.
I'm using DefaultHttpClient with a ThreadSafeClientConnManager on Android (2.3.x) to send HTTP requests to a my REST server (embedded Jetty).
After ~200 seconds of idle time, the server closes the TCP connection with a [FIN]. The Android client responds with an [ACK]. This should and does leave the socket in a half-closed state (server is still listening, but can't send data).
I would expect that when the client tries to use that connection again (via HttpClient.execute), DefaultHttpClient would detect the half-closed state, close the socket on the client side (thus sending it's [FIN/ACK] to finalize the close), and open a new connection for the request. But, there's the rub.
Instead, it sends the new HTTP request over the half-closed socket. Only after sending is the half-closed state detected and the socket closed on the client-side (with the [FIN] sent to the server). Of course, the server can't respond to the request (it had already sent its [FIN]), so the client thinks the request failed and automatically retries via a new socket/connection.
The end result is that server sees and processes two copies of the request.
Any ideas on how to fix this? (My server does the correct thing with the second copy, but I'm annoyed that the payload is transmitted twice.)
Shouldn't DefaultHttpClient detect that the socket was closed when it first tries to write the new HTTP packet, close that socket immediately, and start a new one? I'm baffled as to how a new HTTP request is sent on a socket minutes after the server sent a [FIN].
This is a general limitation of the blocking I/O in Java. There is simply no way of finding out whether or not the opposite endpoint has closed connection other than by attempting to read from the socket. Apache HttpClient works this problem around by employing the so stale connection check which is essentially a very brief read operation. However, the check can and often is disabled. In fact it is often advisable to have it disabled due to extra latency the check introduces. I have no idea how exactly the version of HttpClient shipped with Android behaves in this regard but you could try explicitly enabling the check by using an appropriate config parameter.
A better solution to this problem might be evicting connections from the connection pool that have been idle over a particular period of time (say 150 seconds) after a period of inactivity.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e652
My TCP server is implemented using Netty. My client using vanilla java.net.Socket to connect to this server. I'm using the same socket to send multiple requests to the server. Once done with all the requests the client calls socket.close().
I'm not closing the channel anywhere in my server code. Also, I've set TCP KEEP_ALIVE on my server. Will closing the socket on the client end automatically close the channel on the server or do I've to do something else explicitly and what is the best practice ?
Usually, if an application closes a socket, its remote peer also notices that the closure. Therefore, you don't need to call close() on both side. However, sometimes, due to network problems, you might not get notified when the remote peer closes the connection. To work around this problem, it's a good idea to send some message periodically, and then you will detect the unexpected closure sooner.
Please note SO_KEEP_ALIVE will not help much here because for most operating systems because the default keep alive time is very long.