Guest User

Untitled

a guest
Nov 17th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. namespace ChatTCPClient
  7. {
  8.     class Program
  9.     {
  10.         static string userName;
  11.         private const string host = "127.0.0.1";
  12.         private const int port = 12000;
  13.         static TcpClient client;
  14.         static NetworkStream stream;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             Console.Write("Введите свое имя: ");
  19.             userName = Console.ReadLine();
  20.             client = new TcpClient();
  21.             try
  22.             {
  23.                 client.Connect(host, port); //подключение клиента
  24.                 stream = client.GetStream(); // получаем поток
  25.  
  26.                 string message = userName;
  27.                 byte[] data = Encoding.Unicode.GetBytes(message);
  28.                 stream.Write(data, 0, data.Length);
  29.  
  30.                 // запускаем новый поток для получения данных
  31.                 Thread receiveThread = new Thread(new ThreadStart(ReceiveMessage));
  32.                 receiveThread.Start(); //старт потока
  33.                 Console.WriteLine("Добро пожаловать, {0}", userName);
  34.                 SendMessage();
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.                 Console.WriteLine(ex.Message);
  39.             }
  40.             finally
  41.             {
  42.                 Disconnect();
  43.             }
  44.         }
  45.         // отправка сообщений
  46.         static void SendMessage()
  47.         {
  48.             Console.WriteLine("Введите сообщение: ");
  49.  
  50.             while (true)
  51.             {
  52.                 string message = Console.ReadLine();
  53.                 byte[] data = Encoding.Unicode.GetBytes(message);
  54.                 stream.Write(data, 0, data.Length);
  55.             }
  56.         }
  57.         // получение сообщений
  58.         static void ReceiveMessage()
  59.         {
  60.             while (true)
  61.             {
  62.                 try
  63.                 {
  64.                     byte[] data = new byte[64]; // буфер для получаемых данных
  65.                     StringBuilder builder = new StringBuilder();
  66.                     int bytes = 0;
  67.                     do
  68.                     {
  69.                         bytes = stream.Read(data, 0, data.Length);
  70.                         builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
  71.                     }
  72.                     while (stream.DataAvailable);
  73.  
  74.                     string message = builder.ToString();
  75.  
  76.                     Console.WriteLine(message);//вывод сообщения
  77.                 }
  78.                 catch
  79.                 {
  80.                     Console.WriteLine("Подключение прервано!"); //соединение было прервано
  81.                     Console.ReadLine();
  82.                     Disconnect();
  83.                 }
  84.             }
  85.         }
  86.  
  87.         static void Disconnect()
  88.         {
  89.             if (stream != null)
  90.                 stream.Close();//отключение потока
  91.             if (client != null)
  92.                 client.Close();//отключение клиента
  93.             Environment.Exit(0); //завершение процесса
  94.         }
  95.     }
  96. }
Add Comment
Please, Sign In to add comment