socket tcp

网络Socket编程 tcp联网


        /// <summary>
        /// 服务端同步链接
        /// </summary>
        public void serverConnect()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);
            IPAddress adress = IPAddress.Parse("127.0.0.1");
            IPEndPoint endPoint = new IPEndPoint(adress, 10);
            socket.Bind(endPoint);
            socket.Listen(0);
            while (true)
            {
                Socket list = socket.Accept();
                byte[] buff = new byte[1024];
                int count = list.Receive(buff);
                string str = Encoding.UTF8.GetString(buff, 0, count);
                byte[] sBuff = new byte[1024];
                list.Send(Encoding.UTF8.GetBytes(str));
            }
        }
        /// <summary>
        /// 客户端同步链接
        /// </summary>
        public void clientConnect()
        {        
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);
            IPAddress adress = IPAddress.Parse("127.0.0.1");
            IPEndPoint endPoint = new IPEndPoint(adress, 10);
            socket.Connect(adress, 10);
            //socket.Send()
            //socket.Receive()
        }

        /// <summary>
        /// 服务端异步链接
        /// </summary>
        private Socket serverSocket;
        public void serverConnectAsyn()
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);
            IPAddress adress = IPAddress.Parse("127.0.0.1");
            IPEndPoint endPoint = new IPEndPoint(adress, 10);
            serverSocket.Bind(endPoint);
            serverSocket.Listen(0);
            serverSocket.BeginAccept(servercb, null);
        }
        public void servercb(IAsyncResult ar)
        {
            Socket con = serverSocket.EndAccept(ar);
            //sock 存到connection 数组。每个connection里面有一个socket 重复使用
            //con.BeginReceive(con.ReceiveBufferSize, SocketFlags.None, cb, con);
            serverSocket.BeginAccept(servercb, null);
        }
        public void cb(IAsyncResult ar)
        {
            //connect connect = (connect)(ar.AsyncState);
            //connect.socket
            Socket socket = null;
            int count = socket.EndReceive(ar);
            if (count <= 0)
                socket.Close();
            //socket.Send()
            //socket.BeginReceive(cb, null);
        }
        public void killbag()
        {
            //服务端粘包处理

            //connection 存了当前的buff 和 大小
            //然后递归把里面的数据都读出来
            //递归里面 先读一个int32 得出长度。然后覆盖。继续读

        }
        /// <summary>
        /// 客户端异步链接
        /// </summary>
        public void clientConnectAsyn()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);
            IPAddress adress = IPAddress.Parse("127.0.0.1");
            IPEndPoint endPoint = new IPEndPoint(adress, 10);
            socket.Connect(adress, 10);
            //socket.Send()
            //socket.Receive()
        
            socket.BeginReceive(endBuff, 0, 1024, SocketFlags.None, endRe, null);
        }
        byte[] endBuff = new byte[1024];
        public void endRe(IAsyncResult ar)
        {
            int count = socket.EndReceive(ar);
            Encoding.UTF8.GetString(endBuff, 0, count);
            socket.BeginReceive(endBuff, 0, 1024, SocketFlags.None, endRe, null);
        }

        public void handle()
        {
            //在update里面不断循环。把 在BeginReceive 加入msglist的东西都解析
        }

        /// <summary>
        /// mu 项目使用
        /// </summary>
        public void muConnect()
        {
            //链接之后在update里面不断receive信息然后存到byte数组。然后一个一个解析出来
        }

猜你喜欢

转载自blog.csdn.net/a133900029/article/details/80184265