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

/* Port fuer die Requests */
#define PORT 7777

int main(void)
  {
  struct sockaddr_in my_addr;
  struct sockaddr_in remote_addr;
  int size;
  int s;
  int remote_s;

  /* Die Socket erzeugen */
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0)
    {
      fprintf(stderr, "Error: Socket\n");
      return -1;
    }

  memset(&my_addr, 0, sizeof (my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(PORT); 
  my_addr.sin_addr.s_addr = INADDR_ANY; /* beliebige Anfragen */

  if (bind(s, (struct sockaddr *)&my_addr, sizeof(my_addr))==-1)
    {
    fprintf(stderr, "Error: bind\n");
    return -1;
    }

  /* Warteschlange einrichten */
  if (listen(s, 1) == -1)
    {
    fprintf(stderr, "Error: listen\n");
    return -1;
    }

  size = sizeof(remote_addr);
  /* Auf eine eingehende Verbindung warten */
  remote_s = accept(s, (struct sockaddr *)&remote_addr, (socklen_t*)&size);
  if (remote_s < 0)
    {
    fprintf(stderr, "Error: accept\n");
    return -1;
    }
  /* Infos ausgeben */
  printf("\nConnect von: %s\n", inet_ntoa(remote_addr.sin_addr));
  printf("sende Daten...\n");
  size = send(remote_s, "Hello World",11,0);
  if (size == -1)
    {
    fprintf(stderr, "error while sending\n");
    } 
  else 
    {
    printf("%d Bytes sent\n", size);
    }
  printf("closing sockets\n");
  /* Sockets wieder freigeben */
  close(remote_s);
  close(s);
  printf("terminating\n");
  return 0;
  }
