ReservationServer.cpp
------------------------------------
Reservation Server Main Project File
------------------------------------*/
#include "stdafx.h"
#include "TcpServer.h"
int main(array<System::String ^> ^args)
{
Console::WriteLine("***** Restaurant Booking Server *****");
Console::WriteLine("Commenced server session "+DateTime::Now+"\n");
TcpServer^ server = gcnew TcpServer();
/*create a delegate to the method that is the starting point of the thread. Thread method is fully qualified in argument.
Note - this thread takes no arguments - hence use of ThreadStart and not ParameterizedThreadStart */
ThreadStart^ redisplayBookingsStart = gcnew ThreadStart(server, &TcpServer::redisplayBookingsEverySixtySeconds);
//create the thread:
Thread ^redisplayBookingsThread = gcnew Thread(redisplayBookingsStart);
//start the thread:
redisplayBookingsThread->Start();
//create a new listener for connections from TCP network clients (from any IP address):
TcpListener^ socket = gcnew TcpListener(IPAddress::Any, 12345);
//starts socket listening for incoming connection requests:
socket->Start();
while(true)
{
Console::WriteLine("Waiting for client connection...");
//Accept a pending connection request and assign it to a new TcpClient Class object, ready for sending and receiving:
TcpClient ^ client = socket->AcceptTcpClient();
//create a new parameterized thread delegate (this will be used for the connecting client):
Thread ^thr = gcnew Thread(gcnew ParameterizedThreadStart(server, &TcpServer::ProcessThread));
//start the new thread for the connecting TcpClient object by supplying the object as an argument to the thread (i.e. client):
thr->Start(client);
}
return 0;
}