I am using a Socket connection to connect to a database. When I pull data from the database via InputStream, sometimes it returns full data but most of the time it returns incomplete data (5%/95%). There's an EOF that the database sends but it only gets that far about 5% of the time.
#Override
protected Socket doInBackground(Void... params) {
Socket client = null;
try {
client = new Socket(SocketHelper.IP_ADDRESS, SocketHelper.PORT);
//output message
OutputStream output = client.getOutputStream();
output.write(message.getBytes("UTF-8"));
//parse response
//blocks here more of the time
SocketHelper.parseData(client.getInputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return client;
}
In my parse method, I look for the EOF and would normally close the InputStream and close the Socket there. I can't find any consistencies for the times it does reach the EOF. Right now the length of the data is static, I haven't adjusted it while I"m trying to figure this out. But the length of the data will be dynamic.
To read data from a Socket, in your parseData method you need to loop, calling read(byte[] b, int off, int len) until all the bytes you expect have arrived.
The read method returns the number of bytes actually read; keep looping until you've got everything. Remember to adjust the offset argument, so that you build up the full message.
Related
I am trying to upload Images/Videos which are taken through Device camera to server at a specific folder which can be retrieved later in a dashboard.
I have gone through numerous posts and tutorials and all of them are basically using a JSP to choose a file and then upload it or they are using PHP as a server side code to upload it.
I have my whole backend developed in JAVA SERVLET and I need to include this upload/download functionality.
Basically what I want is to make a POST request using Retrofit or Volley to make a server request and file should be uploaded. (It's like when we use POSTMAN to fire an api call and choose an image as binary file to upload).
Links which I have tried :
Link 1 , Link 2, Link 3 and a lot more. All of them include JSP or something to choose file, I need to pass the media(image/video) as a parameter to the POST request.
So I finally managed to achieve it. I had to post an image/video as well as a JSON corresponding to that media.
My solution is as follows :
#WebServlet("/ImageUploadServlet")
#MultipartConfig
public class ImageUploadServlet extends HttpServlet {
..............
.............
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long req_received_time=System.currentTimeMillis();
String to_be_saved_location="";
System.out.println("JSON received is : "+request.getParameter("input_json"));
JSONObject req = null;
try {
req = readPOST(request.getParameter("input_json"));
to_be_saved_location = "your_location";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
SqlUtil.incident_reporting(xxx);// function to enter data in sql
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = request.getPart("image").getInputStream();//change it to video(it's just a parameter name)
OutputStream out = new FileOutputStream("/Users/driftking9987/Documents/Stuffs/"+to_be_saved_location+".jpg");//Add .mp4 for video
//OutputStream out = new FileOutputStream("/var/www/html/media/abc.mp4");
copy(in, out); //The function is below
out.flush();
out.close();
}
public static long copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
While saving it on the server, I gave the tomcat user the permission to write in the media folder.
Below is the POSTMAN screenshot.
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.
I am trying to receive image stream from desktop in the android phone. When I put decodeStream() method in my thread, then put decodestream() into runOnuiThread() to display in the imageView, it takes more than 7 secs. But when I try to directly read image from asset, and convert into inputstream, then decodeStream(), it may take 500ms, I don't know why it happens. The image is 100K.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
SocketConnect = new socketConnect();
SocketConnect.connectsocket("134.129.125.126", 8080);
while (true) {
data = new byte[2048 * 2048];
try {
read = SocketConnect.getInputStream().read(data, 0,
data.length);
input = SocketConnect.getInputStream();
System.out.println("getInputStream()");
bitmap = BitmapFactory.decodeStream(input);
System.out.println("decodestream()");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println(e);
}
runOnUiThread(new Runnable() {
public void run() {
image.setImageBitmap(bitmap);
System.out.println("setImage at less than 500");
}
});
}
}
});
thread.start();
client side should sent the image very 5 sec.
If I try to read same image from asset, Image display immedately.
try {
inputasset=getAssets().open("good1.jpg");
Bitmap bit = BitmapFactory.decodeStream(inputasset);
image.setImageBitmap(bit);
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
}
socketConnect class
public class socketConnect {
private Socket clientSocket;
private InputStream input;
private Bitmap bMap;
public InputStream getInputStream() {
return input;
}
public void connectsocket(String ipString, int port)
{
System.out.println("starts");
try {
// clientSocket = new Socket("134.129.125.172",8080);
System.out.println("starts");
clientSocket = new Socket(ipString, port);
System.out.println("AsyncTask: Connect to 134.129.125.126");
input = clientSocket.getInputStream();
System.out.println("AsyncTask: get the inputStream");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
read = SocketConnect.getInputStream().read(data, 0,
data.length);
input = SocketConnect.getInputStream();
System.out.println("getInputStream()");
bitmap = BitmapFactory.decodeStream(input);
System.out.println("decodestream()");
You read the data from the input stream, then you ask the BitmapFactory to read the data from the input stream. But you already read it! Remove the call to read -- you're stealing data from the BitmapFactory.
My guess would be that the server side is not properly signaling that the end of the file has been reached. I've encountered this previously because the web server implementation might not tell you the length of the file it is sending. If it doesn't set the content length then the input stream keeps trying to read forever. My guess is that it takes a number of minutes because this is the default socket timeout value.
So, the input stream from the socket reads as much as it can and keeps waiting for more data. After waiting several minutes (although usually 5 minutes is the default timeout) the socket closes and you get your image.
I'm not entirely sure that a content length will help, another option might be that the server closes the connection after all the data is sent.
Clearly, if you're trying to stream new images though, you need to do something different than you are. Perhaps have your server send a file header that the reader interprets. This header might include the length of the data being sent and then the client you write stops reading after reaching this length. In this case you'll need to read the image data into a buffer and then have the BitmapFactory read the buffer instead of the stream.
An alternate approach would have the server close the connection after sending the data for each image and then the client would reconnect to get the next one.
Bottom line, not all InputStreams are created equal and a SocketInputStream doesn't know when to close without a content length or the socket being closed.
My problem is when it tries to read the object the second time, it throws the exception:
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at Client.run(BaseStaInstance.java:313)
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at Client.run(BaseStaInstance.java:313)
The first time I send the exact same object message; however, when I try doing the same thing the second time, it throws the error above. Do I need to re-intialize the readObject() method? I even printed out the message object that is being received by the line below and its exact the same as the first instance where it works ok.
Object buf = myInput.readObject();
I'm assuming there's some problem with appending, but I really have no use for appending. I just want to read a fresh line everytime.
I'd really appreciate some help in fixing this bug. Thank you.
==================================
Before that one line, I'm just creating the input and output objects for the socket in the run() method. The object declaration is outside the run() method in the class:-
#Override
public void run() {
try {
sleep((int) 1 * 8000);
} catch (Exception e) {
e.printStackTrace();
}
try {
//Creating input and output streams to transfer messages to the server
myOutput = new ObjectOutputStream(skt.getOutputStream());
myInput = new ObjectInputStream(skt.getInputStream());
while (true) {
buf = myInput.readObject();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
You're right; I don't close the object. I'm not sure how to do that.
The underlying problem is that you are using a new ObjectOutputStream to write to a stream that you have already used a prior ObjectOutputStream to write to. These streams have headers which are written and read by the respective constructors, so if you create another ObjectOutputStream you will write a new header, which starts with - guess what? - 0xAC, and the existing ObjectInputStream isn't expecting another header at this point so it barfs.
In the Java Forums thread cited by #trashgod, I should have left out the part about 'anew for each object at both ends': that's just wasteful. Use a single OOS and OIS for the life of the socket, and don't use any other streams on the socket.
If you want to forget what you've written, use ObjectOutputStream.reset().
And don't use any other streams or Readers or Writers on the same socket. The object stream APIs can handle all Java primitive datatypes and all Serializable classes.
Below is the receive method that implements a socket server and works perfectly.
private void Receive(){
log.info("Server started - waiting for the clients.");
try {
Boolean end = false;
ServerSocket ss = new ServerSocket(12345);
while(!end){
//Server is waiting for client here, if needed
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
JSONObject jsonObj;
try {
jsonObj = new JSONObject(st);
long id = jsonObj.optLong("DeviceID", count.addAndGet(1) );
String name = jsonObj.toString();
table.put(id, name);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
etResult.setText(st);
Log.d("Tcp Example", "From client: "+st);
output.println("Response from Sever: Connectivity ok");
s.close();
if (st != null ){ end = true; }
}
ss.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The only problem is, when I hit the button to call that method, the socket starts listening and waits for a client messages. While it does not happen, the app remains freezes and I try to hit any other button, the app may crashes.
Does anyone have a hint about how could handle it and leave the socket listening in "background" withou locking the entire screen?
thank you
Make a thread or AsyncTask and do all the socket functions on that. IF it's something you're going to rarely do and want to fire it off and process the results, use an AsyncTask. If it's something you're going to want to do constantly and don't want to run multiple workers at the same time or have multiple workers queued up, use a Thread.