#include <sys/socket.h>

#include <sys/types.h>

#include <stdio.h>

#include <arpa/inet.h>

int main() {

/*declaring an integer variable client_socket_fd*/

/*calling the socket function to initiate a tcp socket

and mapping the returned file descriptor value

with the client_socket_fd variable*/

int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);

if (client_socket_fd != -1) {

printf("socket creation is successful, and the file descriptor value is %d\n\n",

client_socket_fd);

}

int length;

/*declaring two strctures client_address and server_address

with the type sockaddr_in*/

struct sockaddr_in server_address, client_address;

/*client_address.sin_addr.s_addr = INADDR_ANY; /*special keyword to bind any available ip

addresses in this host*/

getsockname(client_socket_fd, (struct sockaddr*)&client_address, &length);

//char client_ip_address = inet_ntoa(client_address.sin_addr);

//int client_port = ntohs(client_address.sin_port);

printf("The implicit IP that will be assigned to the client for the connection with server is: %s\n",

inet_ntoa(client_address.sin_addr));

char choice;

int explicit_port;

printf("should we explicitly specify the client port number(y/n): ");

scanf("%s", &choice);

if (choice == 'y') {

printf("enter the explicit client port number: ");

scanf("%d", &explicit_port);

client_address.sin_port = htons(explicit_port);

getsockname(client_socket_fd, (struct sockaddr*)&client_address, &length);

printf("The explicit Port that will be assigned to the client for the connection with server is:

%d\n", ntohs(client_address.sin_port));

}

else if (choice == 'n') {

printf("The implicit Port that will be assigned to the client for the connection with server is:

%d\n", ntohs(client_address.sin_port));

}

printf("\n");

server_address.sin_family = AF_INET;

/*the explicit remote host address refers to google.com's public IP*/

server_address.sin_addr.s_addr = inet_addr("172.217.163.206");

/*the explicit remote host port is 80 which is http*/

/*to convet 80 into network byte order format, htons means host to network short*/

server_address.sin_port = htons(80);

getsockname(client_socket_fd, (struct sockaddr*)&client_address, &length);

printf("The explicit Port that will be assigned to the client for the connection with server is:

%d\n", ntohs(client_address.sin_port));

}

else if (choice == 'n') {

printf("The implicit Port that will be assigned to the client for the connection with server is:

%d\n", ntohs(client_address.sin_port));

}

printf("\n");

server_address.sin_family = AF_INET;

/*the explicit remote host address refers to google.com's public IP*/

server_address.sin_addr.s_addr = inet_addr("172.217.163.206");

/*the explicit remote host port is 80 which is http*/

/*to convet 80 into network byte order format, htons means host to network short*/

server_address.sin_port = htons(80);

printf("server details provided in the code are: IP 172.217.163.206 , Port 80\n");

int c = connect(client_socket_fd, (struct sockaddr*)&server_address, sizeof(server_address));

if (c == 0){

printf("connection to the server is successful\n");

}

}

--end-of-post--