I want to share a client-server project. In this example the server can accept more than one client (Daemon server implementation). This example was created and tested using MQX4.2, CodeWarrior 10.6 and FRDM-K64F boards.
In general, the client-server model is the most common communication model used by the applications such as HTTP, Telnet, FTP, SSH, and others. In a client-server model, the server listens to client requests and waits for new connections. When a client needs to connect to a server, it sends a request. The server acknowledges the request, and if the client is supported by the server, the connection is established.
A TCP client-server communication can be implemented as in the following figure.
SETTING THE RTCS
Below are presented the steps required to set the RTCS with the default settings:
Create the RTCS. This step reserves memory for different RTCS parameters and initializes the TCP/IP task.
Each project (server and client) contains an RTCS.c file which contains the initialize networking function with the RTCS configuration.
Set the RTCS IP address to the device after initialization.
The main.h files have the macros and variables that store the IP parameters. You can change the IP address according your needs.
In RTCS.c files, the following code sets the address, mask, gateway and server IP addresses.
ip_addr = IPADDR(A,B,C,D);
phy_addr = BSP_DEFAULT_ENET_DEVICE;
ip_data.ip = ip_addr;
ip_data.mask = ENET_IPMASK;
ip_data.gateway = 0;
Calculate the MAC address for the device using the variables.
Use the ENET_get_mac_address() function to build a valid MAC address for the application. MAC address calculation takes the device number and IP address as parameters, and returns a valid MAC address in the IPCFG_default_enet_address variable.
Initialize the Ethernet device available in the board with the calculated MAC address.
Use the ipcfg_init_device() function to tell the RTCS task what Ethernet device should be used and to set the calculated MAC address to that device. Once the MAC address is set and the device is initialized, it is possible to bind the device to the IP address.
For the server implementation, MQX uses a socket structure that is created and filled in this way:
laddr.sin_family = AF_INET;
laddr.sin_port = CONNECT_PORT;
laddr.sin_addr.s_addr = INADDR_ANY;
The socket() function creates an unbound socket in a communications domain. This function also returns a handler used in later function calls that operate on sockets.
/* Listen on TCP port */
listensock = socket(PF_INET, SOCK_STREAM, 0);
if (listensock == RTCS_SOCKET_ERROR)
error = RTCSERR_OUT_OF_SOCKETS;
The bind() function assigns a local socket address to a socket and the listen() function marks a socket as accepting connections.
error = bind(listensock, &laddr, sizeof(laddr));
error = listen(listensock, 0);
The accept() function extracts the first connection on the queue of pending connections, creates a new socket with the same socket type protocol and address family as the specified socket, and allocates a new file descriptor for that socket.
child_sock = accept(listensock, NULL, NULL);
The recv() function receives a message from a socket.