How to do it…

  1. Execute the first three steps from the previous part of this recipe. Assign the localhost IP address, 127.0.0.1, as the address to the socket.
  2. Enter the message to be sent to the server. Invoke the sendto function to send the message to the server.
  3. Invoke the recvfrom function to get the message from the server. The message that's received from the server is then displayed on the screen.
  4. Close the descriptor of the socket.

The client program, udpc.c, to send a message to the server and to receive the reply using a UDP socket is as follows:

#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
char msgReceived[255];
char msgforserver[255];
int UDPSocket, n;
struct sockaddr_in client_Address;
printf("Enter the message to send to the server: ");
gets(msgforserver);
bzero(&client_Address, sizeof(client_Address));
client_Address.sin_addr.s_addr = inet_addr("127.0.0.1");
client_Address.sin_port = htons(2000);
client_Address.sin_family = AF_INET;
if ( (UDPSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("Socket could not be created");
exit(1);
}
if(connect(UDPSocket, (struct sockaddr *)&client_Address,
sizeof(client_Address)) < 0)
{
printf("\n Error : Connect Failed \n");
exit(0);
}
sendto(UDPSocket, msgforserver, 255, 0, (struct sockaddr*)NULL,
sizeof(client_Address));
printf("Message to the server sent. \n");
recvfrom(UDPSocket, msgReceived, sizeof(msgReceived), 0, (struct
sockaddr*)NULL, NULL);
printf("Received from the server: ");
puts(msgReceived);
close(UDPSocket);
}

Now, let's go behind the scenes.