OneCompiler

TCP CLIENT

143

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

int main()
{
int client_socket;

//creating a socket
client_socket = socket(AF_INET,SOCK_STREAM,0);

//defining address structure
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080);
server_address.sin_addr.s_addr = INADDR_ANY;

//connect
int connection_status = connect(client_socket,(struct addr*)&server_address,sizeof(server_address));

if(connection_status == -1)
{
	printf("Error occurred while making the connection\n");
}

char phone_num[1000];
printf("Enter phone number : ");
scanf("%s",phone_num);
send(client_socket,phone_num,sizeof(phone_num),0);
printf("Client sends : %s\n",phone_num);

int OTP[1];
//receiving OTP from server
recv(client_socket,OTP,sizeof(OTP),0);
printf("OTP received : %d\n",OTP[0]);
//closing the socket
close(client_socket);

return 0;
}