Android read/write in same socket from ServerSocket - android

I have an android app which will create a ServerSocket and accept a socket.
I want it can communicate(read/write) with remote device.
My sample code like following:
mListenSocket = new ServerSocket();
mListenSocket.setReuseAddress(true);
mListenSocket.bind(new InetSocketAddress(DOCK_PORT));
mSocket = mListenSocket.accept();
Thread {
loop {
outputStream = mSyncSocket.getOutputStream();
inputStream = mSyncSocket.getInputStream();
...
inputStream.read(data)
outputStream.write(data);
...
}
}
It can read right inputStream data from client, but the second time read() always return -1 after I write data in outputStream firt time .
I have no idea about this issue.
Somebody can give me some tips? Thanks a lot.
======================================================================
I think I need to express my problem more clearly.
There are two devices(A, B), and their workflow as follows:
Type 1 task:
1. A sends command to B
2. B receives command and reply message to A
3. A receives message, Done.
Type 2 task:
1. A sends command to B, Done.
Above tasks are asynchronized.
My old method was that create a ServerSocket on A and B device respectively to handle A->B and B->A communication.
I think maybe one socket can resolve above task, but one socket will encounter read -1 issue.
Someone can give me more advices? Thanks a lot.

read() returns -1 because the peer has closed the connection. You should do likewise, and stop reading, and stop handling the connection completely. It's finished.

I use something like this:
final DataOutputStream dos = new DataOutputStream(s.getOutputStream());
final BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
// Send the login data.
final JSONObject login = new JSONObject();
// Send the login message.
dos.write((login.toString() + "\r\n").getBytes());
// Wait until we receive something from the server.
String receivedMessage;
while ((receivedMessage = in.readLine()) != null) {
Log.i(TAG, "Received data: " + receivedMessage);
processMessagesFromServer(dos, receivedMessage);
}
I use a normal socket instead of ServerSocket.
Please check this thread for more information.

Related

Volley - CancelAll() doesn't stop sending the request

I want to completely stop volley request from sending the data, calling mRequestQueue.cancelAll(TAG)
method just stops the response from returning to the response handlers but sending the data process is still working at the background, i am sending a large byte[] file so once the user cancels the sending process, i want to interrupt the request and stop sending the data.
Can any one think of a workaround to overcome this problem ?
Since mRequestQueue.cancelAll(TAG) method just stops the response from returning to the response handlers and keeps sending the request at the background, i could find a workaround to solve this issue by editing the volley library itself, here are the steps:
Clone the volley library from here: https://android.googlesource.com/platform/frameworks/volley/
Import it as a module to your project.
Navigate to com.android.volley.toolbox.HurlStack java class, go to addBodyIfExists() method
Replace this code:
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(body);
out.close();
with this code:
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
final int offset = 0;
for (int i = offset; i < offset + body.length; i++) {
//if at any time of sending the body, the mCancel boolean became true, then stop sending the request and break the loop
if (request.isCanceled()) {
Log.d("Tracing Request", "request.isCanceled()");
out.close();
break;
}
Log.d("Tracing Request", "Writing to DataOutputStream");
out.write(body[i]);
}
out.close();
What i did was simply implementing here the code insideout.write(body); method but with adding the if condition to check if the request has been canceled then break out of the loop to stop sending the request.

Android: Best practice to read data from socket

I am trying to get around Sockets in Android. Especially I want to know what is best practice to read data from socket and present it to UI.
As per I understand, we cannot have call to read data in the main UI thread as it is a blocking operation.
So I got these code snippets reading data from socket. (Btw, I've picked up these snippets from voted up SO questions):
This...
SocketAddress sockaddr = new InetSocketAddress("192.168.1.1", 80);
nsocket = new Socket();
nsocket.connect(sockaddr, 5000); //10 second connection timeout
if (nsocket.isConnected()) {
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
Log.i("AsyncTask", "doInBackground: Waiting for inital data...");
byte[] buffer = new byte[4096];
int read = nis.read(buffer, 0, 4096); //This is blocking
while(read != -1){
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
publishProgress(tempdata);
Log.i("AsyncTask", "doInBackground: Got some data");
read = nis.read(buffer, 0, 4096); //This is blocking
}
And this...
clientSocket = new Socket(serverAddr, port);
socketReadStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = null;
String stringToSend = "This is from client ...Are you there???";
//Write to server..stringToSend is the request string..
this.writeToServer(stringToSend);
//Now read the response..
while((line = socketReadStream.readLine()) != null){
Log.d("Message", line);
Being newbe to android development I like to know:
What is difference between these two ways of reading?
First one was written as AsyncTask while second one was intended to run as separate thread. Which one is correct approach?
Is there any better way to read from socket? (e.g. using non-blocking sockets, callbacks, using any popular third party library etc.)
What is difference between these two ways of reading?
The second approach used BufferedReader, which has a internal buffer mechanism, which make you write less code.
First one was written as AsyncTask while second one was intended to run as separate thread. Which one is correct approach?
AsyncTask is a wrapper of Thread, using AsyncTask can do network operation in the background thread and publish result int the ui thread.AsyncTask also manages the Thread pool, in some cases, you need not create a new thread every time. It is recommended to use AsyncTask in Android.
Is there any better way to read from socket? (e.g. using non-blocking sockets, callbacks, using any popular third party library etc.)
You can use Square's okio, it is a better IO library for java, and has Buffer support.

How to read available text (not ending with new line) from TCP socket using BufferedReader

I'm communication with a server through a tcp socket connection, i'm able to read lines that ends with \n fine, however when the line is not terminated (ends in \n) i'm not able to read it. I tried the following but it didn't work and caused my app to freeze at startup:
private Socket socket;
private BufferedReader input;
public boolean isConnected;
#Override
public void onCreate()
{
try
{
socket = new Socket ("server.ip.add.ress", 23456);
input = new BufferedReader (new InputStreamReader (socket.getInputStream());
handshake();
isConnected = true;
}
catch // Handle IOException and UnknownHostException
}
// custom runnable to read availabe input from the server
private class MyRunnable implements Runnable
{
private volativle String value;
public String getValue()
{
return value;
}
#Override
public void run()
{
int count;
char[] buffer = new char[10]; // expected message 'username: '
try
{
count = input.read (buffer, 0, 10);
if (count > 0) value = new String (buffer);
}
catch // IOException
}
}
// when connection is established with server expect 'username: ' from
// the server and send the user name back to it
public void handshake()
{
MyRunnable runnable = new MyRunnable();
try
{
Thread thread = new Thread (runnable);
thread.start();
thread.join();
String greeting = runnable.getValue();
if (greeting.equals ("username: ")) // Send username back
}
catch // InterruptedException
}
why is it hanging? and how can i read a non terminated line?
Edit:
To clarify: The server sends the greeting message username: immediately after the connection is established with a client, the client wait for the greeting and send back it's username when received (that's what handshake() does), if no handshake the client disconnects otherwise it start listening for incoming messages. Because i need to know if handshake is complete before starting the listener i had to use Thread.join().
The problem: Thanks for the comments and answers below, it turned out that BufferedReader.read() blocks the thread and waits until something is sent from the server and if nothing is being sent it causes the app to hang, Therefor there's no way to find out if the line has ended.
The solution: In my specific situation i just wanted to know if a specific message is sent "username: " so i used read (buffer, 0, 10) to read exactly 10 characters (the length of "username: "), and because it blocks if nothing is sent i used Thread.join (1000) which waits only one second and then if nothing received i disconnect the client.
Why is it hanging?
This is what it is suppose to be. It will block the thread if no data is available to read. This is also why you want to put it in a background thread.
Can it not just return if nothing is available?
What you are looking for is ready(), which will tell you whether there is available data or not.
Indicates whether this reader is ready to be read without blocking.
Returns
true if this reader will not block when read is called, false if unknown or blocking will occur.
But you should be very careful when using this function. Because networking is a lot about timing. The fact that you don't have any data to read at this second doesn't necessary mean that it won't be any data in the next second.
So a better design of the server should be more or less as the following:
If the username is found, return the username
If the username is not found, return an error message to let the client side know that the username is not found
There's no need for the thread. Your goal is to wait until you've read what you've been waiting for. Why not just let read() perform the wait for you?
What you're struggling with is the classic problem of TCP communication: "when do I know that I've got everything the server sent?"
In your case, you're expecting to read bytes until the collection of bytes ends with "username: ". So, change your algorithm to perform 1 byte reads (filling a buffer as you go) until that buffer ends with "username: ".
You can make a more complicated algorithm -- which would be more efficient -- that would attempt to read multiple bytes at a time and append them to a buffer -- performing your check each time. But either strategy is logically equivalent.
I also recommend just using the InputStreamReader. It has various read() methods. I am a bit suspicious about the BufferedInputReader, especially when dealing with data that isn't newline terminated. I'm probably just paranoid. I've just never used it when writing TCP client/server programs, so I'm not sure.

Check RTSP URI during onCreate in Android

I'm creating a fairly simple Android app for work that will plug a URI into the system video player so that users can easily watch the RTSP stream that we are sending from our Wowza server.
In general, I basically just have a play button that calls:
Uri myURI = Uri.parse("rtsp://ipaddress:1935/path/myStream");
Intent intent = new Intent(Intent.ACTION_VIEW, myURI);
startActivity(intent);
but we don't stream from this URI 24/7 and I would like to change the behavior during onCreate so that it will validate the URI before drawing the button, giving me the opportunity to give the user visual feedback that the stream isn't live and save us unneeded support emails.
I've looked at the Uri and URI classes to see if there was a method that I could call to check the URI but nothing seems to really exist. Other URI related questions seem to refer to local files so the dev can just check a file, but I don't think that would work for a live stream. Any advice would be greatly appreciated!
Basically you want to open a plain client socket connection to the RTSP server hostname/port and then post a RTSP OPTIONS request.
If the server responds with a "RTSP/1.0 200 OK", it means the streaming service is up, otherwise it's down.
I wrote a quick and dirty sample RTSP check and tested it out. Feel free to adapt it to your needs:
Update handling UI operations
In your activity class, create these fields:
private static final int MESSAGE_RTSP_OK = 1;
private static final int MESSAGE_RTSP_ERROR = -1;
private Handler handler;
in your onCreate method, add:
handler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch (msg.what){
case MESSAGE_RTSP_OK:
//show_player();
// implement ui operation here
break;
case MESSAGE_RTSP_ERROR:
//show_error();
break;
}
}
};
Then run this sample code:
Updated rtsp check code
new Thread() {
public void run() {
try {
Socket client = new Socket("a1352.l1857053128.c18570.g.lq.akamaistream.net", 554);
OutputStream os = client.getOutputStream();
os.write("OPTIONS * RTSP/1.0\n".getBytes());
os.write("CSeq: 1\n\n".getBytes());
os.flush();
//NOTE: it's very important to end any rtsp request with \n\n (two new lines). The server will acknowledge that the request ends there and it's time to send the response back.
BufferedReader br =
new BufferedReader(
new InputStreamReader(
new BufferedInputStream(client.getInputStream())));
StringBuilder sb = new StringBuilder();
String responseLine = null;
while (null != (responseLine = br.readLine()))
sb.append(responseLine);
String rtspResponse = sb.toString();
if(rtspResponse.startsWith("RTSP/1.0 200 OK")){
// RTSP SERVER IS UP!!
handler.obtainMessage(MESSAGE_RTSP_OK).sendToTarget();
} else {
// SOMETHING'S WRONG
handler.obtainMessage(MESSAGE_RTSP_ERROR).sendToTarget();
}
Log.d("RTSP reply" , rtspResponse);
client.close();
} catch (IOException e) {
// NETWORK ERROR such as Timeout
e.printStackTrace();
handler.obtainMessage(MESSAGE_RTSP_ERROR).sendToTarget();
}
}
}.start();`
For this test I picked the public NASA TV rtsp server: rtsp://a1352.l1857053128.c18570.g.lq.akamaistream.net:554
The code sends the following rtsp request:
OPTIONS * RTSP/1.0
CSeq: 1
And receives the following response:
RTSP/1.0 200 OK
Server: QTSS-Akamai/6.0.3 (Build/526.3; Platform/Linux; Release/Darwin; )
Cseq: 1
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, OPTIONS, ANNOUNCE, RECORD
If you are looking for more complex checks, feel free to dive into the rtsp protocol documentation and enhance the code per your own needs: https://www.ietf.org/rfc/rfc2326.txt
Please let me know if more information is needed.

Android's SSLServerSocket causes increasing native memory in the App, OOM

Background
I am developing an Android App which provides a simple HTTP/HTTPS server. If the HTTPS serving is configured then on every connection an increasing native memory usage is observed which eventually leads to an app crash (oom), while using the HTTP configuration keeps the native memory usage relative constant. The app's Java VM keeps relative constant in both configurations.
The app serves an HTML page which contains a javascript with periodic polling (one json poll every second), so calling the app page using the HTTPS configuration and keeping the page open for several hours will lead to the mentioned out-of-memory because of increasing native memory usage. I have tested many SSLServerSocket and SSLContext configurations found on internet with no luck.
I observe the same problem on various Android devices and various Android versions beginning with 2.2 up to 4.3.
The code for handling client requests is the same for both configurations HTTP/HTTPS. The only difference between the two configurations is the setup of the server socket. While in the case of HTTP server socket one single line similar to this "ServerSocket serversocket = new ServerSocket(myport);" does the job, in the case of HTTPS server setup the usual steps for setting up the SSLContext are taken -- i.e. setting up the keymanager and initializing the SSLContext. For now, I use the default TrustManager.
Need For Your Advice
Does somebody know about any memory leak problems in Android's default TLS Provider using OpenSSL? Is there something special I should consider to avoid the leak in the native memory? Any hint is highly appreciated.
Update: I have also tried both TLS providers: OpenSSL and JSSE by explicitly giving the provider name in SSLContext.getInstance( "TLS", providerName ). But that did not change anything.
Here is a code block which demonstrates the problem. Just create a sample app put it into the bottom of the main activity's onCreate and build & run the app. Make sure that your Wifi is on and call the HTML page by following address:
https://android device IP:9090
Then watch the adb logs, after a while you will see the native memory beginning to increase.
new Thread(new Runnable() {
public void run() {
final int PORT = 9090;
SSLContext sslContext = SSLContext.getInstance( "TLS" ); // JSSE and OpenSSL providers behave the same way
KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
KeyStore ks = KeyStore.getInstance( KeyStore.getDefaultType() );
char[] password = KEYSTORE_PW.toCharArray();
// we assume the keystore is in the app assets
InputStream sslKeyStore = getApplicationContext().getResources().openRawResource( R.raw.keystore );
ks.load( sslKeyStore, null );
sslKeyStore.close();
kmf.init( ks, password );
sslContext.init( kmf.getKeyManagers(), null, new SecureRandom() );
ServerSocketFactory ssf = sslContext.getServerSocketFactory();
sslContext.getServerSessionContext().setSessionTimeout(5);
try {
SSLServerSocket serversocket = ( SSLServerSocket )ssf.createServerSocket(PORT);
// alternatively, the plain server socket can be created here
//ServerSocket serversocket = new ServerSocket(9090);
serversocket.setReceiveBufferSize( 8192 );
int num = 0;
long lastnatmem = 0, natmemtotalincrease = 0;
while (true) {
try {
Socket soc = (Socket) serversocket.accept();
Log.i(TAG, "client connected (" + num++ + ")");
soc.setSoTimeout(2000);
try {
SSLSession session = ((SSLSocket)soc).getSession();
boolean valid = session.isValid();
Log.d(TAG, "session valid: " + valid);
OutputStream os = null;
InputStream is = null;
try {
os = soc.getOutputStream();
// just read the complete request from client
is = soc.getInputStream();
int c = 0;
String itext = "";
while ( (c = is.read() ) > 0 ) {
itext += (char)c;
if (itext.contains("\r\n\r\n")) // end of request detection
break;
}
//Log.e(TAG, " req: " + itext);
} catch (SocketTimeoutException e) {
// this can occasionally happen (handshake timeout)
Log.d(TAG, "socket timeout: " + e.getMessage());
if (os != null)
os.close();
if (is != null)
is.close();
soc.close();
continue;
}
long natmem = Debug.getNativeHeapSize();
long diff = 0;
if (lastnatmem != 0) {
diff = natmem - lastnatmem;
natmemtotalincrease += diff;
}
lastnatmem = natmem;
Log.i(TAG, " answer the request, native memory in use: " + natmem / 1024 + ", diff: " + diff / 1024 + ", total increase: " + natmemtotalincrease / 1024);
String html = "<!DOCTYPE html><html><head>";
html += "<script type='text/javascript'>";
html += "function poll() { request(); window.setTimeout(poll, 1000);}\n";
html += "function request() { var xmlHttp = new XMLHttpRequest(); xmlHttp.open( \"GET\", \"/\", false ); xmlHttp.send( null ); return xmlHttp.responseText; }";
html += "</script>";
html += "</head><body onload=\"poll()\"><p>Refresh the site to see the inreasing native memory when using HTTPS: " + natmem + " </p></body></html> ";
byte[] buffer = html.getBytes("UTF-8");
PrintWriter pw = new PrintWriter( os );
pw.print("HTTP/1.0 200 OK \r\n");
pw.print("Content-Type: text/html\r\n");
pw.print("Content-Length: " + buffer.length + "\r\n");
pw.print("\r\n");
pw.flush();
os.write(buffer);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
soc.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
-- EDIT --
I have uploaded a sample app project called SSLTest for eClipse which demonstrates the problem:
http://code.google.com/p/android/issues/detail?id=59536
-- UPDATE --
Good news: today the reported Android issue above was identified and proper submissions were made to fix the memory leak. For more details see the link above.
I imagine this would be a substantial time investment, but I see that Valgrind has been ported to Android. You could try getting that up and running. Of course, if you find there's an internal memory leak, there isn't a lot you can do about it except attempt to get the bug fixed in future Android releases.
As a workaround, you could make your application multi-process and put the https service in a separate process. That way you could restart it periodically, avoiding OOM. You might also have to have a third process just accepting port 443 connections and passing them on to the https worker - in order to avoid tiny outages when the https worker is restarted.
This also sounds like a substantial time investment :) But it would presumably successfully avoid the problem.
--- EDIT: More detail ---
Yes, if you have a main application with its own UI, a worker process for handling SSL and a worker process for accepting the SSL requests (which as you say probably can't be 443), then on top of your normal Activity classes, you would have two Service classes, and the manifest would place them in separate processes.
Handling SSL process: Rather than waiting for an OOM to crash the service, the service could monitor its own Debug.getNativeHeapSize(), and explicitly restart the service when it increased too much. Either that, or restart automatically after every 100 requests or so.
Handling listening socket process: This service would just listen on the TCP port you choose and pass on the raw data to the SSL process. This bit needs some thought, but the most obvious solution is to just have the SSL process listen on a different local port X (or switch between a selection of different ports), and the listening socket process would forward data to port X. The reason for having the listening socket process is to gracefully handle the possibility that X is down - as it might be whenever you restart it.
If your requirements allow for there being occasional mini-outages I would just do the handling SSL process, and skip the listening socket process, it's a relatively simple solution then - not that different to what you'd do normally. It's the listening socket process that adds complexity to the solution...
Does it help to explicitly close the input stream? In the sample code the input stream seems to only be closed in the case of a SocketTimeoutException exception.
--EDIT--
You could rename run() to run2() and move the while loop into run() and remove it from run2() and see if that makes a difference? This couldn't be a solution but would tell you if any of the long-lived objects free up the memory when their references are dropped.
There is one detail I would recommend changing in your implementation.
Make a list of all your resource variables, for example Sockets, Streams, Writers, etc. Be sure to have the declaration outside your try statement and be sure to do cleanup / closing in the finally statement. I normally do something like this to be 100% sure:
InputStream in = null;
OutputStream out = null;
try {
//assign a proper value to in and out, and use them as needed.
} catch(IOException e) {
//normal error handling
} finally {
try {
in.close();
} catch(IOException e) {}
try {
out.close();
} catch(IOException e) {}
}
It looks a little bit confusing, but imagine you use your in Stream inside the try block and you get some Exception, then your Streams never get closed and that is a potential reason for memory leaks.
I cannot guarantee that this is the reason, but it should be a good startup point.
About managing your service. I had a lot of bad experiences with Android services because I was running them in the same thread as the GUI. Under some circumstances, Android will see some code that is executing for too long and kill your main process in order to protect from crashes. The solution I found was to follow the suggestion from this tutorial (look at point 4):
http://www.vogella.com/articles/AndroidServices/article.html
After this, my service just worked as expected and didn't interfere with my GUI Process.
Regards

Categories

Resources