#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    // Creating the socket (client)
    int ClientSocket;
    ClientSocket = socket(AF_INET, SOCK_STREAM, 0);// TCP socket

    if(ClientSocket != -1)// verifying that the socket is created succesfully
    {
        printf("Socket created !\n");
    }else
    {
        perror("Error creating socket");
    }

    // connection to server
    struct sockaddr_in serverInfos;// the structure that store the server's infos (the server you want to connect to) the structure type is "sockaddr_in" (google it if you want to know more about it)
    serverInfos.sin_family = AF_INET;// TCP method
    serverInfos.sin_port = htons(8888);// PORT converted to a network format
    serverInfos.sin_addr.s_addr = inet_addr("192.168.0.13");// IP converted to a network format

    if(connect(ClientSocket, (struct sockaddr*)&serverInfos, sizeof(serverInfos)) != -1)// Connecting to the server
    {
        printf("Connected to server !\n");
    }else
    {
        perror("Error connecting to server");
        return -1;
    }

    while(1)
    {
        // variables to store the data that you send/receive to/from the server
        char commandIn[999];
        char commandOut[999];

        scanf("%998s", commandOut);// i use scanf but you can replace it by a more secured one

        if(send(ClientSocket, commandOut, 998, 0) != -1)// send the data
        {
            printf("command sent !\n");// return if it worked
        }
        if(recv(ClientSocket, commandIn, 998, MSG_PEEK) != -1)// receive what the server sent you back (if it did), if you get an infinite loop printing the server's reply, replace "MSG_PEEK" by "0"
        {
            printf("Here is return from server: %s\n", commandIn);// printing the server's message
            return 0;
        }
    }



    return 0;
}