In the following program, we will demonstrate, how data can be transferred between client and server using Transport Layer Protocol - UDP. This code is self-explanatory. Hence, first, you will learn to develop server-side applications. Then, you will learn to develop client-side applications.
We know that UDP is a connection-less protocol, hence at the time of testing you may not get a response from the server many times.
Source code of Echo Server
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void) {
int received_data_length;
int sent_data_length;
int client_address_length;
int server_port = 5000;
short sock_file_descriptor;
char server_ip[] = "127.0.0.1";
char buffer[256];
char *client;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
fflush(stdin);
client = (char *) malloc(sizeof(server_address));
sock_file_descriptor = socket(PF_INET, SOCK_DGRAM, 0);
if (sock_file_descriptor < 0) {
printf("failed");
exit(1);
} else {
printf("socket created");
}
bzero(&server_address, sizeof(server_address));
server_address.sin_family = PF_INET;
inet_aton(server_ip, &server_address.sin_addr);
server_address.sin_port = htons(server_port);
bind(sock_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address));
received_data_length = recvfrom(sock_file_descriptor, buffer, strlen(buffer), 0, (struct sockaddr *) &client_address, &client_address_length);
fflush(stdout);
if (received_data_length != -1)
printf("\n* receive success *\n");
else
printf("\n* receive failure *\n");
sent_data_length = sendto(sock_file_descriptor, buffer, received_data_length, 0, (struct sockaddr *) &client_address, sizeof(client_address));
if (sent_data_length != -1)
printf("\n* send success *\n");
else
printf("\n* send failure *\n");
close(sock_file_descriptor);
return 0;
}
Source code of Echo Client
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(void) {
int sock_file_descriptor;
int sent_data_length;
int received_data_length;
unsigned int server_address_len;
unsigned short server_port = 5000;
char server_ip[] = "127.0.0.1";
char text[] = "Hello";
char buffer[256];
struct sockaddr_in server_address;
server_address_len = sizeof(server_address);
bzero(&server_address, sizeof(server_address));
server_address.sin_family = PF_INET;
server_address.sin_port = htons(server_port);
inet_aton(server_ip, &server_address.sin_addr);
sock_file_descriptor = socket(PF_INET, SOCK_DGRAM, 0);
/* send text to server */
sent_data_length = sendto(sock_file_descriptor, text, strlen(text), 0, (struct sockaddr *) &server_address, sizeof(server_address));
if (sent_data_length != -1)
printf("\n* message sent successfully *\n");
else
printf("\n* message sent failure *\n");
/* receive data from server */
received_data_length = recvfrom(sock_file_descriptor, buffer, strlen(buffer), 0, (struct sockaddr *) &server_address, &server_address_len);
if (received_data_length != -1) {
buffer[received_data_length] = '\0';
printf("\n* %s *\n", buffer);
}
close(sock_file_descriptor);
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.