首页 > 代码库 > C# socket编程 使用udp实现单对单的连接对话

C# socket编程 使用udp实现单对单的连接对话

ipLocalPoint = new IPEndPoint(IPAddress.Parse("192.168.31.122"), 5000);

            //定义网络类型,数据连接类型和网络协议UDP 
            mySocket = new Socket
   (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //绑定网络地址 
            mySocket.Bind(ipLocalPoint);

            //得到客户机IP 
            IPAddress ip = IPAddress.Parse("192.168.31.127");
            int port = 5000;
            IPEndPoint ipep = new IPEndPoint(ip, port);
            RemotePoint = (EndPoint)(ipep);

            //启动一个新的线程,执行方法this.ReceiveHandle, 
            //以便在一个独立的进程中执行数据接收的操作 

            RunningFlag = true;
            Thread thread = new Thread(new ThreadStart(this.ReceiveHandle));
            thread.Start();

  //ReceiveHandle数据接收函数

  //接收数据处理线程 
            string msg;
            byte[] data = http://www.mamicode.com/new byte[1024];" : " + msg });

            }

  //发送udp数据包

    string msg;
            msg = tbSendMsg.Text;
            //发送UDP数据包 
            byte[] data = http://www.mamicode.com/Encoding.Default.GetBytes(msg);>

  //更新接收的消息到winform

     //接收数据显示 
            this.tbMsg.AppendText(msg + "\n");

  

C# socket编程 使用udp实现单对单的连接对话