Detect if connection is failed - Java Android - java

How to check if server is online or offline, and if is offline start connecting until server is on. I have tried with this:
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(rConnection).start();
}
});
public Runnable rConnection = new Runnable() {
#Override
public void run() {
boolean status = connect();
while (!status)
{
System.out.println("Connection Status: " + status);
status = Connect();
}
}
};
public boolean Connect() {
boolean status = false;
try {
s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
System.out.println("Socket: " + s.toString());
if (s.toString() != "")
{
status = true;
}
} catch (UnknownHostException e) {
e.printStackTrace();
status = false;
s=null;
} catch (IOException e) {
e.printStackTrace();
status = false;
s=null;
} catch (NullPointerException e)
{
e.printStackTrace();
status = false;
s=null;
}
return status;
}
If server is running before staring app it connects successfully but if server is off or disconnects after some time I don't get any error message and it won't start reconnecting again. How to solve this?

Basically you may split this:
s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
into
s = new Socket();
s.connect(remoteAddr,timeout)
And then control if connect returns on timeout or on successfull connection.

Look at this thread for a solution and keywords: How can I monitor the network connection status in Android? . Also, consider retrying requests on a new connection if the underlying connection is lost (or times out).

How to check if server is online or offline, and if is offline start connecting until server is on
Try to connect to it when you need to connect to it, and handle the failures that result. At present you seem to be trying to maintain an eternal connection, which is never going to work. The best way to detect whether a resource is available is to try to use it at the time that you need to use it. Anything is subject to numerous sources of error such as timing window problems, testing the wrong thing, testing the right thing at the wrong time, and at best to overuse of scarce resources. Rethink your requirement.

Related

httpClient: How to check whether the connection was already closed by the server side

We connect to a data provider and keep reading XML data in an endless loop using HttpClient. We already set connection timeout and socket timeout in the Java code. However, when the connection is closed on the HTTP server side, our application does not throw any exception and just hangs there doing nothing. Because of this behavior, when the server is up the Java code will not reconnect. Is there any way to see whether the socket connection was already closed by the server side?
In addition, https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html mentioned how to close STALE connections, but it still does not solve our problem.
Thank you very much in advance!
Code snippet:
private static final HttpClientConnectionManager CONN_MGR = new BasicHttpClientConnectionManager();
public boolean connectToServer() {
disconnect();
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15 * 1000)
.setSocketTimeout(15 * 1000).build();
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
.setSSLSocketFactory(sslsf)
.setConnectionManager(CONN_MGR)
.setDefaultRequestConfig(requestConfig).build();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
in = response.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(in));
connected = true;
return true;
} catch (Exception re) {
LOGGER.error("error", re);
}
return false;
}
public void process() {
String xml = null;
while (!shuttingDown) {
try {
/* if we know the server side closed the connection already here
we can simply return and scheduler will take care of anything else. */
xml = reader.readLine();
lastSeen = System.currentTimeMillis();
if (StringUtils.isBlank(xml)) {
continue;
}
xml = xml.trim();
// processing XML here
} catch (IOException | NullPointerException ie) {
/* We see SocketTimeoutException relatively often and
* sometimes NullPointerException in reader.readLine(). */
if (!shuttingDown) {
if(!connectToServer()) {
break;
}
}
} catch (RuntimeException re) {
LOGGER.error("other RuntimeException", re);
break;
}
}
// the scheduler will start another processing.
disconnect();
}
// called by the scheduler periodically
public boolean isConnected() {
CONN_MGR.closeExpiredConnections();
CONN_MGR.closeIdleConnections(15, TimeUnit.SECONDS);
long now = System.currentTimeMillis();
if (now - lastSeen > 15000L) {
LOGGER.info("call disconnect() from isConnected().");
disconnect();
}
return connected;
}
I think with the introduction of Automatic Resource management in java 1.7
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
cleaning up of stale connections has been taken care and even on the worst case it has been taken care by the implementation providers carefully by monitoring the connection
Apache HttpClient uses HttpClientConnectionManager and other utility classes that does the job of cleaning the stale connections.
After the last catch block add a finally block for your cleanup code.
finally {
CONN_MGR.shutdown();
}
As per documentation it shuts down connection manager and releases allocated resources
We avoided the problem by closing the connection from another thread when no data coming in for more than 15 seconds. More specifically now isConnected() method is as follows:
// called by the scheduler periodically
public boolean isConnected() {
long now = System.currentTimeMillis();
if (now - lastSeen > 15000L) {
disconnect();
}
return connected;
}
Of course, the reading part get will java.net.SocketException: Socket closed or java.lang.IllegalStateException: Connection is still allocated when this happens.
After this change everything works fine.

Java Socket selector doesnt switch to write state

We have implemented solution based on selector
like
#Override
public void run() {
super.run();
while (session.isConnectionAlive()) {
try {
// Wait for an event
selector.select();
} catch (IOException e) {
log.error("Selector error: {}", e.toString());
log.debug("Stacktrace: ", e);
session.closeConnection();
break;
}
handleSelectorkeys(selector.selectedKeys());
}
executorService.shutdown();
log.debug("Ucp worker stopped");
}
private void handleSelectorkeys(Set<SelectionKey> selectedKeys) {
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey selKey = keys.next();
selector.selectedKeys().remove(selKey);
try {
processSelectionKey(selKey);
} catch (IOException e) {
// Handle error with channel and unregister
selKey.cancel();
log.error("Selector error: {}", e.toString());
log.debug("Stacktrace: ", e);
}
}
}
public void processSelectionKey(SelectionKey selKey) throws IOException {
// Since the ready operations are cumulative,
// need to check readiness for each operation
if (selKey.isValid() && selKey.isConnectable()) {
log.debug("connectable");
// Get channel with connection request
SocketChannel sChannel = (SocketChannel) selKey.channel();
boolean success = sChannel.finishConnect();
if (!success) {
// An error occurred; handle it
log.error("Error on finish");
// Unregister the channel with this selector
selKey.cancel();
}
}
if (selKey.isValid() && selKey.isReadable()) {
readMessage(selKey);
}
if (selKey.isValid() && selKey.isWritable()) {
writeMessage(selKey);
}
if (selKey.isValid() && selKey.isAcceptable()) {
}
}
It works fine till we start sending around 100 messages per sec and receive also around 100 response and 100 income messages and send 100 our responses (around 400 messages per sec in both ways) With such load from time to time due to unknown issue on other side our partner cut connection. We reestablish connection but for some reason selector doesn't switch to write state only read. We receive a lot messages on read but cannot send anything.
Any ideas? Is it OS issue issue on our side with connection. How does selector works? Switch from read to write by some logic or spontaneously?
There is no such thing as 'switch [from read] to write state'. A socket can be readable and writable at the same time, and a Selector doesn't 'switch': it merely reports which states exist on the socket.
If the write event never triggers, it is because the socket send buffer is full, which indicates that the peer isn't reading from the connection.

How to read response from remote server using Java Sockets

Part of a Java program I'm creating needs to talk to a service on a remote machine. That remote machine is running a service (written in Delphi I believe) on a Windows platform.
I need to connect to that machine, send command strings and receive (String) responses.
If I connect using Linux CLI telnet session I get responses as expected:
[dafoot#bigfoot ~]$ telnet [host IP] [host port]
Trying [host IP]...
Connected to [host IP].
Escape character is '^]'.
Welcome to MidWare server
ping
200 OK
ProcessDownload 4
200 OK
In the above the lines 'ping' and 'ProcessDownload 4' are me typing in the terminal, other lines are responses from remote system.
I created a Main in my Java class that will do the work to call the appropriate methods to try and test this (I've left out irrelevant stuff):
public class DownloadService {
Socket _socket = null; // socket representing connecton to remote machine
PrintWriter _send = null; // write to this to send data to remote server
BufferedReader _receive = null; // response from remote server will end up here
public DownloadServiceImpl() {
this.init();
}
public void init() {
int remoteSocketNumber = 1234;
try {
_socket = new Socket("1.2.3.4", remoteSocketNumber);
} catch (IOException e) {
e.printStackTrace();
}
if(_socket !=null) {
try {
_send = new PrintWriter(_socket.getOutputStream(), true);
_receive = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean reprocessDownload(int downloadId) {
String response = null;
this.sendCommandToProcessingEngine("Logon", null);
this.sendCommandToProcessingEngine("ping", null);
this.sendCommandToProcessingEngine("ProcessDownload", Integer.toString(downloadId));
try {
_socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private String sendCommandToProcessingEngine(String command, String param) {
String response = null;
if(!_socket.isConnected()) {
this.init();
}
System.out.println("send '"+command+"("+param+")'");
_send.write(command+" "+param);
try {
response = _receive.readLine();
System.out.println(command+"("+param+"):"+response);
return response;
} catch (IOException e2) {
e2.printStackTrace();
}
return response;
}
public static void main(String[] args) {
DownloadServiceImpl service = new DownloadServiceImpl();
service.reprocessDownload(0);
}
}
As you will see in the code, there are a couple of sys.outs to indicate when the program is attempting to send/receive data.
The output generated:
send 'Logon(null)'
Logon(null):Welcome to MidWare server
send 'ping(null)'
So Java is connecting to the server ok to get the "Welcome to Midware" message back, but when I try to send a command ('ping') I don't get a response.
So the questions:
- does the Java look about right?
- could problem be related to character encoding (Java -> windows)?
You need to flush the output stream:
_send.write(command+" "+param+"\n"); // Don't forget new line here!
_send.flush();
or, since you create a auto-flushing PrintWriter:
_send.println(command+" "+param);
The latter has the disadvantage that the line end can be \n or \r\n, depending on the system on which your Java VM runs. So I prefer the first solution.

errors when using the bluetooth sample code on android developer site

I am working to add Bluetooth capabilities to my app and ultimately the device that I want to use is a headset/earpiece. I have begun assembling the code and I partial functionality with it. When I got to the code for setting up a bluetooth connection by server, I got errors when adding the code. I have tried solving the problems through the hover over the error and autocorrect but every time I fix one problem a different on arises. This leads me to believe that I am missing something that autocorrect doesn't know about. I need some help fixing the errors. Useful suggestions for setting a bluetooth codin for the first time would also be appreciated. Errors are surrounded with ||#| xxx |||. Error 1:cannot be resolved. Error 2:cannot be resolved to a variable. Error 3:undefined for the type AcceptSocket.
import java.io.IOException;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
public class AcceptSocket extends Thread {
private static final String MY_UUID = null;
BluetoothServerSocket mmServerSocket;
public void AcceptThread() {
// Use a temporary object that is later asssigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = ||1|mBluetoothAdapter|||.listenUsingRfcommWithServiceRecord(||2|NAME|||,
MY_UUID);
} catch (IOException e) {
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
||3|manageConnectedSocket|||(socket);
mmServerSocket.close();
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) {
}
}
}
Error 1,2: There is no constant called NAME anywhere in the class.
Error 3: There is no method called manageConnectedSocket() in the class.
You can't just copy and paste something from the developer's page and expect it to work. It leads you in the correct direction and you have to fill in the missing pieces.

HttpURLConnection, problem turning on/off WiFi and trying to connect

I have an application which is a RESTful API consumer, and I wish to have a timeout on my connection with the API.
As I've searched, and also tested, the HttpURLConnection.setReadTimeout() method doesn't work, so the solution I found was to use an AsyncTask which will try to connect to the server and then pass the timeout to the AsyncTask.get().
It works, partially. The problem is when I do the following:
Enter the application with the WiFi turned on. I click "Login" button and get "Invalid/User password". Ok.
Turn off the WiFi, click "Login" button. The application tries to connect but after 5 seconds (the timeout I chose) it shows me the notification dialog saying I'm not connected. Ok, everything as expected.
Turn on back the WiFi, click "Login" button. It stills working as if I was not connected, always showing the dialog. I can wait many seconds but the behaviour is the same as If I was disconnected.
I debugged all my code, step by step using Eclipse and there's nothing wrong with the logic.
My HttpURLConnection is always a new object, so I'm not trying to use the same connection object to connect after the WiFi is turned on back...
Also I'm using the Scribe library, for OAuth, but I checked the source code and everything seems ok, except that I changed the method which creates a connection to always use a new instance.
I'm starting to think that Android is "caching" my connection object or the AsyncTask object...
Below some code of my RequestManager class:
public class RequestManager {
private static RequestManager self = new RequestManager();
private Request currentRequest;
private static final long TIMEOUT = 5000;
public static RequestManager getInstance() {
return self;
}
public void startRequest(Request request) {
if (currentRequest != null) return;
currentRequest = request;
}
public String getResponse() throws ConnectionErrorException {
RequestThreat rt = new RequestThread();
rt.execute(currentRequest);
try {
return (String) rt.get(TIMEOUT, TimeUnit.MILISECONDS);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} catch (TimeoutException e) {
throw new ConnectionErrorException();
} finally {
endRequest();
}
return null;
}
public void endRequest() {
currentRequest = null;
}
private class RequestThread extends AsyncTask<Request, Integer, String> {
#Override
protected String doInBackground(Request... requestParams) {
return requestParams[0].send().getBody();
}
}
}
Also in the method I call getResponse() I'm calling endRequest() after.
Any thoughts?
You should check to see if a network connection is available before trying to access the network. Have a look at this quesion:
How to respect network use settings
Specifically:
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {

Categories

Resources