I feel like I am back to step 1...
I had a socket exchange between two my Apps, on local network, on OSX,iOS and Android. They were mostly sending each other small arrays.
Then I had to solve the problem with larger arrays, since my procotol based on "read until x" did not work anymore.
I came up with what follows below and I am wondering if it is correct and it is the best/fastest way to have a solid socket communication:
Arrays are treated this way:
Code: Select all
function CodificaDati tData
if tData is empty then return empty
if tData is an array then
return TextEncode(base64Encode(ArrayEncode(tData)),"UTF-8")
else
return TextEncode(base64Encode(tData),"UTF-8")
end if
end CodificaDati
function DecodificaDati pData
if pData is empty then return empty
put base64Decode(TextDecode(pData,"UTF-8")) into pData
try
put ArrayDecode(pData) into TheData
catch errorVariable
put pData into TheData
end try
return TheData
end DecodificaDati
--Sender
Code: Select all
function broadcastClientSend pCommand, pArray
put false into sRePlayReceived --a local flag
put CodificaDati(pArray) into TheMessage
write length(TheMessage) & comma & TheMessage to socket gSocket
wait 500 milliseconds with messages --give it some time to answer
repeat 9
if sRePlayReceived then
put false into sRePlayReceived
read from socket pSocket for 1 items with message "ClientMessageReceived"
return true
end if
wait 500 milliseconds with messages
end repeat
ErrorComHandling 10
return false
end broadcastClientSend
on ClientMessageReceived pSocket, pMsg
put item 1 of pMsg into tSize
if tSize is an integer then
repeat until length(tRD) >= tSize
read from socket pS for 10
put it after tRD
end repeat
breakpoint
put tRD into pMsg
put item 1 of pMsg into tCommand
end if
delete item 1 of pMsg
if tCommand = "CONF" then
put true into sRePlayReceived
else
--do other things
end if
read from socket pSocket for 1 items with message "ClientMessageReceived"
end ClientMessageReceived
Code: Select all
on ServerMessageReceived pSocket, pMsg
put item 1 of pMsg into tSize
if tSize is an integer then
repeat until length(tRD) >= tSize
read from socket p_Socket for 10
put it after tRD
end repeat
put tRD into pMsg
delete item 1 of pMsg --this leaves only the content
write "CONF" & kEndOfFile to socket p_Socket
end if
......... --do something with pMsg
read from socket pSocket for 1 item with message "ServerMessageReceived"
end ServerMessageReceived