blondedude092

ServerMainForm.cs

Feb 1st, 2016
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net.Sockets;
  11. using System.Net;
  12.  
  13. namespace Server
  14. {
  15. public partial class ServerMainForm : Form
  16. {
  17. TcpListener Listener;
  18. List<Client> ClientList = new List<Client>();
  19.  
  20. public ServerMainForm()
  21. {
  22. InitializeComponent();
  23. StartStropListening();
  24. }
  25.  
  26. private void StartStropListening()
  27. {
  28. try
  29. {
  30. if (button1.Text == "Start")
  31. {
  32. Listener = new TcpListener(IPAddress.Any, Convert.ToInt32(textBox1.Text));
  33. Listener.Start();
  34. Listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), Listener);
  35. button1.Text = "Stop";
  36.  
  37. }
  38. else
  39. {
  40. Listener.Stop();
  41. button1.Text = "Start";
  42.  
  43. }
  44.  
  45.  
  46. }
  47. catch (Exception)
  48. {
  49.  
  50. throw;
  51. }
  52. }
  53.  
  54. private void AcceptClient(IAsyncResult ar)
  55. {
  56. try
  57. {
  58. Client client = new Client(Listener.EndAcceptTcpClient(ar));
  59.  
  60. client.DataRecievedEvent += DataRecieved;
  61.  
  62. client.ClientExitedEvent += ClientExited;
  63.  
  64. ClientList.Add(client);
  65.  
  66. SetText("Clients connected: " + ClientList.Count);
  67. // ClientsConnectedLabel.Text = "Clients connected: " + ClientList.Count;
  68.  
  69. //call this to accept next client...
  70. Listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), Listener);
  71.  
  72.  
  73. }
  74. catch (Exception)
  75. {
  76.  
  77. throw;
  78. }
  79. }
  80.  
  81. private void button1_Click(object sender, EventArgs e)
  82. {
  83. StartStropListening();
  84. }
  85.  
  86. private void DataRecieved(Byte[] data)
  87. {
  88. //just to let the server know that globally there is data recieved...
  89.  
  90. }
  91.  
  92. private void ClientExited(Client client)
  93. {
  94. // the client exited event was raised thus we need to remove them fromt he list...
  95. ClientList.Remove(client);
  96. SetText("Clients connected: " + ClientList.Count);
  97.  
  98. }
  99.  
  100. delegate void SetTextCallback(string text);
  101. private void SetText(string text)
  102. {
  103. // InvokeRequired required compares the thread ID of the
  104. // calling thread to the thread ID of the creating thread.
  105. // If these threads are different, it returns true.
  106. if (ClientsConnectedLabel.InvokeRequired)
  107. {
  108. SetTextCallback d = new SetTextCallback(SetText);
  109. this.Invoke(d, new object[] { text });
  110. }
  111. else
  112. {
  113. this.ClientsConnectedLabel.Text = text;
  114. }
  115. }
  116.  
  117. private void button2_Click(object sender, EventArgs e)
  118. {
  119. ClientList[0].SendMessage("hello world! :P ");
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment