I have a program in c++ that sends a socket of 23723 bytes to a flash player in one “shot”.
The flash player sometimes receives two packets of size 17520 and 6203, and other times it receives a single packet of 23723.
EDIT: There does not seem to be a way to obtain the total number of bytes associated with the send data from flash. This would make it very difficult to build a loop to reconstruct the “broken” packet.
I thought TCP was supposed to correctly recombine the packets and I am having difficulty recombining them myself.
This is my receiving handler in flash:
var socket:Socket = new Socket();
socket.addEventListener(Event.CONNECT, socketConnectHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataReceivedHandler);
socket.connect("127.0.0.1", 7445);
socket.writeUTFBytes("hi");
private function socketDataReceivedHandler(event:ProgressEvent):void {
var socket:Socket = event.target as Socket;
trace(socket.bytesAvailable);
var data:ByteArray = new ByteArray();
socket.readBytes(data);
}
The command:
trace(socket.bytesAvailable);
will at times print 23723, and at other times, will print 17520 quickly followed by 6203. The total size are just examples and are subject to change for each send sockets. Also many sockets can be sent or received at the same time, so the split packets can be inter-mixed. In c++ I could determine what the total size of the sent data was, even if I did not receive the whole sent data yet.
This has nothing to do with tcp. The socket can send you any number of bytes as long as it is at least one byte. So you might get [1, 23722]. The correct way to do this is to call read in a loop and fill an array yourself. If your code does not contain a loop but works, then it works by accident. This cannot work without a loop.
Edit: do not use bytesAvailable. Check the return value of the read function. Check your code: If your code relies on bytesAvailable it is wrong.
Edit2: Looks like the flash api (which I do not know) works differently from the socket api that all other frameworks and languages use. I assumed it would work the same, so let me correct: You are supposed to have the following loop: