I have a problem with my TCP connection. I send data (a simple string) via a smartphone to a tablet by a TCP socket connection. The connection works fine and data is transmitted as expected. But when I do a loop and in every iteration dos.write() is fired only one packages arrive on the tablets data receiver. What am I doing wrong?
Here's the sending part of my connection. It iterates through the list and writes every data to the DataOutputStream
.
for(int i = 0; i <= logList.length - 1; ++i){
String backupPayload = invertLogStringToJson(logList[i]);
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(backupPayload);
dos.flush();
dos.close();
On the tablet I receive the data via this code snippet:
@Override
public void run() {
try {
while(true){
mySocket = ss.accept();
dis = new DataInputStream(mySocket.getInputStream());
message = dis.readUTF();
handler.post(() -> {
bufferIntentSendCode.putExtra("data", message);
ctx.sendBroadcast(bufferIntentSendCode);
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
As I said the connection works fine when I send only one data package. But if I want to send multiple packages inside the loop only the first package will arrive at the destination.
Can anyone help me? :)