Building on my network/socket example, I'm trying to demonstrate sending data objects larger than the packet size limit of 1.4kb. As far as I can tell, that part works. I'm sending an image by create a memblock and sending the bytes over to the client (same as the server in this case) and creating a new memblock to write the data. Then I create an image from the memblock and finally create a sprite. The image dimensions seem to carry over just fine, but for some reason the color data is all zeros. The bytes are sent the exact same way, so I'm not sure why the first 12 bytes are perfectly fine but the rest are not. I'm not sure if its because sockets send signed bytes when everything else in agk using unsigned or what.
// Project: socket
// Created: 2023-11-17
SetErrorMode(2)
SetWindowTitle( "socket" )
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//SetSyncRate(30, 0)
// The listener is our server. In this example, we will listen for incoming
// connections on the localhost at port 8080
listener = createSocketListener("127.0.0.1", 8080)
// This will be the server's connection to connected client. In a true server
// this would be an array as you'd potentially have multiple clients connecting.
// For this example, we'll only deal with a single client, ourself.
conn = 0
// The client's connection to the server.
client = 0
fromServer$ = ""
fromClient1$ = ""
fromClient2$ = ""
msg$ = "Client disconnected."
#CONSTANT DATA_MEMBLOCK_IMG = 2
// Data coming from server
img = loadImage("powercell64.png")
m = createMemblockFromImage(img)
mSize = getMemblockSize(m)
sendingMemblock = 0
mPointer = 0
// Used on client
doingLargeThing = 0
clientMem = 0
cPointer = 0
clientImg = 0
do
// Free the sockets on hitting ESC and exit the app
if getRawKeyPressed(27) = 1
deleteSocketListener(listener)
deleteSocket(conn)
deleteSocket(client)
end
endif
// Press 'c' to connect the client to the server
if getRawKeyPressed(67) = 1
client = connectSocket('127.0.0.1', 8080, 3000)
endif
if sendingMemblock = 1
//SendSocketByte(conn, mPointer)
packSize = mPointer + 1000
if packSize > mSize then packSize = mSize-1
for i = mPointer to packSize
SendSocketByte(conn, getMemblockByte(m, mPointer))
t = getMemblockByte(m, mPointer)
inc mPointer
next i
flushSocket(conn)
if packSize = mSize-1
sendingMemblock = 0
//SendSocketString(conn, "END")
//flushSocket(conn)
endif
endif
// Press 'spacebar' to send timestamp from server to the client
if getRawKeyPressed(32) = 1
// Make sure the client is still connected
if getSocketConnected(conn) = 1
// Send our data to the buffer
//sendSocketString(conn, str(GetMilliseconds()))
if sendingMemblock = 0
sendingMemblock = 1
SendSocketByte(conn, DATA_MEMBLOCK_IMG)
SendSocketInteger(conn, mSize)
// Flush the data. Until you flush the socket, anything
// you write to the buffer will sit there.
// Caution: Once the buffer has reach 1.4KB, it will automatically flush the buffer.
flushSocket(conn)
endif
endif
endif
// Press 's' to send data from the client to the server
if getRawKeyPressed(83) = 1
// Check client is still connected to server
if client > 0
if getSocketConnected(client) = 1
h$ = hex(val(fromServer$))
// While we had the server send us only 1 string,
// we're going to have the client send 2 strings back to the server.
// First string will take the timestamp this client received from the server and
// convert it to hex. The second string will be the hex value encoded in base64.
sendSocketString(client, h$)
sendSocketString(client, hexToBase64(h$))
flushSocket(client)
endif
endif
endif
// Until we have a client connected, the server will
// continue to listen for incoming connections. You
// would modify this to always listen if your server
// is to handle multiple clients at once.
if conn = 0
lc = getSocketListenerConnection(listener)
if lc > 0 then conn = lc
else
// If server is connected to a client, listen for data from that client
if getSocketConnected(conn) = 1
if getSocketBytesAvailable(conn) > 0
// This example expects 2 strings sent from the client
fromClient1$ = getSocketString(conn)
fromClient2$ = getSocketString(conn)
endif
endif
endif
// The client listens for incoming data from the server
if client > 0
// Make sure client is still connected to the server
if getSocketConnected(client) = 1
msg$ = "Connected to server at " + getSocketRemoteIP(client)
// Check if any data is awaiting to be read
if getSocketBytesAvailable(client) > 0
// In our example, we expect to get a string
//fromServer$ = getSocketString(client)
// Not pending any large/chunked transfers
if doingLargeThing = 0
code = getSocketByte(client)
if code = DATA_MEMBLOCK_IMG
clientMemSize = getSocketInteger(client)
clientMem = createMemblock(clientMemSize)
cPointer = 0
doingLargeThing = 1
endif
else
while getSocketBytesAvailable(client) and cPointer < clientMemSize
setMemblockByte(clientMem, cPointer, getSocketByte(client))
inc cPointer
if cPointer >= clientMemSize
doingLargeThing = 0
clientImg = createImageFromMemblock(clientMem)
cs = createSprite(clientImg)
exit
endif
endwhile
endif
endif
else
msg$ = "Client disconnected."
endif
endif
print(t)
print(msg$)
print("")
print("Client received message from server: "+fromServer$)
print("")
print("Server received client's response 1: "+fromClient1$)
print("Server received client's response 2: "+fromClient2$)
print(str(cPointer) + " / " + str(clientMemSize))
p = floor(((cPointer+0.0) / clientMemSize) * 100)
if clientMem > 0
print(str(p)+"% completed.")
endif
if cs > 0
setSpritePosition(cs, getRawMousex(), getRawMouseY())
print(getSpriteWidth(cs))
endif
Sync()
loop