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 is stored in the connection array. There is a socket in each connection for reuse
            //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()
        {
            //Server sticky packet processing

            //connection saves the current buff and size
            //then recursively reads all the data
            inside //recursively reads an int32 first to get the length. Then overwrite. Continue reading

        }
        /// <summary>
        /// Client asynchronous connection
        /// </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()
        {
            //In update keep looping. Parse everything added to msglist in BeginReceive
        }

        /// <summary>
        /// mu project uses
        /// </summary>
        public void muConnect()
        {
            //After the link, the information is continuously received in the update and then stored in the byte array. Then parse them out one by one
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326012905&siteId=291194637