Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1.  public class StateObject
  2.     {
  3.         public Socket workSocket = null;
  4.         public const int BufferSize = 1024;
  5.         public byte[] buffer = new byte[BufferSize];
  6.         public StringBuilder sb = new StringBuilder();
  7.     }
  8.  
  9.     public class AsynchronousSocketListener
  10.     {
  11.         public static ManualResetEvent allDone = new ManualResetEvent(false);
  12.         public AsynchronousSocketListener()
  13.         {
  14.         }
  15.         public static void StartListening()
  16.         {
  17.             IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
  18.             IPAddress ipAddress = ipHostInfo.AddressList[0];
  19.             IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
  20.  
  21.             Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  22.          try
  23.             {
  24.                 listener.Bind(localEndPoint);
  25.                 listener.Listen(100);
  26.                 while (true)
  27.                 {
  28.                     allDone.Reset();
  29.                     listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
  30.                     allDone.WaitOne();
  31.                 }
  32.             }
  33.        catch (Exception p)
  34.             {
  35.                 Console.WriteLine("Error..... " + p.StackTrace);
  36.             }
  37.         }
  38.         public static void AcceptCallback(IAsyncResult ar)
  39.         {
  40.             allDone.Set();
  41.             Socket listener = (Socket)ar.AsyncState;
  42.             Socket handler = listener.EndAccept(ar);
  43.             StateObject state = new StateObject();
  44.             state.workSocket = handler;
  45.             handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
  46.         }
  47.         public static void ReadCallback(IAsyncResult ar)
  48.         {
  49.             String content = String.Empty;
  50.             StateObject state = (StateObject)ar.AsyncState;
  51.             Socket handler = state.workSocket;
  52.             int bytesRead = handler.EndReceive(ar);
  53.             if (bytesRead > 0)
  54.             {
  55.                 state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
  56.                 content = state.sb.ToString();
  57.                 content = "x";
  58.                 Send(handler, content);
  59.             }
  60.         }
  61.  
  62.         private static void Send(Socket handler, String data)
  63.         {  
  64.             byte[] byteData = Encoding.ASCII.GetBytes(data);
  65.             handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
  66.         }
  67.         private static void SendCallback(IAsyncResult ar)
  68.         {
  69.             try
  70.             {
  71.                 Socket handler = (Socket)ar.AsyncState;
  72.                 int bytesSent = handler.EndSend(ar);
  73.                 handler.Shutdown(SocketShutdown.Both);
  74.                 handler.Close();
  75.             }
  76.             catch (Exception p)
  77.             {
  78.                 Console.WriteLine("Error..... " + p.StackTrace);
  79.             }
  80.         }
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement