Need to convert simple UDP example
BlitzMax Forums/BlitzMax Programming/Need to convert simple UDP example
| ||
| Coming from B3D, I'm really finding it quite a bit hard adjusting with networking commands using BNet even though it is the closest over BNetEx in terms of command set. From what I understand so far that the read/write strings operations and stream checking are not the same and particularly how udpstream is treated as integer in b3d. Here's the receiver example that works:
AppTitle "Receiver"
;set up address and ports (note that if the sender and client run on different computers, then only 1 port is required)
ipAddress = dotToInt("127.0.0.1")
udpSendPort = 40001
udpRecvPort = 40002
;make stream
udpStream=CreateUDPStream(udpRecvPort)
UDPTimeouts 30 ;wait 30ms to receive see if any message comes in
i=0
Print udpStream
WaitKey
While Not KeyHit(1)
i = i + 1
If udpStream <> 0
recvIP = RecvUDPMsg(udpStream)
If recvIP <> 0
Print ReadString(udpStream)
EndIf
;send a message too
WriteString udpStream, "Test reply " + Str(i)
SendUDPMsg(udpStream, ipAddress, udpSendPort)
Else
Print "No stream created"
EndIf
Wend
CloseUDPStream udpStream
End
Function DotToInt%(ip$)
ip$=Trim$(ip$)
ip1=split$(ip$,1,".")
ip2=split$(ip$,2,".")
ip3=split$(ip$,3,".")
ip4=split$(ip$,4,".")
Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function
Function Split$(in$,n,delim$)
;splits string by one character delimiter
;returns nth component
For i = 1 To n
;find location of next delimiter
;find part to left of delimiter
pos=Instr(in$,delim$)
If pos=0
;no further delimiter found
If i<>n
lh$="" ;there is no nth part of the list, so return ""
Else
lh$=in$ ;we have found the nth part, which is the last in the list
EndIf
Exit ;break out of loop
EndIf
lh$ = Left(in$,pos-1)
;discard part to left of delimiter
in$ = Right(in$,Len(in$)-pos)
Next
Return lh$
End Function
It should just do nothing and wait for the sender code but it throws out error reading/writing to stream with my Blitzmax code below. Here's what I got so far:
Import PUB.BNet
Import BRL.Basic
Import BRL.Retro
AppTitle$ = "Receiver"
'set up address and ports (note that if the sender and client run on different computers, then only 1 port is required)
ipAddress% = IntIP("127.0.0.1")
udpSendPort = 40001
udpRecvPort = 40002
'make stream
InitNetwork
Global udpStream:TUDPStream=CreateUDPStream(udpRecvPort)
UDPTimeouts 30 'wait 30ms to receive see if any message comes in
i=0
While Not KeyHit(KEY_ESCAPE)
i = i + 1
If udpStream <> Null Then
recvIP = RecvUDPMsg(udpStream)
If recvIP <> 0
Print ReadString(udpStream)
EndIf
'send a message too
WriteString udpStream, "Test reply " + String(i)
SendUDPMsg(udpStream, ipAddress, udpSendPort)
Else
Print "No stream created"
EndIf
Wend
CloseUDPStream udpStream
End
Function DotToInt%(ip$)
off1=Instr(ip$,".") ;ip1=Left$(ip$,off1-1).ToInt()
off2=Instr(ip$,".",off1+1);ip2=Mid$(ip$,off1+1,off2-off1-1).ToInt()
off3=Instr(ip$,".",off2+1);ip3=Mid$(ip$,off2+1,off3-off2-1).ToInt()
off4=Instr(ip$," ",off3+1);ip4=Mid$(ip$,off3+1,off4-off3-1).ToInt()
Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function
Function ReadString$(in:TStream)
Local length
length=ReadInt(in)
If length>0 And length<1024*1024 Return brl.stream.ReadString(in,length)
End Function
Appreciate any help to shed some light on this problem. |