UDP example

BlitzMax Forums/BlitzMax Programming/UDP example

Does anyone have a working UDP example?

Nothing in Code archives. I searched, found nothing except people trying to get UDP to work.

Any help here?

Thanks :D

depends on what your trying to do. i think blitmzax can only communicate with its own udp sockets rather than universal ones.
read my post.
http://www.blitzmax.com/Community/posts.php?topic=57391
its got a basic example of making udp work. but i think it can only connect to another blitzmax app and send and recieve.

I can receive as a server, but I cant narrow it down to what IP:port im getting it from. I cant send as server.

I can send as client, and I believe receive although I cant test receiving.

=/

Kinda odd, how theres no UDP code archives at all, nothing.

Use SocketRemoteIP( socket:TSocket )
on the socket that the client is connected to. i think thats the one. the name makes it seem the logical thing to use. although im not sure.

That only returns an IP though, can't set it.

I can send to a server, use ConnectSocket. Then just write to the socket stream.

That's not too UDP like :P Can't switch to a different IP without having to destroy the socket and stream.

make a new connection with a new socket and stream. ;)

Don't try to use socket streams with UDP sockets, it just isn't going to work.

Either use one of the freely available networking modules available (BMax includes one, well, two really as GNet is a high level mod based on the enet mod), or use raw sockets.

It really depends on what you're trying to do.

or use raw sockets

and how would we do that sir? :)

Use my BNetEx http://vertex.art-fx.org/bnet/bnetex162.zip (object orientated)
or BNet http://vertex.art-fx.org/bnet/bnet160.zip (BlitzBasic like)
Examples are includet.

You can use the code too, to understand, how udp work with BMax.

Raw Sockets are not easy to use.

cu olli

When I said 'raw sockets', I meant via the BMax sockets module as this makes things easier. I'm by no means an expert with sockets but they seem fairly straight forward, it could well be a case of 'ignorance is bliss' thought.

@Moogles - As you asked so nicely, here's simple example you may recognise... ;o)

Strict

Framework BRL.Basic
Import BRL.socket
Import BRL.standardio


Local socket:TSocket = CreateUDPSocket()

If socket = Null Then RuntimeError "Couldn't create UDP socket"

If ConnectSocket(socket, HostIp("83.149.87.203"), 7778) = False Then RuntimeError "Couldn't connect to server"

SWriteLine socket, "\info"

Print DottedIP(SocketRemoteIP(socket)) + " : " + SocketRemotePort(socket)
Print

Local info$ = SRead$(socket)

Print info.length + " : " + info$

socket.Close()

End


Function SRead$(socket:TSocket)
	Local buf:Byte[1024]
	Local receivedLen = socket.Recv(buf, 1024)
	
	Return String.FromBytes(buf, receivedLen)
End Function	

Function SWriteLine(socket:TSocket, str$)
	Local buf:Byte Ptr = (str$ + "~r~n").ToCString()
	Local sent = socket.Send(buf, str.length + 2)

	MemFree buf
	
	Return sent = (str.length + 2)
End Function

A more 'correct' way is probably something like...

Strict

Framework BRL.Basic
Import BRL.socket
Import BRL.standardio

Local socket:TSocket = CreateUDPSocket()

If socket = Null Then RuntimeError "Couldn't create UDP socket"

SWriteLine(socket, "83.149.87.203", 7778, "\info")

Local info$ = SRead$(socket, "83.149.87.203", 7778)

Print info.length + " : " + info$

socket.Close()

End


Function SRead$(socket:TSocket, host$, port, bufferSize=1024)
	Local buf:Byte[bufferSize], rip, rport  
	Local receivedLen, time = MilliSecs()
	
	Repeat
		receivedLen = 0
		Local read = socket._socket
		If socket.Connected()
			Local size = socket.ReadAvail()
			If size
				receivedLen = recvfrom_(socket._socket, buf, size, 0, rip, rport)
				If (rip = HostIp(host$)) And (rport = port) Then Exit
			EndIf
		EndIf	
	Until (MilliSecs() - time) >= 30000
	
	If receivedLen > 0 Then Return String.FromBytes(buf, receivedLen)
End Function	

Function SWriteLine(socket:TSocket, host$, port, str$)
	Local buf:Byte Ptr = (str$ + "~r~n").ToCString()
	Local sent = sendto_(socket._socket, buf, str.length + 2, 0, HostIp(host$), port)

	MemFree buf
	
	Return sent
End Function


If you're only going to be doing simple stuff, like above, the network libraries are overkill IMHO. If, however, you're going to be doing anything mildly useful then use one of the libraries (I haven't used BNet myself, but it seems to be very highly rated by those that have).

ZOMG!. You made it work!! :P
Thanks Ernie! :)
Vertex I tried Bnetex too. its nice but it couldnt read that text from the server like enie's example does.
thanks ernie! er.. sir! :)
[edit] btw the way. your "proper" way doesnt work for that example. :) ill use the improper way thanks :)