private void acceptConnection() { /*Pre: the listenOnPort method created a thread.*/ /*Post:the method will continually look for new connections*/ listening = true; while (listening == true) { //blocking - the socket starts listening tcpServer.Listen(int.MaxValue); try { //wait for connection attempted, store socket info inside tryToConnect Socket tryToConnect = tcpServer.Accept(); //if number of clients does not exceed MAXCLIENTS int numClients = clientList.Count; if (numClients < MAXCLIENTS && tryToConnect != null) { /***ADD Name for client - hopefully something random***/ //create a new client object (with thread) with socket information passed to constructor RTSPClient client = new RTSPClient(tryToConnect, (r.Next(10000) + r.Next(10)).ToString()); //and add client object to linked list clientList.AddLast(client); //tell main view that client was added referenceToView.Invoke(referenceToView.changeServerStatusTextBox, "Client added."); } else { //otherwise tell main view client was not added referenceToView.Invoke(referenceToView.changeServerStatusTextBox, "Client could not be added."); } } catch (SocketException se) { //if socket fails close it if (tcpServer != null) tcpServer.Close(); //and abort thread if (listeningOnPort != null) listeningOnPort.Abort(); } catch (ThreadAbortException tae) { //if thread is attempted to abort check if socket is closed if not close it. if (tcpServer != null) tcpServer.Close(); } catch (Exception e) { } } }
public void removeClient(RTSPClient removeThisClient) { /*Pre: a client object to be removed is supplied*/ /*Post:the client object has been removed from the linked list making room for other clients*/ try { //if client object is in the list if (clientList.Contains(removeThisClient)) { //remove the object from the list clientList.Remove(removeThisClient); } } catch (ArgumentException ae) { //if a bad argument was supplied tell the main view what caused it if (referenceToView != null) referenceToView.Invoke(referenceToView.changeServerStatusTextBox, "Argument exception in RemoveClient StreamingServer: "+ae.ToString()); } }