Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace IO2
  11. {
  12. class Program
  13. {
  14. private static byte[] buffer;
  15.  
  16. public static object TcpClientclient { get; private set; }
  17.  
  18. static void Main(string[] args)
  19. {
  20. ThreadPool.QueueUserWorkItem(ServerTCP);
  21. ThreadPool.QueueUserWorkItem(ClientTCP);
  22. ThreadPool.QueueUserWorkItem(ClientTCP);
  23. Thread.Sleep(10000);
  24. }
  25.  
  26. static void ThreadProc(Object stateInfo)
  27. {
  28. var integer = (int)stateInfo;
  29. Thread.Sleep(integer);
  30. Console.WriteLine(integer);
  31. }
  32.  
  33. static void ServerTCP(object state)
  34. {
  35. TcpListener listener = new TcpListener(IPAddress.Any, 8888);
  36. listener.Start();
  37. while (true)
  38. {
  39. TcpClient client = listener.AcceptTcpClient();
  40. byte[] odczyt = new byte[1024];
  41. while (true)
  42. {
  43. client.GetStream().Read(odczyt, 0, 1024);
  44. Console.WriteLine(System.Text.Encoding.ASCII.GetString(odczyt));
  45. client.GetStream().Write(odczyt, 0, odczyt.Length);
  46. }
  47. client.Close();
  48. }
  49.  
  50. }
  51. static void WriteConsoleMessage(string message, ConsoleColor color)
  52. {
  53. Console.ForegroundColor = color;
  54. Console.WriteLine(message);
  55. Console.ResetColor();
  56. }
  57. static void ClientTCP(object state)
  58. {
  59. TcpClient client = new TcpClient();
  60. client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
  61. byte[] message = new ASCIIEncoding().GetBytes("wiadomosc");
  62. client.GetStream().Write(message, 0, message.Length);
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement