Socket

网络通信三要素:

IP地址、端口号、传输协议 TCP、UDP协议

Socket通信流程:

Server: 1.创建socket

msocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

2.绑定socket和端口号

                //创建一个IP对象
                IPAddress iPAddress = IPAddress.Parse(ip);
                //创建网络端口
                IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, port);
                //绑定端口到socket
                msocket.Bind(iPEndPoint);

3.监听该端口号

                //设置最大监听数量
                msocket.Listen(int.MaxValue);
                Console.WriteLine("监听{0}",msocket.LocalEndPoint);

4.监听客户端的链接

                //监听客户端的链接
                Thread thread = new Thread(MthreadStart);
                thread.Start();
        //监听客户端连接
        private void MthreadStart()
        {
            try
            {
                Socket client = msocket.Accept();
                client.Send(Encoding.UTF8.GetBytes("Welcome"));
                //接受客户端数据
                Thread thread = new Thread(ReceiveMessage);
            }
            catch (Exception)
            {

                throw;
            }
        }

5.接收客户端的信息

private void ReceiveMessage(Object socket)
        {
            //Socket clientSocket = socket as Socket;
            Socket clientSocket = (Socket)socket;
            while (true)
            {
                try
                {
                    int len = clientSocket.Receive(buffer);
                    Console.WriteLine("接收{0}客户端消息{1}",clientSocket.RemoteEndPoint, Encoding.UTF8.GetString(buffer,0,len));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Close();
                    throw;
                }
            }
        }

Client

class SocketClient
    {
        private string ip = string.Empty;
        private int port = 0;
        private byte[] buffer = new byte[1024*1024];
        private Socket socket;

        public SocketClient(string ip,int port)
        {
            this.ip = ip;
            this.port = port;
        }
        public void StartClient()
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress iPAddress = IPAddress.Parse(ip);
                IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, port);
                socket.Connect(iPEndPoint);
                Console.WriteLine("Connect To Server{0}", ip);
                int len = socket.Receive(buffer);
                Console.WriteLine("接收{0}客户端消息{1}", socket.RemoteEndPoint, Encoding.UTF8.GetString(buffer, 0, len));
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(2000);
                    string message = string.Format("Client Send message,time{0}", DateTime.Now);
                    socket.Send(Encoding.UTF8.GetBytes(message));
                    Console.WriteLine("!!!!!!!!!!!!向服务器发送{0}", message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
                throw;
            }
            Console.WriteLine("消息发送结束");
        }
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券