Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace SocketsTest
- {
- public class Example
- {
- static ManualResetEvent clientDone = new ManualResetEvent(false);
- public static void Demo()
- {
- try
- {
- SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
- // Don't write http or www before. Don't add '/' after. Else it will fail.
- DnsEndPoint hostEntry = new DnsEndPoint("137.135.176.144", 1337);
- // Create a socket and connect to the server
- Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
- socketEventArg.RemoteEndPoint = hostEntry;
- Debug.WriteLine("User token used: "+sock.ToString());
- socketEventArg.UserToken = sock;
- sock.ConnectAsync(socketEventArg);
- clientDone.WaitOne();
- }catch(Exception e){
- Debug.WriteLine(e.Message);
- }
- }
- // A single callback is used for all socket operations.
- // This method forwards execution on to the correct handler
- // based on the type of completed operation
- static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
- {
- Debug.WriteLine("Operation processing: " + e.LastOperation.ToString());
- switch (e.LastOperation)
- {
- case SocketAsyncOperation.Connect:
- ProcessConnect(e);
- break;
- case SocketAsyncOperation.Receive:
- ProcessReceive(e);
- break;
- case SocketAsyncOperation.Send:
- ProcessSend(e);
- break;
- default:
- throw new Exception("Invalid operation completed");
- }
- }
- // Called when a ConnectAsync operation completes
- private static void ProcessConnect(SocketAsyncEventArgs e)
- {
- if (e.SocketError == SocketError.Success)
- {
- // Successfully connected to the server
- Debug.WriteLine("Connect: Successful connection etablished to "+e.RemoteEndPoint.ToString());
- // Send 'Hello World' to the server
- byte[] buffer = Encoding.UTF8.GetBytes("connection");
- e.SetBuffer(buffer, 0, buffer.Length);
- Socket sock = e.UserToken as Socket;
- bool willRaiseEvent = sock.SendAsync(e);
- if (!willRaiseEvent)
- {
- ProcessSend(e);
- }
- }
- else
- {
- throw new SocketException((int)e.SocketError);
- }
- }
- // Called when a SendAsync operation completes
- private static void ProcessSend(SocketAsyncEventArgs e)
- {
- if (e.SocketError == SocketError.Success)
- {
- // Sent "Hello World" to the server successfully
- //Read data sent from the server
- Socket sock = e.UserToken as Socket;
- bool willRaiseEvent = sock.ReceiveAsync(e);
- if (!willRaiseEvent)
- {
- ProcessReceive(e);
- }
- }
- else
- {
- throw new SocketException((int)e.SocketError);
- }
- }
- // Called when a ReceiveAsync operation completes
- // </summary>
- private static void ProcessReceive(SocketAsyncEventArgs e)
- {
- if (e.SocketError == SocketError.Success)
- {
- // Received data from server
- Debug.WriteLine("Data received: " + Encoding.UTF8.GetString(e.Buffer, 0, e.Count) + " (" + e.Count + ")");
- // Data has now been sent and received from the server.
- // Disconnect from the server
- Socket sock = e.UserToken as Socket;
- sock.Shutdown(SocketShutdown.Send);
- sock.Close();
- clientDone.Set();
- }
- else
- {
- throw new SocketException((int)e.SocketError);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement