I am making app which transfers data using wifi between two devices using hotspot server and client connection.
I am using following code to send data from Client to Server.
OutputStream stream = socket.getOutputStream();
stream.write("".getBytes());
ContentResolver cr = context.getContentResolver();
InputStream is = null;
try {
is = new ByteArrayInputStream(data.getBytes());
} catch (Exception e) {
Log.d("TEST", e.toString());
}
copyFile(is, stream);
The is my copy file method.
public static boolean copyFile(InputStream inputStream, OutputStream out) {
byte[] buf = new byte[1024];
while (true) {
try {
int len = inputStream.read(buf);
if (len != -1) {
out.write(buf, 0, len);
} else {
//out.flush();
//out.close();
return true;
}
} catch (IOException e) {
Log.d("TEST","faaaaaaaaaaaaaaaa::::"+ e.toString());
return false;
}
}
}
And, This is my Client side code.
try {
AppFonts.serverSocket = new ServerSocket(8988);
Log.d("TEST", "Server: Socket opened0000000000");
AppFonts.socket = AppFonts.serverSocket.accept();
Log.d("TEST", "Server: connection done0000000000000000");
// OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
String gotData = convertStreamToString(AppFonts.socket.getInputStream());
Log.d("TEST", "Server: Socket Files,,,,,,");
Log.d("TEST", "server: copying files0000000000 " + gotData);
return gotData;
} catch (IOException e) {
Log.e("TEST", "fsat , " + e.getMessage());
e.printStackTrace();
return null;
} catch (Exception e2) {
e2.printStackTrace();
return null;
}
In copyFile() method i have tried out.flush() but data is not received on client side.
--
- I don't want to close outputstream because I want more communication between two devices.
- when I use
out.close();
data sent to client successfully, but without using that data does not reach to client. - Server is sending data to client from copyFile() method. Data received from client side exactly what is being sent when I use outputstream.close(); but nothing happens when I remove outputstream.close(); or/and use outputstream.flush();. I tried flush() method after seeing Android: Socket is closed this link.