2. UDP sockets¶
Sockets are used to communicate between different processes on the same machine or on different ones. There are different types of sockets. In this chapter we will deal with Datagram sockets. Datagram sockets use UDP User Datagram Protocol.
CLient-Server architecture is usually used between 2 applications in order exchange information. Typically a client make a request for information to a server. A client have to know about the exitence and the address of the server. The server typically answer to some request, and doesn’t need to know about the existence the client prior to the connection being established.
For our purpose, in order to establish a communication we need a hostname
and a port
. A hostname could be a string or an ip address. A port is where a server listens for client’s calls. It is recommended, when using Robox products, to use port nember outside the range [8000, 8999] to avoid conflicts with Robox’s implementations.
2.1. R3: UDP Client/server¶
In RDE predefined examples you can find the demo R3: UDP client/server, it is a workspace with two RTE projects, one for server and one for client. In order to test this demo you need 2 Robox controllers.
This is a modified version
of the sample, it is configured to work with two RP1. The first with address 192.168.1.130
act as server, the second RP1 act as client and have the address 192.168.1.131
.
Each UDP datagram is charecterized by a length, the length of a datagram is send along with the data.
2.1.1. UDP server¶
In the server code we will use the following R3 functions:
str_to_ipaddr( ipstring, ipnum)
: convert an ip address string to it’s hexadecimal equivalent. e.g.192.168.1.130
will be0xC0A80182
udp_open_server(server_port, server_address)
: open a server with assigned port and address, and return a handle of the socket.udp_recv_from(sockethandle, buffer, buff_size, remote_port????, client_address)
udp_send_to (sockethandle, buff, buff_size, remote_port, client_address)
The buffer
to be send and received can be a STRING
, an array
or a struct
. If it is a struct
the first member should be an U32
that represents the message identifier, its value is fixed by the client which sent the request through the function udp_send_notify(). The remaining members are user defined, e.g. :
STRUCT_P buff_send
U32 msgId ; message identifier
I32 comando
I32 reg_start
I32 reg_num
END_STRUCT_P
The server in this example, have to send registers value to a client. It receive a command, udp_recv_from
, that represent the register type to be sent. Once the command received, if the the comand is know, the server build a buffer with the data of the requested registers and send them to the client, udp_send_to
.
First a command buffer is built:
STRUCT_P buff_send
U32 msgId
I32 comando ; command, register type
I32 reg_start ; starting index of the register
I32 reg_num ; how many registers (number of repetitions)
END_STRUCT_P
Codification of register types:
LIT REQ_R 1
LIT REQ_RR 2
LIT REQ_NVR 3
LIT REQ_NVRR 4
Then a buffer to be sent to the client is constructed:
STRUCT_P buff_recv
I32 msgId
I32 comando
I32 reg_start
I32 reg_num
REAL regs[30]
END_STRUCT_P
A server is opened using the function udp_open_server()
. Data are received udp_recv_from
, then depending on the command the buffer to be sent is filled with registers value, then sent to the client udp_send_to
.
The complete code can be found in the attached project.
2.1.2. UDP client¶
In the client code we will use the following R3 functions:
str_to_ipaddr( ipstring, ipnum)
: convert an ip address string to it’s hexadecimal equivalent. e.g.192.168.1.131
will be0xC0A80183
udp_open_client(server_port, server_address)
: open a client communication, and return a handle of the socket.UDP_SEND_NOTIFY(sockethandle, buff, buff_size)
: sends a UDP message with notificationUDP_RECV_NOTIFY(sockethandle, buff, buff_size, tmOutRx, tmOutTot)
: receives a UDP message (notified) from a previously selected station
The buffer
can be a STRING
, an array
or a struct
, see discussion in server section.
2.2. Appliaction Client¶
A client application will be developped that run on a personal computer. The UDP server code is that same we see before and will be running on Robox controller e.g. RP1 with address 192.168.1.130
.
We will develop a client similar to the one of RDE predefined example in UDP client.