// TCPServer.cpp 
// A simple example for TCP server

#include <stdio.h>
#include <Winsock2.h>

#define PORTNUM 50000 /* random port number, we need something */


/* this is the function that plays with the socket.  it will be called
 * after getting a connection.
 */
void do_something(SOCKET s)
{
	char inStr[256];
	int ret;
	printf("On new connection\n");
	
	ret = recv(s, inStr, sizeof(inStr), 0);

	printf("Server Received: %s\n", inStr);
	char str[] = "hello world received :)";
	ret = send( s, str, sizeof(str), 0);  
}


/* code to establish a socket
 */
SOCKET establish(unsigned short portnum)
{ 
	char   myname[256];
	SOCKET s;
	struct sockaddr_in sa;
	struct hostent *hp;

	memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
	gethostname(myname, sizeof(myname));        /* who are we? */
	hp = gethostbyname("localhost");                 /* get our address info */
	if (hp == NULL)                             /* we don't exist !? */
		return(INVALID_SOCKET);
	sa.sin_family = hp->h_addrtype;             /* this is our host address */
	sa.sin_port = htons(portnum);               /* this is our port number */
	s = socket(AF_INET, SOCK_STREAM, 0);        /* create the socket */
	if (s == INVALID_SOCKET)
		return INVALID_SOCKET;

	/* bind the socket to the internet address */
	if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) ==
		SOCKET_ERROR) 
	{
		closesocket(s);
		return(INVALID_SOCKET);
	}
	listen(s, 3);                               /* max # of queued connects */
	return(s);
}


int main(int argc, char* argv[])
{ 
	SOCKET s;
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
 
	wVersionRequested = MAKEWORD( 2, 2 );
 
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
	  /* Tell the user that we could not find a usable */
	   /* WinSock DLL.                                  */
	   return 1;
	}
 
	char   myname[256];
	struct sockaddr_in sa;
	struct hostent *hp;

	memset(&sa, 0, sizeof(struct sockaddr_in)); /* clear our address */
	gethostname(myname, sizeof(myname));        /* who are we? */
	hp = gethostbyname("localhost");                 /* get our address info */
	if (hp == NULL)                             /* we don't exist !? */
		return(INVALID_SOCKET);
	sa.sin_family = hp->h_addrtype;             /* this is our host address */
	sa.sin_port = htons(PORTNUM);               /* this is our port number */
	s = socket(AF_INET, SOCK_DGRAM, 0);        /* create the socket */
	if (s == INVALID_SOCKET)
		return INVALID_SOCKET;

	/* bind the socket to the internet address */
	if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) ==
      SOCKET_ERROR) 
	{
		closesocket(s);
		return(INVALID_SOCKET);
	}

	if ((s = establish(PORTNUM)) == INVALID_SOCKET) { /* plug in the phone */
		perror("establish");
	    exit(1);	
	}

	SOCKET new_sock;
	for (;;) 
	{                            /* loop for phone calls */
		new_sock = accept(s, NULL, NULL);
		if (s == INVALID_SOCKET) 
		{
			fprintf(stderr, "Error waiting for new connection!\n");
			exit(1);
		}
		do_something(new_sock);
	}
    closesocket(new_sock);
	return 1;
}



