PRSHandler Handler;//Handles the messages recieved public void StartService(int P, int S, int E, int T) { servicePort = P; //Set the Service Port to listen on Handler = new PRSHandler(S, E, T); //Create the handler to deal with the ports Socket listeningSocket = new Socket(SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("Listening socket created"); // bind the socket to the server port listeningSocket.Bind(new IPEndPoint(IPAddress.Any, servicePort)); Console.WriteLine("Listening socket bound to port " + servicePort.ToString()); bool done = false; //For stop message while (!done) // a stop message has not been recieved { try { // receive a message from a client Console.WriteLine("Waiting for message from client..."); byte[] buffer = new byte[PRSMessage.SIZE]; EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); int result = listeningSocket.ReceiveFrom(buffer, ref remoteEP); Console.WriteLine("Received " + result.ToString() + " bytes: " + new string(ASCIIEncoding.UTF8.GetChars(buffer))); PRSMessage msg = PRSMessage.Deserialize(buffer); if (msg.msgType == PRSMessage.MsgType.STOP) { done = true; } PRSMessage response = Handler.ParseMessage(msg); PRSCommunicator.SendMessage(listeningSocket, remoteEP, response); } catch (Exception ex) { Console.WriteLine("Exception when receiving..." + ex.Message); } } // close the socket and quit Console.WriteLine("Closing down"); listeningSocket.Close(); Console.WriteLine("Closed!"); Console.ReadKey(); }
private static void TestCase2(Socket clientsocket)//Lookup port dead { //TestCase 2: //FTP server reserves a port IPEndPoint endPt = new IPEndPoint(IPAddress.Parse(PRSCommunicator.DEFAULT_IP), PRSCommunicator.DEFAULT_PORT); string FTPServer = "FTP"; ushort allocatedport = 0; ushort requestedport = 0; //Send request port PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeREQUEST_PORT(FTPServer)); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); PRSMessage statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); //PRSCommunicator.PrintMessage(statusMsg); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase2 failed on Request Port"); } allocatedport = statusMsg.port; Console.WriteLine("Allocated port of " + allocatedport.ToString()); //FTP Client asks for the port that the client is on PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeLOOKUP_PORT("FTP")); //FTP Client "attempts" to connect to server statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase2 failed on LookupPort"); } requestedport = statusMsg.port; Console.WriteLine("Service is on port: " + allocatedport.ToString()); //FTP Client fails to connect //FTP client sends port dead to PRS PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakePORT_DEAD(requestedport)); statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase2 failed on Deadport"); } }
private static void TestCase1(Socket clientsocket) { //TestCase 1: //FTP server requests port from prs //recieves port number //ftp server sends close port //recieves success IPEndPoint endPt = new IPEndPoint(IPAddress.Parse(PRSCommunicator.DEFAULT_IP), PRSCommunicator.DEFAULT_PORT); string FTPServer = "FTP"; ushort allocatedport = 0; //Send request port PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeREQUEST_PORT(FTPServer)); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); PRSMessage statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); //PRSCommunicator.PrintMessage(statusMsg); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase1 failed on Request Port"); } allocatedport = statusMsg.port; Console.WriteLine("Allocated port of " + allocatedport.ToString()); PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeKEEP_ALIVE(FTPServer, allocatedport)); statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); //PRSCommunicator.PrintMessage(statusMsg); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase1 failed on Keep Alive"); } PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeCLOSE_PORT(FTPServer, allocatedport)); statusMsg = PRSCommunicator.ReceiveMessage(clientsocket, ref remoteEP); //PRSCommunicator.PrintMessage(statusMsg); if (statusMsg.status != PRSMessage.Status.SUCCESS) { throw new Exception("TestCase1 failed on ClosePort"); } }
static void Main(string[] args) { // TODO: interpret cmd line options /* * -p <service port> * -s <starting client port number> * -e <ending client port number> * -t <keep alive time in seconds> */ int servicePort = 30000; int startingClientPort = 40000; int endingClientPort = 40099; int keepAlive = 300; // initialize a collection of un-reserved ports to manage List <ManagedPort> ports = new List <ManagedPort>(); for (int p = startingClientPort; p <= endingClientPort; p++) { ManagedPort mp = new ManagedPort(); mp.port = p; mp.reserved = false; ports.Add(mp); } // create the socket for receiving messages at the server Socket listeningSocket = new Socket(SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("Listening socket created"); // bind the socket to the server port listeningSocket.Bind(new IPEndPoint(IPAddress.Any, servicePort)); Console.WriteLine("Listening socket bound to port " + servicePort.ToString()); // listen for client messages bool done = false; while (!done) { try { // receive a message from a client IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); PRSMessage msg = PRSCommunicator.ReceiveMessage(listeningSocket, ref remoteEP); // handle the message PRSMessage response = null; switch (msg.msgType) { case PRSMessage.MsgType.REQUEST_PORT: Console.WriteLine("Received REQUEST_PORT message"); response = Handle_REQUEST_PORT(msg); break; case PRSMessage.MsgType.STOP: Console.WriteLine("Received STOP message"); done = true; break; default: // TODO: handle unknown message type! break; } if (response != null) { // send response message back to client PRSCommunicator.SendMessage(listeningSocket, remoteEP, response); } } catch (Exception ex) { Console.WriteLine("Exception when receiving..." + ex.Message); } } // close the socket and quit Console.WriteLine("Closing down"); listeningSocket.Close(); Console.WriteLine("Closed!"); Console.ReadKey(); }
static void Main(string[] args) { string ADDRESS = "127.0.0.1"; int PORT = 30000; // create the socket for sending messages to the server Socket clientSocket = new Socket(SocketType.Dgram, ProtocolType.Udp); Console.WriteLine("Socket created"); // construct the server's address and port IPEndPoint endPt = new IPEndPoint(IPAddress.Parse(ADDRESS), PORT); try { string serviceName = "foo"; ushort allocatedPort = 0; // send REQUEST_PORT PRSCommunicator.SendMessage(clientSocket, endPt, PRSMessage.CreateREQUEST_PORT(serviceName)); // check status IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); PRSMessage statusMsg = PRSCommunicator.ReceiveMessage(clientSocket, ref remoteEP); if (statusMsg.status == PRSMessage.Status.SUCCESS) { allocatedPort = statusMsg.port; Console.WriteLine("Allocated port of " + allocatedPort.ToString()); } else if (statusMsg.status == PRSMessage.Status.SERVICE_IN_USE) { Console.WriteLine("service in use!"); } else if (statusMsg.status == PRSMessage.Status.ALL_PORTS_BUSY) { Console.WriteLine("all ports busy"); } // send KEEP_ALIVE PRSCommunicator.SendMessage(clientSocket, endPt, PRSMessage.CreateKEEP_ALIVE(serviceName, allocatedPort)); // check status statusMsg = PRSCommunicator.ReceiveMessage(clientSocket, ref remoteEP); if (statusMsg.status == PRSMessage.Status.SUCCESS) { Console.WriteLine("success!! yay!"); } // send CLOSE_PORT // check status // send STOP PRSCommunicator.SendMessage(clientSocket, endPt, PRSMessage.CreateSTOP()); } catch (Exception ex) { Console.WriteLine("Exception when receiving..." + ex.Message); } // close the socket and quit Console.WriteLine("Closing down"); clientSocket.Close(); Console.WriteLine("Closed!"); Console.ReadKey(); }
private static void StopServer(Socket clientsocket) { IPEndPoint endPt = new IPEndPoint(IPAddress.Parse(PRSCommunicator.DEFAULT_IP), PRSCommunicator.DEFAULT_PORT); PRSCommunicator.SendMessage(clientsocket, endPt, PRSMessage.MakeSTOP()); }