I have problem when I establish socket server, and client could send file to me and I could receive that.But when I tried to send ACK message to client, they can not receive that.
dis = new DataInputStream(client.getInputStream());
File file = new File("D:/socket/files/");
if (!file.exists()) {
file.mkdir();
}
fos = new FileOutputStream(new File(filePath));
inputByte = new byte[1024];
System.out.println("Start to receive file");
while ((length = dis.read(inputByte, 0, inputByte.length)) > -1) {
String s = new String(inputByte,0,length);
fos.write(inputByte, 0, length);
fos.flush();
}
System.out.println("File Received Location: " + filePath);
OutputStream out = client.getOutputStream();
PrintWriter pw=new PrintWriter(out);
pw.write("hello");
pw.flush();
pw.close();
This is my server receive and send message code. I used wireshark to find that after client send data to me, I will automatically send [ACK] message back without any content, then client send [FIN,ACK] to me, I could send two message back, one is [ACK] and another is [PSH,ACK]. "hello" message is inside [PSH,ACK] packet. I think the reason is when client send[FIN,ACK] to me, they already closed connection. Is there anyway I can add message in the first [ACK] packet I send back to client?wireshark-capture
No. But why would you do that ? Your client is sending a data, and once acknowledged (ACK) it is closing the connection. It wouldn't even read the data the server is sending. The real question is, does the client expect data ? It seems that no.