I have 2 devices connected via Socket
Server Code - (Android app):
log("sending song to client - " + clientSocket.getInetAddress().toString());
InputStream fileInputStream = new FileInputStream(songFile);
socketDataOutputStream.writeLong(songFile.length());
Thread.sleep(50);
byte[] byteBuffer = new byte[16 * 1024];
int count;
while ((count = fileInputStream.read(byteBuffer)) > 0) {
socketDataOutputStream.write(byteBuffer, 0, count);
}
log("song sent to client - " + clientSocket.getInetAddress().toString());
socketOutputStream.flush();
log("sending a message to client - " + clientSocket.getInetAddress().toString());
socketDataOutputStream.writeUTF("play");
log("message sent to client - " + clientSocket.getInetAddress().toString());
Client Code - (PC code):
OutputStream fos = new FileOutputStream(song);
InputStream sis = socket.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(sis));
long size = dis.readLong();
int count;
log ("Receiving file");
while (size > 0 && (count = dis.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
fos.write(buffer, 0, count);
size = size - count;
}
fos.close();
log ("File received");
String s;
while ((s = dis.readUTF()) != null) {
log(s);
}
The Song is successfully received but after that no communication with the socket is possible! I have tried different ways - PrintWriter
, write(bytes[])
. Nothing happens - the client side code does not enter the second while
loop.
I don't understand what wrong I'm doing.