Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Net;
  9.  
  10. namespace ConsoleApplication1
  11. {
  12.     class Program
  13.     {
  14.         public static Object thisLock = new object();
  15.         static void Main(string[] args)
  16.         {
  17.             ThreadPool.QueueUserWorkItem(ThreadSerwer);
  18.             ThreadPool.QueueUserWorkItem(ThreadKlient);
  19.             ThreadPool.QueueUserWorkItem(ThreadKlient);
  20.  
  21.             Thread.Sleep(1000);
  22.             Console.ReadLine();
  23.         }
  24.  
  25.         static void ThreadProc(object stateInfo)
  26.         {
  27.             Thread.Sleep((int)stateInfo);
  28.             Console.WriteLine(stateInfo);
  29.         }
  30.         static void ThreadSerwer(object stateInfo)
  31.         {
  32.             TcpListener server = new TcpListener(IPAddress.Any, 2048);
  33.             server.Start();
  34.  
  35.             while (true)
  36.             {
  37.                 TcpClient client = server.AcceptTcpClient();
  38.                 ThreadPool.QueueUserWorkItem(ThreadConnection);
  39.  
  40.                 byte[] buffer = new byte[1024];
  41.                 client.GetStream().Read(buffer, 0, 1024);
  42.                 client.GetStream().Write(buffer, 0, buffer.Length);
  43.  
  44.                 string result = Encoding.UTF8.GetString(buffer);
  45.  
  46.                 writeConsoleMessage("Serwer: " + result, ConsoleColor.Red);
  47.             }
  48.  
  49.         }
  50.         static void ThreadKlient(object stateInfo)
  51.         {
  52.             TcpClient client = new TcpClient();
  53.             client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2048));
  54.  
  55.             byte[] message = new ASCIIEncoding().GetBytes("wiadomosc");
  56.             client.GetStream().Write(message, 0, message.Length);
  57.             client.GetStream().Read(message, 0, message.Length);
  58.  
  59.             string result = Encoding.UTF8.GetString(message);
  60.  
  61.             writeConsoleMessage("Klient: " + result, ConsoleColor.Green);
  62.         }
  63.  
  64.         static void ThreadConnection(object stateInfo)
  65.         {
  66.  
  67.         }
  68.  
  69.         static void writeConsoleMessage(string message, ConsoleColor color)
  70.         {
  71.             lock (thisLock)
  72.             {
  73.                 Console.ForegroundColor = color;
  74.                 Console.WriteLine(message);
  75.                 Console.ResetColor();
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement