//
//An inline C sockets lecture (part 1).
//Niall McMahon, 2022.
//
//Client, host and port: 127.0.0.1:xxxx
// - The port number is randomly assigned by the OS. xxxx represents a 4-digit number.
// - If we choose, we can specify a port
// - Host address: 127.0.0.1
// - Socket address: 127.0.0.1:xxxx
//
//Server, host and port: 127.0.0.1:1234
// - Server process host and port must be known.
// - Host address: 127.0.0.1
// - Socket address: 127.0.0.1:1234
//

//This is code for a client, i.e. a process looking to communicate with a central host (a server).
//The client and server can be processes running on the same machine.

//CLIENT PROGRAM

//Standard input and output, stdio.h.
#include <stdio.h>
//String function library, string.h.
#include <string.h>
//The standard library, stdlib.h, which defines basic variables and functions.
#include <stdlib.h>
//unistd.h, miscellaneous symbolic constants and types.
#include <unistd.h>
//arpa/inet.h, defines structures and types for internet operations including in_port_t and in_addr_t
//See The Single UNIX Specification, Version 2.
#include <arpa/inet.h>
#include <sys/socket.h>

//Expecting an integer return when main is complete.
int main(){

//Define the two sockets to be connected.

//CREATE CLIENT SOCKET - this code is running in the client process.
//create a new socket.
//Sockets are of type integer, corresponding to the file descriptor id.
//You define a socket like this:
// int socket(int af, int type, int protocol)
//Parameters for socket():
// 1. af(Address Family) AF_INET and AF_INET6
// - The AF_INET address family enables interprocess communication between processes on the same computer (in the transport layer) or processes running on different computers, across the internet.
// - For an AF_INET process that uses IP Version 4, the AF_INET family uses the sockaddr_in address structure. This is loaded from sys/socket.h.
// - See lecture notes about struct types.
// - See IBM's article about AF_INET.
// 2. type: SOCK_STREAM and SOCK_DGRAM
// - See lecture notes about streams and datagrams.
// 3. protocol: IPPROTO_TCP and IPPTOTO_UDP
// - Either TCP or UDP. Stream defaults to TCP and datagram defaults to UDP.
// - See lecture notes about TCP and UDP.
int sock = socket(AF_INET, SOCK_STREAM, 0);

//SOCKET #2 DESCRIPTION - information about the server process.
//Now we need to collect together information about the target socket in a sockaddr_in struct.
//Again, IBM's article about AF_INET.
//See lecture notes about struct types.
//See lecture notes about the sockaddr_in struct.
//Create a new struct of type sockaddr_in called serv_addr.
struct sockaddr_in serv_addr;
//Fill the memory allocated to the serv_addr struct with zeroes. Pass in the address of serv_addr as the first parameter.
memset(&serv_addr, 0, sizeof(serv_addr));
//This socket uses the AF_INET address family - see note above.
serv_addr.sin_family = AF_INET;
//Set the IP address. Remember that sin_addr is of type union. s_addr specifies that the IP address will be stored as a 4 byte integer.
//See lecture notes about the sockaddr_in struct.
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Set the port number used by the server process.
//See lecture notes about htons.
serv_addr.sin_port = htons(1234);

//Now (attempt) to make a connection from the client socket (sock) with the server, defined by serv_addr.
//Use the connect() call.
// - See connect() — Connect a socket.
// - See lecture notes about connect()
// - The second parameter is a pointer to a sockaddr struct. The () change the type of &serv_addr to this pointer type.
//As a note sockaddr is similar to sockaddr_in - sockaddr_in is for IPV4. struct sockaddr* is equivalent to struct sockaddr_in*
connect(sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr));

//Create an array consisting of 40 chars to act as a data buffer.
char buffer[40];

//Once the connection is established, try to read from the second process and place the contents into the buffer.
//Use the read() call:
// - See lecture notes about read()
// - The last character in buffer is reserved for the termination char, '\0', so the size available is one less than the sizeof(buffer).
read(sock, buffer, sizeof(buffer)-1);

//In the default byte-stream mode, read() retrieves data from the STREAM until as many bytes as were requested are transferred, or until there is no more data to be retrieved.
//Assuming that our message from the server fit into the buffer, this can be displayed using:
printf("Message from the server: %s\n", buffer);

//Close the client socket, terminating the connection with the server.
close(sock);

return 0;
}