c#Socket进阶(TcpClient,TcpListener,UdpClient)

应用程序可以通过 TCPClient、TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务。这些协议类建立在 System.Net.Sockets.Socket 类的基础之上,负责数据传送的细节。(也就是说TCPClient、TCPListener 和 UDPClient 类是用来简化Socket)

TcpClient 和 TcpListener 使用 NetworkStream 类表示网络。使用 GetStream 方法返回网络流,然后调用该流的 Read 和 Write 方法。NetworkStream 不拥有协议类的基础套接字,因此关闭它并不影响套接字。

UdpClient 类使用字节数组保存 UDP 数据文报。使用 Send 方法向网络发送数据,使用 Receive 方法接收传入的数据文报。

TCPListener服务器端

    class Program {
        static void Main(string[] args) {
            //1,TcpListener对socket进行了一层封装,这个类里面自己会去创建socket对象
            TcpListener listener = new TcpListener(IPAddress.Parse("192.168.2.124"),7788);

            //2,开始进行监听
            listener.Start();

            //3,等待客户端连接过来
            TcpClient client = listener.AcceptTcpClient();

            //4,取得客户端发送过来的数据
            NetworkStream stream = client.GetStream();//得到了一个网络流  从这个网络流可以取得客户端发送过来的数据

            byte[] data = new byte[1024];//创建一个数据的容器,用来承接数据

            while (true)
            {
                //0 表示从数组的哪个索引开始存放数据
                //1024表示最大读取的字节数
                int length = stream.Read(data, 0, 1024);//读取数据
                string message = Encoding.UTF8.GetString(data, 0, length);
                Console.WriteLine("收到了消息:" + message);
            }


            stream.Close();
            client.Close();
            listener.Stop();//停止监听
            Console.ReadKey();
        }
    }

TcpClient客户端

class Program {
        static void Main(string[] args) {
            //当我们创建tcpclient对象的时候,就会跟server去建立连接
            TcpClient client = new TcpClient("192.168.2.124",7788);

            NetworkStream stream = client.GetStream();//通过网络流进行数据的交换
            //read用来读取数据,write用来写入数据其实就是发送数据
            //利用一个死循环,重复向服务器端发送数据
            while (true)
            {
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);
                stream.Write(data, 0, data.Length);
            }

            stream.Close();
            client.Close();
            Console.ReadKey();
        }
    }

UDPClient服务器端

    class Program {
        static void Main(string[] args) {
            //创建udpclient 绑定ip跟端口号
            UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.0.112"),7788));

            while (true)
            {
                //接收数据
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = udpClient.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组,就是我们的数据
                string message = Encoding.UTF8.GetString(data);
                Console.WriteLine("收到了消息:" + message);
            }


            udpClient.Close();
            Console.ReadKey();
        }
    }

UDPClient客户端

    class Program {
        static void Main(string[] args) {
            //创建udpclient对象
            UdpClient client = new UdpClient();
            while (true)
            {
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);

                client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.168.0.112"), 7788));
            }

            client.Close();
            Console.ReadKey();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_41056203/article/details/81001211
今日推荐