/*  Server Example 1.0.0
This is a very simple example of a TCP server.  The program accepts only one 
argument which can only be a one worded message.
*/
#include <stdio.h>

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

int main(int argc, char **argv)  //main function with parameter acceptence
{
    int socket_desc;  //make socket descriptor
    struct sockaddr_in address;
    int addrlen;
    int new_socket;
    char *message=argv[1];  //place argument into a string
    
    socket_desc=socket(AF_INET,SOCK_STREAM,0);  //create a socket
    
    address.sin_family=AF_INET;  //use TCP
    address.sin_addr.s_addr=INADDR_ANY;
    address.sin_port=htons(1000);  //use port 1000 for server daemon
    
    bind(socket_desc,(struct sockaddr *)&address,sizeof(address));  //bind it
    
    listen(socket_desc,3);  //listen for pending connections
    
    addrlen=sizeof(address);
    
    new_socket=accept(socket_desc,(struct sockaddr *)&address,&addrlen);
    
    printf("New socket at %d\n",new_socket);  
    
    send(new_socket,message,strlen(message),0);  //send message
    
    puts("Message sent");
    
    sleep(3);  //wait for 3 seconds
    
    close(socket_desc);  //close socket
}