C# TCP/IP simple chat with multiple-clients


SUBMITTED BY: menamagice

DATE: Aug. 9, 2017, 1:52 a.m.

FORMAT: ANTLR With C# Target

SIZE: 3.4 kB

HITS: 193

  1. Heys guys im learning c# and socket programming so i decided to make a tcp chat, the basic idea is that A client send data to the server, then the server broadcast it for all the clients online (in this case all the clients in a dictionary).
  2. When there is 1 client connected, it works as expected, the problem is when there is more than 1 client connected.
  3. Server:
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Dictionary<int,TcpClient> list_clients = new Dictionary<int,TcpClient> ();
  9. int count = 1;
  10. TcpListener ServerSocket = new TcpListener(IPAddress.Any, 5000);
  11. ServerSocket.Start();
  12. while (true)
  13. {
  14. TcpClient client = ServerSocket.AcceptTcpClient();
  15. list_clients.Add(count, client);
  16. Console.WriteLine("Someone connected!!");
  17. count++;
  18. Box box = new Box(client, list_clients);
  19. Thread t = new Thread(handle_clients);
  20. t.Start(box);
  21. }
  22. }
  23. public static void handle_clients(object o)
  24. {
  25. Box box = (Box)o;
  26. Dictionary<int, TcpClient> list_connections = box.list;
  27. while (true)
  28. {
  29. NetworkStream stream = box.c.GetStream();
  30. byte[] buffer = new byte[1024];
  31. int byte_count = stream.Read(buffer, 0, buffer.Length);
  32. byte[] formated = new Byte[byte_count];
  33. Array.Copy(buffer, formated, byte_count); //handle the null characteres in the byte array
  34. string data = Encoding.ASCII.GetString(formated);
  35. broadcast(list_connections, data);
  36. Console.WriteLine(data);
  37. }
  38. }
  39. public static void broadcast(Dictionary<int,TcpClient> conexoes, string data)
  40. {
  41. foreach(TcpClient c in conexoes.Values)
  42. {
  43. NetworkStream stream = c.GetStream();
  44. byte[] buffer = Encoding.ASCII.GetBytes(data);
  45. stream.Write(buffer,0, buffer.Length);
  46. }
  47. }
  48. }
  49. class Box
  50. {
  51. public TcpClient c;
  52. public Dictionary<int, TcpClient> list;
  53. public Box(TcpClient c, Dictionary<int, TcpClient> list)
  54. {
  55. this.c = c;
  56. this.list = list;
  57. }
  58. }
  59. I created this box so i can pass 2 args for the Thread.start()
  60. Client:
  61. class Program
  62. {
  63. static void Main(string[] args)
  64. {
  65. IPAddress ip = IPAddress.Parse("127.0.0.1");
  66. int port = 5000;
  67. TcpClient client = new TcpClient();
  68. client.Connect(ip, port);
  69. Console.WriteLine("client connected!!");
  70. NetworkStream ns = client.GetStream();
  71. string s;
  72. while (true)
  73. {
  74. s = Console.ReadLine();
  75. byte[] buffer = Encoding.ASCII.GetBytes(s);
  76. ns.Write(buffer, 0, buffer.Length);
  77. byte[] receivedBytes = new byte[1024];
  78. int byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length);
  79. byte[] formated = new byte[byte_count];
  80. Array.Copy(receivedBytes, formated, byte_count); //handle the null characteres in the byte array
  81. string data = Encoding.ASCII.GetString(formated);
  82. Console.WriteLine(data);
  83. }
  84. ns.Close();
  85. client.Close();
  86. Console.WriteLine("disconnect from server!!");
  87. Console.ReadKey();
  88. }
  89. }

comments powered by Disqus