8
I am developing a Socket communication of my program with the server. This server is responsible for bridging between my program and the users that are connected to it, that is, the user when executing a change in the "program A", the user of the "program B" will receive this change and apply the changes automatically. The number of connected programs is undefined. Below is an example diagram:
Where in red is the sending of bytes and in blue is the receiving. The connection between the server and the client is unique and does not close. Where there are two receiving arrows in blue, it means the two changes sent in red.
Everything works correctly. The problem arose when I went to send a larger amount of bytes than I was testing and realized that the Socket object "chopped" the sending of bytes to the server, separating it into 2 packages, which, consequently, was received in two parts.
It turns out that these bytes are part of a single JSON object, and upon receiving the packages, the event ProgressEvent.SOCKET_DATA
is dispatched twice, with the first and second part of the JSON object.
The sending code is:
function enviar(json:Object):void {
var str:String = JSON.stringify(json); // transformo json em string
socket.writeUTFBytes(str); // escrevo a string no objeto socket
socket.flush(); // faço o envio
}
On receiving (This function runs 2 times, "cutting" bytes into two packets):
function receber(e:ProgressEvent.SOCKET_DATA):void {
var str:String = socket.readUTFBytes(socket.bytesAvailable); // transformo os bytes em string
var json:Object = JSON.parse(str);
}
Is there some kind of byte quantity submission limitation? Should I manage packet queues and sort by process? It happens in any language?
Dear biio, the limit of package size exists in any type of programming via Socket, what happens is that it is often done implicitly and is only noticed as in your case where the package arrives separately. I don’t know about actionscript to help you with this, but I do know that with C++ sockets you need to use the OS API to create the socket and also to change the size of the sent and received package. I hope someone can help you more specifically.
– Bruno Bermann