Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 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.Threading;
  9.  
  10. namespace Client
  11. {
  12.     class Program
  13.     {
  14.         public class AsyncClient
  15.         {
  16.             private Socket Client;
  17.             private byte[] buffer;
  18.  
  19.             public AsyncClient()
  20.             {
  21.                 Setup();
  22.                 Console.ReadKey();
  23.             }
  24.             private void Setup()
  25.             {
  26.                 try
  27.                 {
  28.                     Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  29.                     Client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 1111), new AsyncCallback(ConnectCallBack), null);
  30.  
  31.                     Send();
  32.                 }
  33.                 catch(Exception ex)
  34.                 {
  35.                     Console.WriteLine(ex.ToString());
  36.                 }
  37.             }
  38.  
  39.             private void ConnectCallBack(IAsyncResult AR)
  40.             {
  41.                 try
  42.                 {
  43.                     Client.EndAccept(AR);
  44.                     Console.WriteLine("Connected.");
  45.                 }
  46.                 catch (Exception ex)
  47.                 {
  48.                     Console.WriteLine(ex.ToString());
  49.                 }
  50.             }
  51.             private void Send()
  52.             {
  53.                 while (true)
  54.                 {
  55.                     string text = "";
  56.                     text = Console.ReadLine();
  57.                     buffer = new byte[text.Length];
  58.                     for (int i = 0; i < text.Length; i++)
  59.                     {
  60.                         buffer[i] = (byte)text[i];
  61.                     }
  62.                     Client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
  63.                 }
  64.  
  65.             }
  66.             private void SendCallBack(IAsyncResult AR)
  67.             {
  68.                 try
  69.                 {
  70.                     Client.EndSend(AR);
  71.                 }
  72.                 catch (Exception ex)
  73.                 {
  74.                     Console.WriteLine(ex.ToString());
  75.                 }
  76.             }
  77.  
  78.         }
  79.  
  80.        
  81.  
  82.        
  83.  
  84.         static void Main(string[] args)
  85.         {
  86.             AsyncClient AC = new AsyncClient();
  87.            
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement