Skip to content

Instantly share code, notes, and snippets.

@linusyang
Created March 11, 2018 09:42
Show Gist options
  • Save linusyang/e8165dc50ab5869e74c3497e60814fb1 to your computer and use it in GitHub Desktop.
Save linusyang/e8165dc50ab5869e74c3497e60814fb1 to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var e = new SocketAsyncEventArgs();
s.SetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)15, 1);
e.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("localhost"), 8000);
byte[] bytes = Encoding.ASCII.GetBytes("Aloha!");
e.SetBuffer(bytes, 0, bytes.Length);
e.Completed += (sender, arg) =>
{
if (e.SocketError == SocketError.Success)
{
Console.WriteLine("OK.");
}
else
{
Console.WriteLine("Error in Socket!");
}
};
s.ConnectAsync(e);
Thread.Sleep(2000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment