Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3.  
  4. // This will stop the threads/connections and toggle the button back to its original state
  5. if (button2.Text == "Stop Listening")
  6. {
  7.  
  8. listener.Close();
  9. stop = true;
  10. threadsActive = false;
  11. button2.Text = "Start Listening";
  12. textBox1.AppendText("Manually Closed Threads/Connections" + Environment.NewLine);
  13.  
  14. }
  15. else
  16. {
  17.  
  18. listenThread = new Thread(listenLoop);
  19. listenThread.IsBackground = true;
  20. status = new Thread(checkIfOnline);
  21. status.IsBackground = true;
  22. stop = false;
  23. threadsActive = true;
  24. button2.Text = "Stop Listening";
  25. localEndPoint = new IPEndPoint(IPAddress.Parse("129.59.79.65"), 3000);
  26. listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27. listenThread.Start();
  28. status.Start();
  29.  
  30. }
  31.  
  32. }
  33.  
  34. private void listenLoop()
  35. {
  36.  
  37. try
  38. {
  39.  
  40. listener.Bind(localEndPoint);
  41. listener.Listen(100);
  42.  
  43. textBox1.AppendText("Waiting for a client..." + Environment.NewLine);
  44.  
  45. listener = listener.Accept();
  46. textBox1.AppendText("Client Connected!!" + Environment.NewLine);
  47.  
  48. status.Start();
  49.  
  50. while (!close)
  51. {
  52.  
  53. if (stop)
  54. return;
  55. // server connection loop
  56.  
  57. }
  58.  
  59. if(close)
  60. return;
  61.  
  62. }
  63. catch (Exception excp)
  64. {
  65.  
  66.  
  67.  
  68. }
  69.  
  70. }
  71.  
  72. private void ResetSocket()
  73. {
  74.  
  75. // stop all threads and connections
  76. stop = true;
  77. listener.Close();
  78.  
  79. textBox1.AppendText("Attempting to kill threads..." + Environment.NewLine);
  80. //while (listenThread.IsAlive == true || status.IsAlive == true) { /*loop until the threads are killed*/ textBox1.AppendText("Closing Threads..."); }
  81. //listener.Close();
  82. threadsActive = false;
  83.  
  84. textBox1.AppendText("All Threads/Connections Closed" + Environment.NewLine + "Restarting Threads/Connections..." + Environment.NewLine);
  85.  
  86. // re-establish and start threads and connections again
  87. stop = false;
  88. listenThread = new Thread(listenLoop);
  89. listenThread.IsBackground = true;
  90. status = new Thread(checkIfOnline);
  91. status.IsBackground = true;
  92. threadsActive = true;
  93. localEndPoint = new IPEndPoint(IPAddress.Parse("129.59.79.65"), 3000);
  94. listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  95. listenThread.Start();
  96. status.Start();
  97.  
  98. textBox1.AppendText("Threads/Connections Restarted Successfully" + Environment.NewLine);
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement