/* This file is listen.c.  It opens a blocking socket at port 5000 and
listens for messages in a for loop.  It takes the name of the machine
that it will be listening on as argument.
*/

/* file listen.c */

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

#define SERVER_PORT 5000

main(int argc, char *argv[]) {
  char message[1024];
  int sock;
  struct sockaddr_in name;
  struct hostent *hp, *gethostbyname();
  int bytes;

  printf("Listen activating.\n");

  /* Create socket from which to read */
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)   {
    perror("Opening datagram socket");
    exit(1);
  }
  
  /* Bind our local address so that the client can send to us */
  bzero((char *) &name, sizeof(name));
  name.sin_family = AF_INET;
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  name.sin_port = htons(SERVER_PORT);
  
  if (bind(sock, (struct sockaddr *) &name, sizeof(name))) {
    perror("binding datagram socket");
    exit(1);
  }
  
  printf("Socket has port number #%d\n", ntohs(name.sin_port));
  
  while ((bytes = read(sock, message, 1024)) > 0) {
    message[bytes] = '\0';
    printf("recv: %s\n", message);
  }

  close(sock);
}


