udp client
// CS20B1024 Sankethraj Kotagond
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
int main()
{
int c_sock, otp[4], verify_otp[4];
char phone_num[256], check[1];
printf("Enter phone number: ");
scanf("%s", phone_num);
c_sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in client, other;
client.sin_family = AF_INET;
client.sin_port = htons(9001);
client.sin_addr.s_addr = INADDR_ANY;
socklen_t ad;
ad = sizeof(other);
sendto(c_sock, phone_num, sizeof(phone_num), 0, (struct sockaddr *)&client, sizeof(client));
printf("client send: %s", phone_num);
recvfrom(c_sock, otp, sizeof(otp), 0, (struct sockaddr *)&other, &ad);
printf("\n\nOTP received:\n");
for (int i = 0; i < 4; i++)
{
printf("%d", otp[i]);
}
printf("\n");
printf("Enter OTP: ");
for (int i = 0; i < 4; i++)
{
scanf("%d", &verify_otp[i]);
}
printf("\nOTP sent for verification");
sendto(c_sock, verify_otp, sizeof(verify_otp), 0, (struct sockaddr *)&client, sizeof(client));
recvfrom(c_sock, check, sizeof(check), 0, (struct sockaddr *)&other, &ad);
if (check[0] == '1')
{
printf("\n\nOTP Verified successfully!");
}
else
{
printf("\n\nOTP is wrong!");
}
close(c_sock);
return 0;
}