Advertisement
Vadorequest

Example.cs

Oct 7th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10.  
  11. namespace SocketsTest
  12. {
  13.     public class Example
  14.     {
  15.         static ManualResetEvent clientDone = new ManualResetEvent(false);
  16.  
  17.         public static void Demo()
  18.         {
  19.             try
  20.             {
  21.                 SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
  22.                 // Don't write http or www before. Don't add '/' after. Else it will fail.
  23.                 DnsEndPoint hostEntry = new DnsEndPoint("137.135.176.144", 1337);
  24.  
  25.                 // Create a socket and connect to the server
  26.                 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  27.  
  28.                 socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
  29.  
  30.                 socketEventArg.RemoteEndPoint = hostEntry;
  31.                 Debug.WriteLine("User token used: "+sock.ToString());
  32.                 socketEventArg.UserToken = sock;
  33.                 sock.ConnectAsync(socketEventArg);
  34.                 clientDone.WaitOne();
  35.             }catch(Exception e){
  36.                 Debug.WriteLine(e.Message);
  37.             }
  38.         }
  39.  
  40.         // A single callback is used for all socket operations.
  41.         // This method forwards execution on to the correct handler
  42.         // based on the type of completed operation
  43.         static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
  44.         {
  45.             Debug.WriteLine("Operation processing: " + e.LastOperation.ToString());
  46.             switch (e.LastOperation)
  47.             {
  48.                 case SocketAsyncOperation.Connect:
  49.                     ProcessConnect(e);
  50.                     break;
  51.                 case SocketAsyncOperation.Receive:
  52.                     ProcessReceive(e);
  53.                     break;
  54.                 case SocketAsyncOperation.Send:
  55.                     ProcessSend(e);
  56.                     break;
  57.                 default:
  58.                     throw new Exception("Invalid operation completed");
  59.             }
  60.         }
  61.  
  62.         // Called when a ConnectAsync operation completes
  63.         private static void ProcessConnect(SocketAsyncEventArgs e)
  64.         {
  65.             if (e.SocketError == SocketError.Success)
  66.             {
  67.                 // Successfully connected to the server
  68.                 Debug.WriteLine("Connect: Successful connection etablished to "+e.RemoteEndPoint.ToString());
  69.                 // Send 'Hello World' to the server
  70.                 byte[] buffer = Encoding.UTF8.GetBytes("connection");
  71.                 e.SetBuffer(buffer, 0, buffer.Length);
  72.                 Socket sock = e.UserToken as Socket;
  73.                 bool willRaiseEvent = sock.SendAsync(e);
  74.                 if (!willRaiseEvent)
  75.                 {
  76.                     ProcessSend(e);
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 throw new SocketException((int)e.SocketError);
  82.             }
  83.         }
  84.  
  85.  
  86.         // Called when a SendAsync operation completes
  87.         private static void ProcessSend(SocketAsyncEventArgs e)
  88.         {
  89.             if (e.SocketError == SocketError.Success)
  90.             {
  91.                 // Sent "Hello World" to the server successfully
  92.  
  93.                 //Read data sent from the server
  94.                 Socket sock = e.UserToken as Socket;
  95.                 bool willRaiseEvent = sock.ReceiveAsync(e);
  96.                 if (!willRaiseEvent)
  97.                 {
  98.                     ProcessReceive(e);
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 throw new SocketException((int)e.SocketError);
  104.             }
  105.         }
  106.  
  107.         // Called when a ReceiveAsync operation completes
  108.         // </summary>
  109.         private static void ProcessReceive(SocketAsyncEventArgs e)
  110.         {
  111.             if (e.SocketError == SocketError.Success)
  112.             {
  113.                 // Received data from server
  114.                 Debug.WriteLine("Data received: " + Encoding.UTF8.GetString(e.Buffer, 0, e.Count) + " (" + e.Count + ")");
  115.                 // Data has now been sent and received from the server.
  116.                 // Disconnect from the server
  117.                 Socket sock = e.UserToken as Socket;
  118.                 sock.Shutdown(SocketShutdown.Send);
  119.                 sock.Close();
  120.                 clientDone.Set();
  121.             }
  122.             else
  123.             {
  124.                 throw new SocketException((int)e.SocketError);
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement