ReceiveData callback and userdata

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

ReceiveData callback and userdata

Post by msu » Sun Apr 02, 2017 7:55 pm

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

Greetings, Michael

msu
Posts: 16
Joined: Mon Jun 13, 2016 11:36 am
Location: Sinzig/Germany

Re: ReceiveData callback and userdata

Post by msu » Tue Apr 04, 2017 9:20 pm

Quick support here! :(

xabierpayet
Posts: 160
Joined: Fri Feb 24, 2012 10:34 am

Re: ReceiveData callback and userdata

Post by xabierpayet » Tue Apr 04, 2017 10:53 pm

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: 2477
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany
Contact:

Re: ReceiveData callback and userdata

Post by airsoftsoftwair » Tue Apr 04, 2017 10:54 pm

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