Advertisement
Guest User

потоки

a guest
Jan 9th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 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;
  7. using System.Net.Sockets;
  8. using System.IO;
  9.  
  10. namespace ChatTCP
  11. {
  12.     internal class Client
  13.     {
  14.         public string message;
  15.         public string text;
  16.         public string name;
  17.         public string sendMessage;
  18.  
  19.         static TcpClient client = new TcpClient();
  20.         static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21.         static Stream stream = null;
  22.         static public string address = "127.0.0.1";
  23.         static public short port = 2000;
  24.         static IPEndPoint ep = new IPEndPoint(IPAddress.Parse(address), port);
  25.         public bool Connect()
  26.         {
  27.             try
  28.             {
  29.                 client.Connect(ep);
  30.                 socket.Connect(ep);
  31.                 if(client.Connected && socket.Connected)
  32.                 {
  33.                     stream = new NetworkStream(socket);
  34.                     message = "Подключение выполнено";
  35.                     return true;
  36.                     ProcessMessage();
  37.                 }
  38.                 else
  39.                 {
  40.                     return false;
  41.                 }
  42.             }
  43.             catch(Exception e)
  44.             {
  45.                 message = $"Не удалось подключится к серверу: {e}";
  46.                 return false;
  47.             }
  48.         }
  49.         public void SendMessage()
  50.         {
  51.             StreamWriter sw = new StreamWriter(stream);
  52.             try
  53.             {
  54.                 sw.WriteLine("\n" + name + ": " + text);
  55.             }
  56.             catch(Exception e)
  57.             {
  58.                 message = e.ToString();
  59.             }
  60.         }
  61.         private void ProcessMessage()
  62.         {
  63.             StreamReader sr = new StreamReader(stream);
  64.             while (client.Connected)
  65.             {
  66.                 try
  67.                 {
  68.                     sendMessage = sr.ReadLine();
  69.                 }
  70.                 catch (Exception e)
  71.                 {
  72.                     message = e.ToString();
  73.                 }
  74.                 System.Threading.Thread.Sleep(500);
  75.             }
  76.         }
  77.         public void Disconnect()
  78.         {
  79.             try
  80.             {
  81.                 client.Close();
  82.             }
  83.             catch(Exception e)
  84.             {
  85.                 message = e.ToString();
  86.             }
  87.         }
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement