Does anybody have more helpful information on the Exception "Try again"?
I'm sending Bitmaps between apps using LocalServerSocket and LocalSocket:
Output:
socket = new LocalSocket();
socket.connect(new LocalSocketAddress(SOCKET_NAME));
fos = new DataOutputStream(socket.getOutputStream());
...
public void onEvent() {
fos.writeInt(width);
fos.writeInt(height);
fos.writeInt(newBuffer.length);
fos.write(newBuffer);
}
Input:
server = new LocalServerSocket(SOCKET_NAME);
socket = server.accept();
socket.setSoTimeout(60);
while(true) {
int width = fis.readInt(); // IO Exception being thrown here
int height = fis.readInt();
int length = fis.readInt();
byte[] bytes = new byte[length];
fis.read(bytes);
}
[try/catch etc removed for clarity]
04-18 09:19:11.664: W/System.err(1268): java.io.IOException: Try again
04-18 09:19:11.664: W/System.err(1268): at android.net.LocalSocketImpl.readba_native(Native Method)
04-18 09:19:11.664: W/System.err(1268): at android.net.LocalSocketImpl.access$400(LocalSocketImpl.java:29)
04-18 09:19:11.664: W/System.err(1268): at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:92)
04-18 09:19:11.664: W/System.err(1268): at libcore.io.Streams.readFully(Streams.java:81)
04-18 09:19:11.664: W/System.err(1268): at java.io.DataInputStream.readInt(DataInputStream.java:124)
04-18 09:19:11.664: W/System.err(1268): at com.test.util.BitmapSendingUtils$BitmapReceiver$1.run(BitmapSendingUtils.java:105)
The exception you see is probably the java equivalent to the EAGAIN error. See for example this answer.
You should handle the exception and try the failed IO operation again.
I've since rengineered this as I couldn't find the solution. But while implementing it a different way I came across these errors in the original code:
byte[] bytes = new byte[length];
fis.read(bytes);
Should be:
byte[] content = new byte[length];
int read = is.read(content);
while(read < content.length) {
read += is.read(content, read, content.length - read);
}
as .read(byte[]) doesn't slurp the whole thing at once. I assumed that this continually slurped and blocked while it did so.
There is also this:
socket.setSoTimeout(60);
The arg is in millis rather than seconds so should be:
socket.setSoTimeout(60 * 1000);
I still don't know the cause of the above badly named exception though so hopefully someone will still answer this if they know!
try as flow, use mInputValid to control whether end the flow:
private int fill(byte[] buffer, int offset,int length) throws IOException {
int sum = 0, len;
while ((sum<length) && mInputValid) {
try{
len = is.read(buffer, offset + sum, length - sum);
if (len < 0) {
throw new IOException("End of stream");
} else{
sum += len;
Log.i(TAG, "is.read: " + len + " buffer:" + buffer[0]);
}
}
catch (IOException e){
e.printStackTrace();
Log.i(TAG, "read input fail, try again");
continue;
}
}
return sum;
}
I think the "Try again" IOException should actually be handled in the same way a SocketTimeoutException would be handled. This is a very poorly implemented API, but we are used to such crappy design on Android:
private int read(byte[] buffer) throws IOException {
while (true) {
try {
return fis.read(buffer);
} catch (SocketTimeoutException e) {
continue;
} catch (IOException e) {
String message = e.getMessage();
if (message != null && message.equals("Try again")) {
continue;
}
throw e;
}
}
}
private int readInt() throws IOException {
while (true) {
try {
return fis.readInt();
} catch (SocketTimeoutException e) {
continue;
} catch (IOException e) {
String message = e.getMessage();
if (message != null && message.equals("Try again")) {
continue;
}
throw e;
}
}
}
I know I am late to the party, but I just solved the same issue and there are a number of things that can cause this:
Not calling outputStream.flush() on sending. If you use writers, it's writer.flush(). This sends the last buffer. If you are not using buffers, it doesn't mean they aren't there. Stream send data in bytes, so unless you are sending a single byte, chances are there's some buffer down the line that doesn't get sent, and the receiver gets half an int.
If you are keeping the streams open, there is no way to detect the stream's end: so the Java will thrown an exception if you try to do things like reader.read(buffer), when size of the buffer exceeds the amount of data sent. In this case you must implement some kind of prototocle (header telling the length or some sort of end token, to know when to stop reading).
If you are closing streams after sending message and calling output.flush(), this exception can be caused by what you meantioned: socket timeouts, as well as disconnects.
Related
I need to implement a TCP comunication between an IoT device(custom) and an Android App.
For the Wifi device we have a Server Socket, while in Android i have an AsyncTask as a Client Socket. Both the device and the smarthone are connected to the same network.
Here is the Android Client Socket code for the initialization/socket-read and socket-write:
Variables:
static public Socket nsocket; //Network Socket
static public DataInputStream nis; //Network Input Stream
static private OutputStream nos; //Network Output Stream
AsyncTask method doInBackgroud:
#Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
boolean result = false;
try {
//Init/Create Socket
SocketInit(IP, PORT);
// Socket Manager
SocketUpdate();
} catch (IOException e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: IOException");
clearCmdInStack();
MainActivity.SocketDisconnectAndNetworkTaskRestart();
result = true;
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
result = true;
} finally {
try {
SocketDisconnect();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("AsyncTask", "doInBackground: Finished");
}
return result;
}
Socket Initializzation:
public void SocketInit(String ip, int port) throws IOException {
InetAddress addr = InetAddress.getByName(ip);
SocketAddress sockaddr = new InetSocketAddress(addr, port);
nsocket = new Socket();
nsocket.setReuseAddress(false);
nsocket.setTcpNoDelay(true);
nsocket.setKeepAlive(true);
nsocket.setSoTimeout(0);
nsocket.connect(sockaddr, 0);
StartInputStream();
StartOutputStream();
}
Read from Socket:
private void SocketUpdate() throws IOException, ClassNotFoundException {
int read = 0;
// If connected Start read
if (socketSingleton.isSocketConnected()) {
// Print "Connected!" to UI
setPublishType(Publish.CONNECTED);
publishProgress();
if(mConnectingProgressDialog != null)
mConnectingProgressDialog.dismiss(); //End Connecting Progress Dialog Bar
//Set Communications Up
setCommunicationsUp(true);
Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
Log.i("AsyncTask", "doInBackground: Waiting for inital data...");
byte[] buffer = new byte[3];
do{
nis.readFully(buffer, 0, 3);
setPublishType(Publish.READ);
publishProgress(buffer);
}while(!isCancelled());
SocketDisconnect();
}
}
Streams init:
public void StartInputStream() throws IOException{
nis = new DataInputStream(nsocket.getInputStream());
}
public void StartOutputStream() throws IOException{
nos = nsocket.getOutputStream();
}
Read and Write methods:
public int Read(byte[] b, int off, int len) throws IOException{
return nis.read(b, off, len); //This is blocking
}
public void Write(byte b[]) throws IOException {
nos.write(b);
nos.flush();
}
public boolean sendDataToNetwork(final String cmd)
{
if (isSocketConnected())
{
Log.i("AsyncTask", "SendDataToNetwork: Writing message to socket");
new Thread(new Runnable()
{
public void run()
{
try
{
Write(cmd.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
Log.i("AsyncTask", "SendDataToNetwork: Message send failed. Caught an exception");
}
}
}).start();
return true;
}
Log.i("AsyncTask", "SendDataToNetwork: Cannot send message. Socket is closed");
return false;
}
The application is very simple, the android app sends a command(via sendDataToNetwork method) to the IoT device and the latter sends back an "ACK" Command string.
The problem
The problem is that while the IoT device always receives the command, the smartphone rarely gets the ACK back. Sometimes i get something like "ACKACKACKACK". By debugging the IoT device i'm sure that it successfully sends back the ACK, so the problem lies in the InputStream read() method which doesn't retrieve the string right away.
Is there a way to empty the InputStream buffer right away, so that i get an "ACK" string back from the IoT device every time i send a command?
Update
I've updated the socket config so that there are no more buffer limitations and i've replaced read() method with readFully. It greatly improved, but still make some mistakes. For istance one out of 2-3 times no ack is received and i get 2 ack the next turn. Is this perhaps the computational limit of the IoT device? Or is there still margin for a better approach?
the problem lies in the InputStream read() method which doesn't empty the buffer right away.
I don't know what 'empty the buffer' means here, but InputStream.read() is specified to return as soon as even one byte has been transferred.
Is there a way to empty the InputStream buffer right away, so that i get an "ACK" string back from the IoT device every time i send a command?
The actual problem is that you could be reading more than one ACK at a time. And there are others.
If you're trying to read exactly three bytes, you should be using DataInputStream.readFully() with a byte array of three bytes.
This will also get rid of the need for the following array copy.
You should not mess with the socket buffer sizes except to increase them. 20 and 700 are both ridiculously small values, and will not be the actual values used, as the platform can adjust the value supplied. Your claim that this improved things isn't credible.
You should not spin-loop while available() is zero. This is literally a waste of time. Your comment says you are blocked in the following read call. You aren't, although you should be. You are spinning here. Remove this.
So I have a piece of code which works correctly on all devices and emulators I have access to, except one. It's a cheap old Android device, Huawei Y330-U01, running 4.2.2. I'm compiling with com.google.android.gms:play-services-drive:9.8.0. It's absolutely standard, as far as I can tell.
I get the file, which is over a megabyte of plain text, and I can read it character by character, for a few thousand characters (the amount varies, and not between numbers which are powers of two or anything), before getting the error
IOException while testing the stream's first character
java.io.IOException: read failed: EBADF (Bad file number)
at libcore.io.IoBridge.read(IoBridge.java:486)
at java.io.FileInputStream.read(FileInputStream.java:179)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at java.io.FileInputStream.read(FileInputStream.java:175)
at com.suchideas.android.alamode.sync.SyncActivity$b.run(Unknown Source)
Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
at libcore.io.Posix.readBytes(Native Method)
at libcore.io.Posix.read(Posix.java:123)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
at libcore.io.IoBridge.read(IoBridge.java:476)
at java.io.FileInputStream.read(FileInputStream.java:179)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at java.io.FileInputStream.read(FileInputStream.java:175)
at com.suchideas.android.alamode.sync.SyncActivity$b.run(Unknown Source)
I'm pretty confident this is something like running out of RAM or disk space (there's certainly more than enough enough space for this file, by hundreds of megabytes, but the device does like to complain about storage) and clearing away something which was actually in use. Again, to reiterate, this code works perfectly on emulators of the same Android version, and all other devices tested.
So. Is there a fix, do you think?
Here's the code, you should be able to fill in the gaps...
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
while (mGoogleApiClient.isConnecting()) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!mGoogleApiClient.isConnected())
return;
}
appFolder = Drive.DriveApi.getAppFolder(mGoogleApiClient);
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, UPLOADED_DATABASE_NAME))
.build();
DriveApi.MetadataBufferResult metadataBufferResult = appFolder.queryChildren(mGoogleApiClient, query).await();
if (!metadataBufferResult.getStatus().isSuccess()) {
metadataBufferResult.release();
return;
}
MetadataBuffer databaseFileResults = metadataBufferResult.getMetadataBuffer();
if (databaseFileResults.getCount() == 0) {
return;
}
Metadata md = databaseFileResults.get(0);
Log.d(TAG, "Database file retrieved [" + md.getFileSize() + "B]. Created " + md.getCreatedDate() + ", modified " + md.getModifiedDate() + ".");
DriveId databaseFileID = md.getDriveId();
databaseFileResults.release();
metadataBufferResult.release();
DriveFile databaseFile = databaseFileID.asDriveFile();
DriveApi.DriveContentsResult driveContentsResult = databaseFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, new DriveFile.DownloadProgressListener() {
#Override
public void onProgress(long downloaded, long expected) {
}
}).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
InputStream in = driveContents.getInputStream();
try {
int c = 0;
for(int i = 0; true; i++) {
c = in.read();
if(c == -1) break;
Log.d(TAG, "Character "+i+": "+(char)c);
}
} catch (IOException e) {
Log.e(TAG, "IOException while testing the stream character", e);
return;
}
Okay, so one can almost certainly do better than this (I don't think you need to read character by character, some buffering is probably okay), but after a few hours of battling, I found a way to avoid triggering the issue on this device.
In practice, I would recommend trying a normal driveContents.getInputStream() first. Then one can catch the sort of errors discussed above, and only turn to this approach if it becomes necessary.
But it works.
The approach: open the DriveContents directly from its FileDescriptor rather than through an InputStream. Gradually build this up in a buffer (I'm just using a StringBuilder here, since this was proof-of-concept). Catch IOExceptions, and if you've successfully read at least some data, start all over again, and keep going, until you reach the end of the string.
private static String safeDriveFileToString(DriveContents driveContents) throws IOException {
StringBuilder sb = new StringBuilder();
InputStream in;
int n = 0, nPrevious = 0;
while(true) {
in = new FileInputStream(driveContents.getParcelFileDescriptor().getFileDescriptor());
try {
int toSkip = n;
while(toSkip > 0) {
toSkip -= in.skip(toSkip);
}
int c;
while ((c = in.read()) != -1) {
sb.append((char) c);
n++;
}
if(c == -1) break;
} catch (IOException e) {
if(nPrevious == n) {
throw e;
} else {
Log.e(TAG, "Ignoring error part-way through a file:", e);
}
nPrevious = n;
}
}
return sb.toString();
}
Wanna know the weirdest thing? After reading this file once with such an approach, it now always works without needing to recourse to this. Absolutely bizarre.
I have been using InputStream.read( byte[] b, int off, int len ) method to read in data, but now have run into a timeout problem. I am sometimes expecting timeouts from reading, and should have the program adjust itself accordingly after a timeout. I have tried to implement a Thread but I really know nothing about Threads and cannot get it to work. I also want to add that this thread is being initialized within another thread. I'm not sure what the implications of this are but it may cause a problem.
My initial code had worked for the majority of times I need to read, but whenever I'm expecting a timeout, my program freezes at the read() call and never times out. When I implemented this new code, the times when my initial code worked now time out. I use Thread.wait(500) which I assume is 500 milliseconds, but I cannot find any Javadocs including the wait() function. Here and Here.
Other posts relating to this: 1, 2, 3.
I have also looked into declaring a timeout for the BluetoothSocket, but I cannot find it anywhere in the documentation.
Here is what my initial code looks like:
public void run(int length) throws IOException {
buffer = new byte[1024];
try {
bytes = mmInStream.read(buffer, 0, length);
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Message msg = mHandler.obtainMessage(MainMenu.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString( TOAST, "Device has disconnected from the Bluetooth Module." );
msg.setData(bundle);
mHandler.sendMessage(msg);
connectionLost();
BluetoothService.this.start();
}
This is what I have tried to implement:
public void run(int length) throws IOException {
buffer = new byte[1024];
length1 = length;
Thread myThread = new Thread(new Runnable() {
public void run() {
try {
bytes = mmInStream.read( buffer, 0, length1 );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
synchronized (myThread) {
myThread.start();
try {
myThread.wait(500);
if(myThread.isAlive()) {
mmInStream.close();
Log.i( "InStream", "Timeout exceeded!");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
myThread.run();
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Message msg = mHandler.obtainMessage(MainMenu.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString( TOAST, "Device has disconnected from the Bluetooth Module." );
msg.setData(bundle);
mHandler.sendMessage(msg);
connectionLost();
BluetoothService.this.start();
}
EDIT:
So I'm trying to recreate
buffer = new byte[1024];
bytes = mmInStream.read(buffer, 0, length);
I have done the following:
Scanner scan = new Scanner(new InputStreamReader(mmInStream));
String readIn;
try {
readIn = scan.next();
bytes = 5; // I tried with or without this, since I do not think it matters...
buffer = readIn.getBytes( Charset.forName( "US-ASCII" ) );
}
Later in my code I make this call....Sorry edit again, the buf=read( 5 ) call goes to what is shown above.
byte[] buf = buffer;
write( a );
buf = read( 5 );
Log.i(TAG, "Before buf[5]" );
try {
buf[5] = '\0';
} catch( NullPointerException e ) {
return false;
}
When I use the original method, It passes this buf[5] call fine. But when I use the new method, it gives me an IndexOutOfBoundsException at that spot. Am I missing something? The expected input should be CMD\r\n
The bluetooth chat example is really poor in this respect, you should use an input scanner instead of mmInStream.read. Here's what I use and it works reasonably well...
For your use case you skip the entire buffer and byte and write and read (no need to use any of those when you are using a scanner and inputstreamreader as those handle that stuff for you)... in other words the below code takes care of all that for you. I changed the delimiter for you to CRLF. What the code below does is you send a string and it writes it and then reads. If you don't need to send anything to the remote device, just start at scan = new Scanner. Each time a line is read and it ends with \r\n it will store it in the string instring.
So if you want to send "a", you would write
String readIn = beginListenForData("a");
The a will be sent under the mmOutStream and then the scanner will read the mmInStream and collect all the characters, then once it sees a CRLF it will return the characters it read and return them in your readIn string. Make sense?
private String beginListenForData(String msg0) {
msg0 += "\r"; //this adds a return character to the string, you can omit this if you just send an a and the remote device understands what that means.
String instring = "";
try {
mmOutStream.write(msg0.getBytes());
} catch (IOException ex) {
stop();
}
scan = new Scanner(new InputStreamReader(mmInStream));
scan.useDelimiter(Pattern.compile("[\\r\\n]+"));
instring = scan.next();
scan = null;
return instring;
}
I have a device here that I can send a status request command to, and then I read it using
bytes = mmInStream.read(statusBuffer);
I'm having trouble when it changes it's status though. Sometimes I will get back the current status, other times the program will hang on that line and not do anything else. It doesn't crash, move onto the next line or anything. I can only move on by turning off the device and severing the connection.
We have a blackberry torch here that does not have this error at all so it must be my code.
Can anyone give me some troubleshooting tips? Below is the while loop that reads the devices current status.
while (true) {
getStatus();
try {
bytes = 0;
while(bytes < 1){
bytes = mmInStream.read(statusBuffer);
if (bytes != 0){
response = new String(statusBuffer);
//Handle response code
}
}
}
} catch (Exception e) {
Log.e(TAG, "disconnected FROM WHILE TRUE LOOP", e);
connectionLost();
break;
}
}
If there is no byte available, -1 is returned by read, you miss your comparison and you go on looping.
If your line ends with a \n char it would be far easier to read it through an BufferedReader for instance. Anyhow, your loop is not very well designed.
do
{
//read bytes
//store the result in byteRead
//if( byteRead != -1 )
//build a string
}//do
while( byteRead != -1 )
Regards,
Stéphane
I have implemented a bluetooth connection using the now-classic Google Bluetooth Chat code. However, I have a question which I just cannot seem to wrap my brain around.
The reading of the input stream goes something like this:
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
Now, that's fine if I was just printing out the characters I was receiving as in the original example. However, suppose I wanted to transfer an image file. I don't know the size of the file, so I cannot count the bytes received or anything like that. In my tests, I don't seem to be ever receiving a "-1" from the input stream, which appears to be the "norm" for reading from input streams. So how can I know that I have reached the end of the file that was being sent?
Thank you for your help and your time.
It seems Android bluetooth input streams never return -1.
I guess setup a simple protocol by sending file size in the first place and EOF signals at last will help.
No it does not. Android sends -1 only when the Socket is closed as far as I know. So a workaround could be to do a reconnect, but I was trying that for hours and did not get it working, since I do not understand this "special" Code here (copied from a Stackoverflow Thread) for setting up the socket:
BluetoothSocket tmp = null;
Log.d(TAG, "New Connection initialized");
Method m;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
This Socket only works, when my App is started for the first filetransfer. If I want to "Reconnect" with a completely new instantiated Object (and a new Socket created with that Code), the program freezes on the blocking method mmSocket.connect(). It seems like the Method never comes to an ending. This is driving me nuts...
Try
while ((bytes = mmInStream.read(buffer) != -1)
and see if that helps.
Try this:
public void run() {
byte[] buffer;
ArrayList<Integer> arr_byte = new ArrayList<Integer>();
while (true) {
try {
int data = mmInStream.read();
if(mmInStream.available()>0) {
arr_byte.add(data);
} else {
arr_byte.add(data);
buffer = new byte[arr_byte.size()];
for(int i = 0 ; i < arr_byte.size() ; i++) {
buffer[i] = arr_byte.get(i).byteValue();
}
Log.e("INPUT",new String(buffer));
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
arr_byte = new ArrayList<Integer>();
}
} catch (IOException e) {
break;
}
}
}