• asp.net小记之——线程


    2012年10月26日,星期五
     
    线程池*
    TCP和UDP的区别*
     
    手动创建线程:
        Thread thread = new Thread(new Thread.Start(DemoMethod));
        thread.IsBackground = true;
        thread.Name = "xqt";
        thread.Priority = ThreadPriority.Highest;//设置线程的级别
        thread.Start();

        static void DemoMethod()
        {
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
        }
     
    Socket通信的模型:
        服务器端:
            -创建Socket:寻址方式,传输的方式,协议————只负责接听
            -绑定一个Host地址和端口:端口同一时间只能被一个进程占用;
            -开启侦听:Linsten();
            -接收用户的请求:Accept();————创建一个新的通信套接字;
            -客户端关闭Socket时,就关闭了通信Socket;
        客户端:
            -创建Socket
            -连接到服务器端——指定IP和端口
            -与服务器端新创建的套接字进行通信
            -可进行Send和Recive了
            -客户端可调用SocketShutdown关闭Socket
            -直接Socket.Close()关闭时,服务器端会报异常,除非手动关闭;
     
    //socket通信模型,服务器端
                - Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                - IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
                - IPEndPoint endPoint = new IPEndPoint(ipAddress, 8888);
                - socket.Bind(endPoint);
                - socket.Listen(10);
    //客户端:
               - Socket clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
               - IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8888);
               - clientSocket.Connect(endPoint);
     
    SocketHelper:
    public class SocketHelper
        {
            //创建服务器端socket
            //创建一个监听一个ip和端口的socket
            //传递两个参数:ip,端口号
            public static Socket CreateListenSocket(string ip,int port)
            {
                if(string.IsNullOrEmpty(ip) || port>65535 || port < 0)
                {
                    return null;
                }
                Socket socket = new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);
                IPAddress ipAddress = IPAddress.Parse(ip);
                IPEndPoint endpoint = new IPEndPoint(ipAddress,port);
                socket.Bind(endpoint);
                socket.Listen(10);
                return socket;
            }
            //客户端连接服务器
            public static Socket ConnectServer(string ip,int port)
            {
                Socket clientSocket = null;
                try
                {
                    clientSocket = new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);
                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip),port);
                    clientSocket.Connect(endPoint);
                }
                finally
                {
                    //记录日志
                }
                return clientSocket;
            }
        }
     
     



  • 相关阅读:
    剑指offer:二分查找找到旋转数组中的最小值
    强制索引
    剑指offer:青蛙跳台阶
    剑指offer:求和
    序列化和反序列化
    装饰器、生成器
    Python函数(一)
    【转】C# 中的委托和事件
    【读书笔记】备忘录模式翻译成C++了
    【学习笔记】Android 调试桥
  • 原文地址:https://www.cnblogs.com/xuquantong/p/2745502.html
Copyright © 2020-2023  润新知