Posted: 22nd Mar 2021 21:46
Hello

I'm sending a string from a non AppGameKit app to an AppGameKit app. It's semi working now.
The connection is made, but when the string is send, the AppGameKit app hangs, until I close the connection (from the non AppGameKit app)
Then the string is showed.

Why does the app hang? I think AppGameKit expects me to manually clear the buffer after GetSocketString (I think it hangs because it's constantly reading the string) however I cannot find a command to clear the buffer.

See my very simple code attached, does anyone see what I'm doing wrong here?

Thanks!

+ Code Snippet
SetErrorMode(1)
SetWindowSize(600, 600, 0)
Msg As String
ConnectSocket(1, "127.0.0.1", 13244, 3000)
Do
	if (GetSocketConnected(1) = 1)
        Print("Connected!")
       
        if (GetSocketBytesAvailable(1) <> 0)
			msg = GetSocketString(1)
        endif  
    else   
         
        Print("Disconnected !")
             
    endif          
 
	Print(msg)
    Print( ScreenFPS() )
    Sync()
 
Loop


Edit: This is the ByteArray I'm sending: 00 00 00 04 54 65 73 74
The first 4 bytes indicate the length (4) and then an UTF-8 string "Test". But the string receives just fine, but only after I kill the connection (and the AppGameKit app stops hanging)

Thanks for any insight.
Posted: 23rd Mar 2021 9:10
If there are 4 or more bytes waiting to be read then this command will wait until the entire string has been received before returning.


From here:
https://www.appgamekit.com/documentation/Reference/Multiplayer/GetSocketString.htm

This may be your case
For further testing, I would recommend you to get everything byte by byte into a buffer array.
Like:

+ Code Snippet
while GetSocketBytesAvailable( socket )
buff.insert( GetSocketByte( socket ) )
endwhile


Keep us updated
Posted: 23rd Mar 2021 10:25
Hi Jack, Thanks for your respons.

It seems that this is indeed what's happening. The buffer only receives 7 bytes. While this log from Wireshark show that the 8 bytes are actually send.
See image here:


The odd thing is that AppGameKit only registers 7, and keeps waiting for the 8 byte. I don't know why AppGameKit doesn't register the last byte, and only processes this after a disconnect.
Could this be a bug?
Posted: 23rd Mar 2021 11:09
Can you pinpoint it further?
There may be something going on with the GetSocketString command, we don't know yet. (Ive always used bytewise communication and had never a fault)
Endianness is always sort of scary, when implementing a protocol
https://en.wikipedia.org/wiki/Endianness



Try to consider to build another socket sender that you pack and send a string to your current client then look what wireshark says
Posted: 23rd Mar 2021 12:43
Thanks again for thinking with me.

I used another server app named Hercules (https://www.hw-group.com/software/hercules-setup-utility) as TCP server instead of my own. This one produces the exact same result.
I

AGK hangs and continues only after a disconnect (and showing the expected result)
I can also use pure byte communication and use the Chr() function to decode the bytes to string. But that doesn't solve the real problem ofcourse.

I also found another weird thing, replacing the first 4 bytes from 00 00 00 04 to 00 00 00 03 doesn't change a thing. I would expect AppGameKit to print the first 3 characters and let the last one on the buffer. But it doesn't make any difference, the app hangs, and then shows the full 4 characters if the server closes the connection.

I also mailed Paul for this issue. Maybe I'm doing something very stupid here and the solution is easy, but just maybe I just found a real bug
Posted: 23rd Mar 2021 13:17
I searched more on Endianness, I tought it couldn't be the problem because the string came trough as Test and not as tseT. But that's not how it works!
So sending 04 00 00 00 instead of 04 00 00 00 worked. It seems that I was actually sending 67108864 instead of 4, so the app was waiting for the other 67108860 bytes to arrive

Maybe it wouldn't be a bad idea to mention this in the docs

Thanks a lot for your help Jack!