The goal was to create a VFP application for receiving UDP packet from a client and responding to the same client using the IP address and the port obtained from the initial packet. The sockaddr structure that the recvfrom call populates contains required information.
The UdpBase class uses several Winsock API functions. It implements the basic functionality such as creates a UDP socket and binds it to a local port. After that the class can use this socket for sending and receiving datagrams.
The WaitForRequest method of the class indefinitely waits for incoming datagrams. Upon receiving a datagram it sends a message back to the client.
DEFINE CLASS UdpServer As UdpBase
PROCEDURE WaitForRequest(nPort)
IF NOT THIS.StartServer(nPort)
RETURN .F.
ELSE
? "Waiting for incoming requests on " +;
THIS.LocalIP + ":" +;
TRANSFORM(THIS.LocalPort) + "..."
ENDIF
* waits indefinitely for a request from a client
* on local ip address and port specified in nPort
THIS.ReceiveDatagram
IF EMPTY(THIS.SenderIP) OR EMPTY(THIS.SenderPort)
* failed to get a response or
* when defining the source of the response
RETURN .F.
ELSE
* displays request received
? "Request received from " + THIS.SenderIP +;
":" + TRANSFORM(THIS.SenderPort)
? THIS.DataReceived
ENDIF
* assemble a data to be sent back to client
LOCAL cConfirmation
cConfirmation = "Responder: " + SYS(0) +;
" " + TTOC(DATETIME())
* sends data back to client
* the client is supposed to be in listening mode
THIS.SendDatagram(m.cConfirmation,;
THIS.SenderIP, THIS.SenderPort)
The User Datagram Protocol (UDP) is one of the core Internet protocols. By using the UDP an application can send short messages, also known as datagrams, across the network.
A datagram is a self-contained connectionless packet, one which contains enough information in the header to allow the network to forward it to the destination independently of previous or future datagrams.
Credits: PHP (4.4.9), an HTML-embedded scripting language,
MySQL (5.1.55-log), the Open Source standard SQL database,
AceHTML Freeware
Version 4, freeware HTML Editor of choice. Hosted by Korax Online Inc.