In the first part of this recipe, we have already defined two strings by the names of msgReceived and msgforclient, both of which are of size 255. We have also defined two variables, server_Address and client_Address, of type sockaddr_in.
Now, you will be prompted to enter a message that is to be sent to the server. The message you enter will be assigned to the msgforserver string. Then, we will invoke the bzero function to initialize the client_Address structure, that is, zeros will be filled in for all the members of the client_Address structure.
Next, we will initialize the members of the client_Address structure to configure the socket. Using the sin_family member, the address family that's specified for the socket is AF_INET, which is used for IPv4 internet protocols. The port number that's specified for the socket is 2000. By using the htons function, the short integer, 2000, is converted into the network byte order before being applied as a port number. Then, we will assign the localhost IP address, 127.0.0.1, as the address to the socket. We will invoke the inet_addr function on the localhost address to convert the string containing the address in standard IPv4 dotted decimal notation into an integer value (suitable to be used as an internet address) before is it applied to the sin_addr member of the client_Address structure.
We will invoke the socket function to create a socket by the name of UDPSocket. The address family that's supplied for the socket is AF_INET, and the socket type that's selected is datagram.
Next, we will invoke the sendto function to send the message that's been assigned to the msgforserver string to the server. Similarly, we will invoke the recvfrom function to get the message from the server. The message that's received from the server is assigned to the msgReceived string, which is then displayed on the screen. Finally, the descriptor of the socket is closed.
Let's use GCC to compile the udps.c program, as follows:
$ gcc udps.c -o udps
If you get no errors or warnings, this means that the udps.c program has compiled into an executable file, udps.exe. Let's run this executable file:
$ ./udps
Waiting for the message from the client
Now, press Alt + F2 to open a second Terminal window. Here, let's use GCC again to compile the udpc.c program, as follows:
$ gcc udpc.c -o udpc
If you get no errors or warnings, this means that the udpc.c program has compiled into an executable file, udpc.exe. Let's run this executable file:
$ ./udpc
Enter the message to send to the server: Will it rain today?
Message to the server sent.
The output on the server will give us the following output:
Message received from the client: Will it rain today?
Enter the reply to be sent to the client: It might
Reply to the client sent
Once the reply is sent from the server, on the client window, you will get the following output:
Received from the server: It might
To run the recipes that demonstrate IPC using shared memory and message queue, we need to run Cygserver. If you are running these programs on Linux, then you can skip this section. Let's see how Cygserver is run.