// simple_server.cpp : simple server, listening for messages 
//	by Kristof Van Laerhoven (kristof@comp.lancs.ac.uk)
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <conio.h>				// for kbhit

int main(int argc, char* argv[])
{
	WORD wVersionRequested = MAKEWORD(1,1);
	WSADATA wsaData;
	int nRet;
	short nPort;
	char ch;
	FILE* logfile;

	nPort = 6767;
	
	printf("Simple Server v 1.01 - \nwritten by Kristof Van Laerhoven (kristof@comp.lancs.ac.uk)\n\n");

	if (argc<2) {
		printf("enter the port number: ");
		scanf("%i",&nPort);
	}
	else
		nPort = atoi(argv[1]);

	// Initialize WinSock and check version
	nRet = WSAStartup(wVersionRequested, &wsaData);
	if (wsaData.wVersion != wVersionRequested) {	
		printf("\n Wrong winsock version\n");
	}

				// Create a UDP/IP datagram socket
				SOCKET theSocket;	
				theSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
				if (theSocket == INVALID_SOCKET) {
					printf("\n Invalid Socket\n");
				}

				// Fill in the address structure
				SOCKADDR_IN saServer;
				saServer.sin_family = AF_INET;
				saServer.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
				saServer.sin_port = htons(nPort);	   // Use port passed from user

				// bind the name to the socket
				nRet = bind(theSocket,(LPSOCKADDR)&saServer,sizeof(struct sockaddr)	);
				if (nRet == SOCKET_ERROR) {
					printf("\n Socket bind error\n");
					closesocket(theSocket);
				}

				int nLen;
				nLen = sizeof(SOCKADDR);
				char szBuf[1024];
				char outBuf[10];

				nRet = gethostname(szBuf, sizeof(szBuf));
				if (nRet == SOCKET_ERROR) {
					printf("\n Socket hostname error\n");
					closesocket(theSocket);
				}

				// Show the server name and port number
				printf("Server named %s waiting on port %d\n", szBuf, nPort);

				SOCKADDR_IN saClient;
				int c, i, j=1;
				long totbyte=0;
				
				// Receive data from the client
				j=1;
				ch = 0;
				while (j>=0 && ch!='q')	{
					memset(szBuf, 0, sizeof(szBuf));	// clear buffer
					
					nRet = recvfrom(theSocket,			// Bound socket
								szBuf,					// Receive buffer
								sizeof(szBuf),			// Size of buffer in bytes
								0,						// Flags
								(LPSOCKADDR)&saClient,	// Buffer to receive client address 
								&nLen);					// Length of client address buffer

					printf("Received from %i.%i.%i.%i: %s\n",	saClient.sin_addr.S_un.S_un_b.s_b1, 
														saClient.sin_addr.S_un.S_un_b.s_b2, 
														saClient.sin_addr.S_un.S_un_b.s_b3, 
														saClient.sin_addr.S_un.S_un_b.s_b4, 					
														szBuf);
					logfile = fopen("logfile.txt","a");
					fprintf(logfile, "%i.%i.%i.%i %s\n",	saClient.sin_addr.S_un.S_un_b.s_b1, 
														saClient.sin_addr.S_un.S_un_b.s_b2, 
														saClient.sin_addr.S_un.S_un_b.s_b3, 
														saClient.sin_addr.S_un.S_un_b.s_b4, 					
														szBuf);
					fclose(logfile);
					j=nRet;
					if (nRet == INVALID_SOCKET)	{
						printf("\nTotal bytes received: %.f\n", (float)totbyte);
						printf("\n Receive error\n");
						closesocket(theSocket);
					}
					i=0;
					
					if (_kbhit())
						ch = getch();
				}
				
				closesocket(theSocket);

	printf("Server halting..\n");
	// Release WinSock
	WSACleanup();

	return 0;
}

