UDP Socket Help

BlitzMax Forums/BlitzMax Beginners Area/UDP Socket Help

YellBellzDotCom(Posted 2011) [#1]
Hello, I'm dabbling with UDP sockets in Bmax and of course running into the never ending walls of my mind.

Do you need to accept a socket on the server when using UDP socket commands?

This doesn't work for UDP but does for TCP
Local socket:TSocket = SocketAccept(sock)

If(socket <> Null)	
     'AddClient(New TServerClient.Create(socket))
	Print "New client connected"
End If


Also trying to figure out how to just read a string sent by the client to the server.

Thanks for any help!


Zeke(Posted 2011) [#2]
server:
'server
SuperStrict

Global socket:TSocket = CreateUDPSocket()

BindSocket(socket , 60000)

Print "Server started"

While True
	PollSystem
	Local result:Int = SocketReadAvail(socket)
	If result
		Print "RECEIVED DATA. len=" + result

		Local buf:Byte Ptr = MemAlloc(result)
		Local size:Int
		'fill buffer
		While result > 0 
			Local recvlen:Int = socket.recv(buf+size , result)
			result:- recvlen
			size:+ recvlen
		Wend

		'Create Ram stream
		Local stream:TRamStream = CreateRamStream(buf , size , True , False)
		Print "--stream size="+StreamSize(stream)+"--"
		'While Not stream.Eof()
			Print stream.ReadLine()
			Print stream.ReadInt()
			Print stream.ReadByte()
			Print stream.ReadFloat()
			Print stream.ReadDouble()
			Print stream.ReadLine()
			Print "--end--~n"
		'Wend
		stream.close
		
		MemFree(buf)				

	EndIf
	Delay 5
Wend


client:
'client
SuperStrict

Global socket:tsocket = CreateUDPSocket()

Print ConnectSocket(socket , HostIp("127.0.0.1") , 60000)

Print "Client ready."

Local stream:tsocketstream = CreateSocketStream(socket)

stream.WriteLine("Hello!")
stream.WriteInt(12345)
stream.WriteByte(99)
stream.WriteFloat(3.1415)
stream.WriteDouble(Pi)
stream.WriteLine("World!")

stream.close

CloseSocket(socket)


or use raknet...

Last edited 2011


YellBellzDotCom(Posted 2011) [#3]
Thanks for the help. There is quite a lot of extra stuff required to get the sockets working. More than I figured on. I really appreciate your help.

I have looked into Raknet and got something going, I will probably revisit that one since getting a socket based system up and running will be quite a task. I would rather spend that time getting a raknet system going.

As far as I have seen there are 3 raknet mods available.
Bruceys
Jimons from RepeatUntil
and yours

as far as I can remember yours had the best example so I will check that out again.

Thanks for the help!