UDP Socket sender IP and Port

BlitzMax Forums/BlitzMax Programming/UDP Socket sender IP and Port

Rck(Posted 2006) [#1]
Could someone explain to me how to use UDP Sockets in bmax to send and receive messages. Of interest is how to determine a sender's IP and Port when receiving a msg.

This was possible in BlitzPlus and had become familiar to me, but I can't currently find out how in bmax.


Vertex(Posted 2006) [#2]
I think, there is a problem with it, becouse Mark use recv and not recvfrom function to receive a udp message.

The method UpdateRemoteName maybe can get the MessageIP/Port by using after RemoteIp and RemotePort.

I love my BNetEx Module :P http://vertex.dreamfall.at/bnet/bnetex162.zip I you intersted in it. Its good structured I think. You can look at first to the examples.

cu olli


Rck(Posted 2006) [#3]
Figured out how.
I had heard about recvfrom, eventually got it to work.

When just reading ints from a server socket, I do:

Local sra:Int = SocketReadAvail(servSocket)
If sra > 0 Then
	Local bytes:Int[1]
	Local rip:Int = 0
	Local rport:Int = 0
	recvfrom_(servSocket._socket, bytes, sra, 0, rip, rport)
	Print "from: " + DottedIP(rip) + ": " + rport
	Print "got int: " + bytes[0]
EndIf



Hopefully this becomes a somewhat straight-forward forum post on the subject and allows the many I've seen wondering how to eventualy know.


Vertex(Posted 2006) [#4]
You can make your own Type like:
Type TSocketStreamEx Extends TSocketStream
	Field RecvTimeout : Int
	Field SendTimeout : Int
	Field MessageIP   : Int
	Field MessagePort : Short

	Method SetTimeouts(Recv:Int, Send:Int)
		Self.RecvTimeout = Recv
		Self.SendTimeout = Send
	End Method

	Method Read:Int(Buffer:Byte Ptr, Count:Int)
		Local Read:Int, Size:Int, Result:Int, TempPort:Int

		If Self._socket._socket = -1 Then Return 0
		Read = Self._socket._socket
		If select_(1, Varptr(Read), 0, Null, 0, Null, Self.RecvTimeout) <> 1 Then Return 0
		If ioctl_(Self._socket._socket, FIONREAD, Varptr(Size)) = SOCKET_ERROR_ Then return 0
		if Size <= 0 Then Return 0
		
		Result = recvfrom_(Self._socket._socket, Buffer, Count, 0, Self.MessageIP, TempPort)
		If result = SOCKET_ERROR Or Result = 0 Then Return 0
		Self.MessagePort = Short(TempPort)

		Return Result
	End Method

	Method Write:Int(Buffer:Byte Ptr, Count:Int)
		Local Write:Int, Size:Int, Result:Int

		If Self._socket._socket = -1 Then Return 0
		Write = Self._socket._socket
		If select_(0, Null, 1, Varptr(Write), 0, Null, Self.SendTimeout) <> 1 Then Return 0
		
		Result = sendto_(Self._socket._socket, Buffer, Count, 0, Self._socket._remoteIp, Self._socket._remotePort)
		If result = SOCKET_ERROR Or Result = 0 Then Return 0

		Return Result
	End Method

	Method MessageIP:Int()
		Return Self.MessageIP
	End Method

	Method MessagePort:Short()
		Return Self.MessagePort
	End Method

	Function Create:TSocketStreamEx( socket:TSocket,autoClose=True )
		Local t:TSocketStreamEx=New TSocketStreamEx
		t._socket=socket
		t._autoClose=autoClose
		Return t
	End Function
End Type

Function CreateSocketStreamEx:TSocketStreamEx(socket:TSocket, autoClose=True)
	Return TSocketStreamEx.Create(socket, autoClose)
End Function

Function SocketStreamSocket:TSocket(stream:TSocketStreamEx)
	Return stream.Socket()
End Function

(I haven't test it)

Firstly you create a UDP Socket than you use CreateSocketStreamEx and set the timeouts with SetTimeouts. Than you can use ReadLine, WriteLine and so on. I you want to know the sender you can use MessageIP and MessagePort methods.

(Oh oh oh, Mark could use superstrictmode in his own modules )
cu olli


Rck(Posted 2006) [#5]
Thank you for the code outline.


Blitzplotter(Posted 2006) [#6]
Very informative post, well put together.