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

int main(void)
{
struct sockaddr_in serv_addr;
char buffer[128];
int sock,sc,len;

   /* Fist you need to set up the questions
    * who ? where ? when ? 
    * The thing we setup here is the local stuff like port and protocol + 
    * adres. In this case we use INADDR_ANY witch the compiler will replace 
    * with 0.0.0.0.
    * port = 1337
    */
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(1337);

   
  /* Like on a walky talky we need a channel to talk to that is what we do 
   * here. socket() returns the channel. */ 
  sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
  
  /* if no socket has been returned print an error */
   if (!sock) 
   { fprintf (stderr,"socket() error\n");
     return -1;
   }
   
   /* We must bind to the port we have set above 
    *  serv_addr.sin_port = htons(1337);
    * on failure the error will be displayed */                  
   if(bind(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
    perror("error_bind");
    return -1;
   }
   
     
  /* start listening */ 
  listen (sock , 1) ;
  
  /* when a connection has been accepted it returns a 2th channel to talk 
   * to and read from */
  sc = accept(sock,0,0);
 
   /* Keep on reading from the channel until there is no more data to read */
   while (read (sc,buffer,sizeof(buffer)-1)  !=0)
   {
   printf("%s",buffer);
   len = strlen(buffer);
   write (sc,buffer,len);
   }

}

