Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. using System.Threading;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Net;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. public static Hashtable clientsList = new Hashtable();
  12.  
  13. static void Main(string[] args)
  14. {
  15. TcpListener serverSocket = new TcpListener(IPAddress.Any,3306);
  16. TcpClient clientSocket = default(TcpClient);
  17. int counter = 0;
  18.  
  19. serverSocket.Start();
  20. Console.WriteLine("Chat Server Started ....");
  21. counter = 0;
  22.  
  23. while ((true))
  24. {
  25. try
  26. {
  27. counter += 1;
  28. clientSocket = serverSocket.AcceptTcpClient();
  29.  
  30. byte[] bytesFrom = new byte[10025];
  31. string dataFromClient = null;
  32.  
  33. NetworkStream networkStream = clientSocket.GetStream();
  34. networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  35. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  36. dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  37.  
  38. clientsList.Add(dataFromClient, clientSocket);
  39.  
  40. broadcast(dataFromClient + " Joined ", dataFromClient, false);
  41.  
  42. Console.WriteLine(dataFromClient + " Joined chat room ");
  43. handleClinet client = new handleClinet();
  44. client.startClient(clientSocket, dataFromClient, clientsList);
  45. }
  46. catch { }
  47. }
  48.  
  49. Console.ReadKey();
  50. clientSocket.Close();
  51. serverSocket.Stop();
  52. Console.WriteLine("exit");
  53. Console.ReadLine();
  54. }
  55.  
  56. public static void broadcast(string msg, string uName, bool flag)
  57. {
  58. try
  59. {
  60. foreach (DictionaryEntry Item in clientsList)
  61. {
  62. TcpClient broadcastSocket;
  63. broadcastSocket = (TcpClient)Item.Value;
  64. NetworkStream broadcastStream = broadcastSocket.GetStream();
  65. Byte[] broadcastBytes = null;
  66.  
  67. if (flag == true)
  68. {
  69. broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
  70. }
  71. else
  72. {
  73. broadcastBytes = Encoding.ASCII.GetBytes(msg);
  74. }
  75.  
  76. broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
  77. broadcastStream.Flush();
  78. }
  79. }
  80. catch { Console.ReadKey(); }
  81. } //end broadcast function
  82. }//end Main class
  83.  
  84.  
  85. public class handleClinet
  86. {
  87. TcpClient clientSocket;
  88. string clNo;
  89. Hashtable clientsList;
  90.  
  91. public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
  92. {
  93. this.clientSocket = inClientSocket;
  94. this.clNo = clineNo;
  95. this.clientsList = cList;
  96. Thread ctThread = new Thread(doChat);
  97. ctThread.Start();
  98. }
  99.  
  100. private void doChat()
  101. {
  102. int requestCount = 0;
  103. byte[] bytesFrom = new byte[10025];
  104. string dataFromClient = null;
  105. Byte[] sendBytes = null;
  106. string serverResponse = null;
  107. string rCount = null;
  108. requestCount = 0;
  109.  
  110. while ((true))
  111. {
  112. try
  113. {
  114. requestCount = requestCount + 1;
  115. NetworkStream networkStream = clientSocket.GetStream();
  116. networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  117. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  118. dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  119. if (dataFromClient == "closing")
  120. {
  121. clientsList.Remove(clNo);
  122. Program.broadcast("Sa deconectat", clNo, true);
  123. break;
  124. }
  125. Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
  126. rCount = Convert.ToString(requestCount);
  127.  
  128. Program.broadcast(dataFromClient, clNo, true);
  129. }
  130. catch (Exception ex)
  131. {
  132. Console.WriteLine("Opsy");
  133. Console.ReadKey();
  134. }
  135. }//end while
  136. }//end doChat
  137. } //end class handleClinet
  138. }//end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement