%recvfrom() function

The %recvfrom() function receives a datagram from a bound socket and stores the information on the sender. If no errors occur, the number of bytes received is returned.


code = %recvfrom(socket, buffer, bufferLength, flags, &sourceAddress, &port)


socket File descriptor for the local socket.
buffer Data to be received.
bufferLength Maximum length of the buffer.
flags Should be zero.
sourceAddress Address of the sender.
port Port number of the sender.


* Open a connection through UDP/IP.
*   recv packets from the network execute the command
*   and send result packets back
cfunction socket.builtin
include dm,bp,includes sysid.inc
include dm,bp,unix.h socket.h
equ ip_port to 3176
* Create a socket
fd=%socket( AF$INET, SOCK$DGRAM, 0)
if fd<0 then print "Error opening socket ":system(0); stop
* Bind the socket to an ethernet port
port=%bind( fd, AF$INET, INADDR$ANY, ip_port )
* Read buffers to get the command to execute
packcnt = 0
bigbuff = ""
   addr=0;  port=0
   buffer = space(256)
   length=%recvfrom( fd, buffer, 256, 0, &addr, &port)
   if length<0 then print "Read Error: ":system(0)
   PackSeq = buffer[5,4]
   MaxPack = buffer[9,4]
   packcnt += 1
   bigbuff[1+((PackSeq-1)*242),242] = buffer[13,242]
until packcnt = MaxPack do repeat
* execute the command
execute bigbuff capturing ret
* get the byteswapped remote address
remhost = ""
addr = dtx(addr)
lad = len(addr)
addr = xtd(addr[lad-7,8])
for x = 1 to 4
   part = rem(addr,256)
   addr = int(addr/256)
   remhost<-1>= part
next x
convert @AM to '.' in remhost
ln = len(ret)
MaxPack = int(ln/242)+1
for x = 1 to MaxPack
   Buffer = "RETC":x 'r%4':MaxPack 'r%4':ret[((x-1)*242)+1,242]:space(242)
   n = %sendto(fd, Buffer, 256, 0, &remhost, ip_port)
   if n < 0 then
      print "Write error: ":system(0)
next x
* Close the socket
%close( fd )