1.直接调用微软socket对象处理
static void Main(string[] args)
{
try
{
IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
//在3721端口新建一个TcpListener对象
TcpListener listener = new TcpListener(ip, 3721);
listener.Start();
Console.WriteLine("started listening..");
//开始监听
while (true)
{
Socket s = listener.AcceptSocket();
string remote = s.RemoteEndPoint.ToString();
//允许js跨越访问
var bytes = System.Text.Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: * \r\n\r\nOK");
s.Send(bytes);
s.Shutdown(SocketShutdown.Send);
}
}
catch (System.Security.SecurityException)
{
Console.WriteLine("firewall says no no to application – application cries..");
}
catch (Exception)
{
Console.WriteLine("stoped listening..");
}
}