Example #1
0
 private void TCPServerInstantiate()
 {
     localEndPoint        = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6161);
     TCPServer            = new TcpListener(localEndPoint);
     isTheserverRunning   = new ServerTestAssistant(); //This will be used as a way to share information between the threads. After isTheserverRunning's serverRunning property will be set to "true", this.Disconnect will be enabled. This is done to prevent calling this.server.Start() after the server has been disposed in this.stopServer().
     this.TCPServerThread = new Thread(() => TCPServerThreadWork(isTheserverRunning));
     this.TCPServerThread.Start();
 }
Example #2
0
 private void TCPServerThreadWork(ServerTestAssistant serverRunning)
 {
     try
     {
         TCPServer.Start();
         serverRunning.serverRunning = true;
         MessageBox.Show("Server is running...");
         bool serverStopped = false;
         try
         {
             this.client = TCPServer.AcceptTcpClient();
         }
         catch (SocketException) { serverStopped = true; }//In case the server was closed before a connection could be made.
         if (!serverStopped)
         {
             MessageBox.Show("Connected to client.");
             IPAddress clientIP = ((IPEndPoint)(client.Client.RemoteEndPoint)).Address;
             //serverRunning.IP = clientIP.ToString();
             NetworkStream stream = client.GetStream();
             byte[]        dataToReceive = new byte[client.ReceiveBufferSize];
             int           bytesRead, statusCode;
             if (client.Connected)
             {
                 System.Timers.Timer timer = new System.Timers.Timer();
                 timer.Stop();
                 timer.Elapsed += new ElapsedEventHandler((object caller, ElapsedEventArgs args) =>
                 {
                     this.connectionBroken = true;
                     this.disconnectStatusCodeReceived();
                     throw new ApplicationException("Connection broken.");
                 });
                 timer.Interval = 600;
                 try
                 {
                     timer.Stop();
                     //timer.Start();
                     this.statusCodeToSend = this.STILLCONNECTEDCODE;
                     while (!this.connectionBroken)
                     {
                         dataToReceive = new byte[client.ReceiveBufferSize];
                         bytesRead     = stream.Read(dataToReceive, 0, dataToReceive.Length);
                         timer.Stop();
                         string allDataRead = Encoding.UTF8.GetString(dataToReceive, 0, bytesRead);
                         foreach (string uneditedDataRead in allDataRead.Split('~'))
                         {
                             if (uneditedDataRead == "")
                             {
                                 continue;
                             }
                             string dataRead = uneditedDataRead.Replace(this.FRAME.ToString(), "");
                             if (int.TryParse(dataRead, out statusCode))
                             {
                                 if (statusCode == this.DISCONNECTCODE)
                                 {
                                     MessageBox.Show("Client had disconnected.");
                                     this.disconnectStatusCodeReceived();
                                     this.connectionBroken = true;
                                     break;
                                 }
                                 else if (statusCode != this.STILLCONNECTEDCODE)
                                 {
                                     MessageBox.Show("Connection broken (unidentified status code " + dataRead + " received from client).\nClosing server.");
                                     this.disconnectStatusCodeReceived();
                                     this.connectionBroken = true;
                                     break;
                                 }
                                 else
                                 {
                                     timer.Start();
                                 }
                             }
                             else
                             {
                                 MessageBox.Show("Invalid characters received.\nMessage was: " + dataRead + "\nBreaking the program's run.");
                                 this.disconnectStatusCodeReceived();
                                 this.connectionBroken = true;
                                 break;
                             }
                         }
                     }
                     //client.Close();
                 }
                 catch (ApplicationException exc)
                 {
                     MessageBox.Show(exc.Message);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         if (!(exception is ThreadAbortException))
         {
             MessageBox.Show(exception.ToString());
         }
         //else the thread was aborted, i.e, the user pressed "Disconnect".
     }
 }