zoukankan      html  css  js  c++  java
  • .NET 端口监听

    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
    Access-Control-Allow-Origin: * 
    
    OK");
                        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..");
                }
            }
    

      

  • 相关阅读:
    C#生成唯一码方法
    解剖常用软件程序都用什么语言开发
    Unity3D笔记七 GUILayout
    函数的递归
    函数
    函数的参数
    函数的返回值
    函数的定义
    文件处理
    集合
  • 原文地址:https://www.cnblogs.com/Adger/p/10848227.html
Copyright © 2011-2022 走看看