ReceiveData callback and userdata

Discuss any general programming issues here
Post Reply
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

ReceiveData callback and userdata

Post by msu »

Hi,
How can I use callback and userdata with ReceiveData(1, #RECEIVEALL,..)?
An example would be nice.

Greetings, Michael
User avatar
msu
Posts: 71
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: ReceiveData callback and userdata

Post by msu »

Quick support here! :(
xabierpayet
Posts: 267
Joined: Fri Feb 24, 2012 9:34 am

Re: ReceiveData callback and userdata

Post by xabierpayet »

here you have a client and a server example

Server:

CreateServer(1,2900)
conexions=0
Function p_ConnectFunc(msg)
Switch(msg.action)
Case "OnDisconnect":
closeconnection(msg.id)
conexions=conexions-1
Case "OnConnect":
conexions=conexions+1
conexioentrant=msg.clientid
Case "OnReceiveData":
data$ = ReceiveData(msg.id, #RECEIVELINE)
senddata(msg.id,data$)
EndSwitch
EndFunction
InstallEventHandler({OnConnect = p_ConnectFunc,OnDisconnect = p_ConnectFunc, onreceivedata=p_connectfunc})
BeginDoubleBuffer()
repeat
cls
checkevent
locate(0,0)
print("conexions:",conexions)
IF CONEXIONS>0 THEN senddata(CONEXIOENTRANT,"PP")
Flip()
forever

and here is the client:

serv$="192.169.1.123"
port=2550
OpenConnection(a, serv$,2900)
nprint("leftmouse to start")
WaitLeftMouse()
while(0=0)
senddata(1,"pp")
wend
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: ReceiveData callback and userdata

Post by airsoftsoftwair »

Probably too easy a question for the folks on here because it's pretty much all said in the documentation :) Well, just do something like this:

Code: Select all

Function p_DumpFunc(msg)
   DebugPrint(msg.count, msg.total, msg.userdata)
   Return(False)
EndFunction

ReceiveData(1, #RECEIVEALL, True, p_DumpFunc, "Hello msu!")
This code will receive all data until the sender closes its connection. The callback will print how many bytes it has just received and the total byte count. It will also print "Hello msu!" because that is the userdata. Note that ReceiveData()'s syntax is different for other types than #RECEIVEALL. But it's all in the documentation... which will also be available in German really soon :)
Post Reply