/* Created by Anjuta version 0.1.9 */
/*	This file will not be overwritten */

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define LISTENQ 20 //queue at most 20 connections

int main()
{
	int listenfd;
	struct sockaddr_in sock;
	
	listenfd = socket(AF_INET, SOCK_STREAM, 0); //get a socket file descriptor
	
	bzero(&sock, sizeof(sock)); //clear the structure
	
	sock.sin_family = AF_INET;
	sock.sin_port = htons(4012);
	sock.sin_addr.s_addr = htonl(INADDR_ANY);
	
	bind(listenfd, (struct sockaddr *) &sock, sizeof(sock));
	
	listen(listenfd, LISTENQ);
	
	for( ; ; )
	{
		int connectionfd;
		pid_t pid;
		
		connectionfd = accept(listenfd, NULL, NULL);
		
		if( (pid = fork() ) == 0 ) //child
		{
			close(listenfd); // close listening socket in the child
			printf("PID: %d", pid);
			printf(",  Get lost!\n");
			
			send(connectionfd, "Get lost", strlen("Get lost"), 0);
			exit(0);
		}
		
		printf("PID: %d", pid);
		printf(",  Get lost!\n");
			
		send(connectionfd, "Get lost", strlen("Get lost"), 0);

		//close connection socket in the parent:
		close(connectionfd);
	}
	
	close(listenfd);
	
	exit(0);
}