Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void button2_Click(object sender, EventArgs e)
- {
- // This will stop the threads/connections and toggle the button back to its original state
- if (button2.Text == "Stop Listening")
- {
- listener.Close();
- stop = true;
- threadsActive = false;
- button2.Text = "Start Listening";
- textBox1.AppendText("Manually Closed Threads/Connections" + Environment.NewLine);
- }
- else
- {
- listenThread = new Thread(listenLoop);
- listenThread.IsBackground = true;
- status = new Thread(checkIfOnline);
- status.IsBackground = true;
- stop = false;
- threadsActive = true;
- button2.Text = "Stop Listening";
- localEndPoint = new IPEndPoint(IPAddress.Parse("129.59.79.65"), 3000);
- listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listenThread.Start();
- status.Start();
- }
- }
- private void listenLoop()
- {
- try
- {
- listener.Bind(localEndPoint);
- listener.Listen(100);
- textBox1.AppendText("Waiting for a client..." + Environment.NewLine);
- listener = listener.Accept();
- textBox1.AppendText("Client Connected!!" + Environment.NewLine);
- status.Start();
- while (!close)
- {
- if (stop)
- return;
- // server connection loop
- }
- if(close)
- return;
- }
- catch (Exception excp)
- {
- }
- }
- private void ResetSocket()
- {
- // stop all threads and connections
- stop = true;
- listener.Close();
- textBox1.AppendText("Attempting to kill threads..." + Environment.NewLine);
- //while (listenThread.IsAlive == true || status.IsAlive == true) { /*loop until the threads are killed*/ textBox1.AppendText("Closing Threads..."); }
- //listener.Close();
- threadsActive = false;
- textBox1.AppendText("All Threads/Connections Closed" + Environment.NewLine + "Restarting Threads/Connections..." + Environment.NewLine);
- // re-establish and start threads and connections again
- stop = false;
- listenThread = new Thread(listenLoop);
- listenThread.IsBackground = true;
- status = new Thread(checkIfOnline);
- status.IsBackground = true;
- threadsActive = true;
- localEndPoint = new IPEndPoint(IPAddress.Parse("129.59.79.65"), 3000);
- listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- listenThread.Start();
- status.Start();
- textBox1.AppendText("Threads/Connections Restarted Successfully" + Environment.NewLine);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement