Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace Client
  8. {
  9.     public class Client
  10.     {
  11.         private readonly Socket _sock;
  12.    
  13.         public void Run()
  14.         {
  15.             new Thread(() =>
  16.             {
  17.                 while (true) ReceiveData();
  18.             }) {IsBackground = true}.Start();
  19.  
  20.             while (true) SendData();
  21.         }
  22.  
  23.         public Client(IPAddress address, int port)
  24.         {
  25.             Socket nouvelle_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  26.             _sock = nouvelle_sock;
  27.             _sock.Connect(address,port);
  28.  
  29.         }
  30.  
  31.         public void ReceiveData()
  32.         {
  33.             byte[] bytes = new byte[1024];
  34.             _sock.Receive(bytes);
  35.             string str = "";
  36.             int i = 0;
  37.             while (bytes[i]!=0)
  38.             {
  39.                 str += (char)(bytes[i]);
  40.                 i++;
  41.             }
  42.             Console.WriteLine("["+ DateTime.Today.ToString()+"] "+str);
  43.         }
  44.  
  45.         public void SendData()
  46.         {
  47.             string message = Console.ReadLine();       
  48.             byte[] msg = Encoding.ASCII.GetBytes(message);
  49.             _sock.Send(msg);
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement