UDP & Recieve messages

BlitzMax Forums/BlitzMax Programming/UDP & Recieve messages

ghislain(Posted 2007) [#1]
Hello,

I've got this flightsim [x-plane] which can communicate with external programs using UDP. I would like to create an udp-connection in Blitzmax and recieve some sim-parameters but I'm just not able to get it working.
It can't be that difficult i guess. I've got another basic-programming-language which accomplished this quite easily.

I've got x-plane set up to send data to '127.0.0.1' port:8080.

Is there anybody who can show me a simple example how to retrieve this udp data?

BTW I tried to use Bnetex but it seems it doesn't work on my Intel Mac..

Ghislain


ghislain(Posted 2007) [#2]
O well,

I've got something working.

Strict

Local socket:TSocket = CreateUDPSocket()
If socket = Null Then RuntimeError "Couldn't create UDP socket"

' Listen to the port!
BindSocket(socket, 8080)

' This isn't required..
' ConnectSocket(socket, HostIp("127.0.0.1"), 8080)

' This seems to be always true..
' If SocketConnected(socket) Then Print "Socket connected."

' Returns 0.0.0.0 : 0
' Print DottedIP(SocketRemoteIP(socket)) + " : " + SocketRemotePort(socket)

Local counter:Int = 0
Local l:Int = 0

While Not KeyDown(key_escape)
	l = SocketReadAvail(socket)
	
	If l
		counter = counter + 1
		Print "Avail: " + l + " bytes."
		Print "Data: " + sRead(socket)
	End If
	
	If counter > 10 Then Exit
Wend

CloseSocket(socket)
End

' ---------------------------------

' This is borrowed from 'Yan'..
Function sRead:String (socket:TSocket)
	Local buf:Byte[1024]
	Local receivedLen = socket.Recv(buf, 1024)
	Return String.FromBytes(buf, receivedLen)
End Function


I borrowed some code from another posting:
BlitzMax Forums/BlitzMax Programming/UDP example

Ghislain