/*
Program: minser.c
Author: Dr Beco, 2011-04-03
Objective:
    show a minimum server program that can
    create a socket, accept a client, read a byte, write a byte, disconnect
*/

#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define BUFFER 200

int main(void)
{
    int port = 8234;
    char data[BUFFER];
    struct sockaddr_in dir;
    struct sockaddr client;
    socklen_t long_client;
    int id, idReuse=1, son, aux;

    memset(&dir,0,sizeof(dir));
    dir.sin_port = port;
    dir.sin_family = AF_INET;
    //dir.sin_addr.s_addr = INADDR_ANY;
    dir.sin_addr.s_addr = inet_addr("192.168.0.201");

    id = socket(AF_INET, SOCK_STREAM, 0);
    if (id == -1) {
        return -1;
	}
    
    if(setsockopt(id,SOL_SOCKET,SO_REUSEADDR,&idReuse,sizeof(idReuse)) == -1) {
       return -1;
	}
    
    if(bind(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
    {
        close (id);
        return -1;
    }
    
    if (listen(id , 1) == -1)
    {
		printf("Failed to listen on the socket\n");
        close(id);
        return -1;
    }
    
    long_client = sizeof (client);
    son = accept(id, &client, &long_client);
    aux = read(son, data , 1);
	//aux = send(son, "S", 1, MSG_NOSIGNAL);
    aux = send(son, "S", 1, SO_NOSIGPIPE); // for macOS
    printf("done!\n");

    return 0;
}
