TcpListenerUDPListener

TcpListener

UDPListener 他们都在TCP UDP基础上的一层封装,封装了Socket

 服务端:

    static void Main(string[] args)
        {
            //1.创建TcpListener  里面包含Socker对象
            TcpListener tcpListener = new TcpListener(IPAddress.Parse("172.17.144.3"), 7788);
            //2 开始监听
            tcpListener.Start();
            //3 接受链接
            TcpClient client = tcpListener.AcceptTcpClient();
            //4获取 网络流 
            NetworkStream stream = client.GetStream();
            //5开始读取数据
            byte[] data = new byte[1024];
            while (true)
            {
                if (client.Client.Poll(10,SelectMode.SelectRead))
                {
                    break;
                }
                int length=stream.Read(data, 0, 1024);
                //转换消息  
                string mes=Encoding.UTF8.GetString(data, 0,length);
            
                Console.WriteLine("收到客服端:消息" + mes + "。总共:" + client.Available);
                    //关闭流 客服端 结束监听
            }
            stream.Close();
            client.Close();
            tcpListener.Stop();
        }

客服端

static void Main(string[] args)
        {
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(new IPEndPoint(IPAddress.Parse("172.17.144.3"), 7788));
            NetworkStream stream = tcpClient.GetStream();

            
            while (true)
            {
                string mes = Console.ReadLine();
                if (mes=="Q")
                {
                    stream.Close();
                    tcpClient.Close();
                    break;
                }
                byte[] data = Encoding.UTF8.GetBytes(mes);
                stream.Write(data, 0,data.Length);

            }
           
        }

 

 

获取本机Ip

    private static string GetIpAddress()
        {
            string hostName = Dns.GetHostName();   //获取本机名
            IPHostEntry localhost = Dns.GetHostByName(hostName);    //方法已过期,可以获取IPv4的地址
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            IPAddress localaddr = localhost.AddressList[0];

            return localaddr.ToString();
        }

UDP:  不需要建立链接 

只需要IP 和端口 就可以传输 数据

UDP也可以建立链接,但是在使用Send的时候就不能向指定的IP send的了,只能是建立链接的那一方。

 static void Main(string[] args)
        {
            UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("172.17.144.3"), 7788));

            IPEndPoint iPEnd = new IPEndPoint(IPAddress.Any, 0);

            byte[] data=udpClient.Receive(ref iPEnd);
            string mes = Encoding.UTF8.GetString(data);
            Console.WriteLine("从" + iPEnd.Address + "端口:" + iPEnd.Port + "收到了数据:" + mes);
        }
static void Main(string[] args)
        {
            UdpClient client = new UdpClient();
            IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("172.17.144.3"), 7788);
             string mes=Console.ReadLine();
            byte[] data = Encoding.UTF8.GetBytes(mes);
            client.Send(data, data.Length, iPEndPoint);
        }

 

使用UDPListener 

双方互相通信

class Program
    {
      static   UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("172.17.144.3"), 7788));

      static IPEndPoint iPEnd = new IPEndPoint(IPAddress.Any, 0);
       static  bool isReceive = false;
      static Thread t;
      static Thread t2;
        static void Main(string[] args)
        {

            t = new Thread(ReceMSG);
            t.Start();
            t2 = new Thread(RepMSG);
            t2.Start();
            

        }
        static void ReceMSG()
        {
            while (true)
            {
                 
                byte[] data = udpClient.Receive(ref iPEnd);
                string mes = Encoding.UTF8.GetString(data);
                Console.WriteLine("从" + iPEnd.Address + "端口:" + iPEnd.Port + "收到了数据:" + mes);
                isReceive = true; 
            }
        }
        static void RepMSG()
        {
            while (true)
            {
                if (isReceive)
                {
                    string mes = Console.ReadLine();
                    byte[] data = Encoding.UTF8.GetBytes(mes);
                    udpClient.Send(data, data.Length, iPEnd);
                }
                 
                
            }
        }
    }

 

   class Program
    {

        static UdpClient client = new UdpClient();
        static IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("172.17.144.3"), 7788);
        static Thread t1;
        static Thread t2;
        static bool isConnect=false;
        static void Main(string[] args)
        {
             
            t1 = new Thread(SendMsg);
            t1.Start();
            t2 = new Thread(ReceiveMsg);
            t2.Start();
             
        }
        static void ReceiveMsg()
        {
            while (true)
            {
                if (isConnect)
                {
                    byte[] data = client.Receive(ref iPEndPoint);
                    Console.WriteLine(Encoding.UTF8.GetString(data));
                }
               
            }
        }
        static void SendMsg()
        {
            while (true)
            {
                 
                string mes = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(mes);
                client.Send(data, data.Length,iPEndPoint);
                isConnect = true;  
            }
        }
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值