Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Networking_server
  12. {
  13. class Program
  14. {
  15. public static string password = "";
  16. public static string userName = "";
  17. public static string ipAddress = "";
  18. public static string serverAddress = "";
  19.  
  20. static void Main(string[] args)
  21. {
  22. CreateUser();
  23. Server myServer = new Server();
  24. Thread serverThread = new Thread(myServer.Run);
  25. serverThread.Start();
  26. serverThread.Join();
  27. }
  28.  
  29. private static void CreateUser()
  30. {
  31. Console.Write("Server IP: ");
  32. serverAddress = Console.ReadLine();
  33. Console.Write("Username: ");
  34. userName = Console.ReadLine();
  35. Console.Write("Password: ");
  36. password = Console.ReadLine();
  37.  
  38. IPHostEntry host;
  39. host = Dns.GetHostEntry(Dns.GetHostName());
  40.  
  41. foreach (IPAddress ip in host.AddressList)
  42. {
  43. if (ip.AddressFamily == AddressFamily.InterNetwork)
  44. {
  45. ipAddress = ip.ToString();
  46. }
  47. }
  48. User user = new User { "rawr", "rawr", "rawr" };
  49.  
  50.  
  51. }
  52. }
  53.  
  54. public class Server
  55. {
  56. List<ClientHandler> clients = new List<ClientHandler>();
  57. List<ClientHandler> clients2 = new List<ClientHandler>();
  58. public void Run()
  59. {
  60. TcpListener listener = new TcpListener(IPAddress.Any, 5000);
  61. Console.WriteLine("Server up and running, waiting for messages...");
  62.  
  63. try
  64. {
  65. listener.Start();
  66.  
  67. while (true)
  68. {
  69. try
  70. {
  71. TcpClient c = listener.AcceptTcpClient();
  72. ClientHandler newClient = new ClientHandler(c, this);
  73. foreach (var client in clients)
  74. {
  75. if (client != newClient)
  76. {
  77. clients2.Add(newClient);
  78. }
  79. }
  80. clients = clients2;
  81. clients2.Clear();
  82. Thread clientThread = new Thread(newClient.Run);
  83. clientThread.Start();
  84.  
  85. }
  86. catch (Exception ex)
  87. {
  88. Console.WriteLine(ex.Message + "sörvör run exception");
  89. }
  90.  
  91.  
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. Console.WriteLine(ex.Message + "sörvör run exception");
  97. }
  98. finally
  99. {
  100. if (listener != null)
  101. listener.Stop();
  102. }
  103. }
  104.  
  105. public void Broadcast(ClientHandler client, string message)
  106. {
  107. foreach (var item in clients)
  108. {
  109. if (item.tcpclient.Connected)
  110. {
  111. clients2.Add(item);
  112. }
  113. }
  114. foreach (ClientHandler tmpClient in clients2)
  115. {
  116. if (tmpClient != client)
  117. {
  118. NetworkStream n = tmpClient.tcpclient.GetStream();
  119. BinaryWriter w = new BinaryWriter(n);
  120. w.Write(message);
  121. w.Flush();
  122. }
  123. else if (clients.Count() == 1)
  124. {
  125. NetworkStream n = tmpClient.tcpclient.GetStream();
  126. BinaryWriter w = new BinaryWriter(n);
  127. w.Write("Sorry, you are alone...");
  128. w.Flush();
  129. }
  130. }
  131. }
  132.  
  133. public void DisconnectClient(ClientHandler client)
  134. {
  135. clients.Remove(client);
  136. Console.WriteLine("Client X has left the building...");
  137. Broadcast(client, "Client X has left the building...");
  138. }
  139. }
  140.  
  141. public class ClientHandler
  142. {
  143. public TcpClient tcpclient;
  144. private Server myServer;
  145. private string UserName;
  146. public ClientHandler(TcpClient c, Server server)
  147. {
  148. tcpclient = c;
  149. this.myServer = server;
  150. }
  151.  
  152. public void Run()
  153. {
  154. try
  155. {
  156. string message = "";
  157. while (!message.Equals("quit"))
  158. {
  159. NetworkStream n = tcpclient.GetStream();
  160. message = new BinaryReader(n).ReadString();
  161. myServer.Broadcast(this, message);
  162. Console.WriteLine(message);
  163. }
  164.  
  165. myServer.DisconnectClient(this);
  166. tcpclient.Close();
  167. }
  168. catch (Exception ex)
  169. {
  170. myServer.DisconnectClient(this);
  171. tcpclient.Close();
  172. Console.WriteLine(ex.Message + "röv");
  173. }
  174. }
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement