udp发送和接收消息,服务端

1、使用UDPClient发送(send)和接收(recive):

UdpClient ServerSender;

    private int sendUDP(Message msg) {


        int responcecode = 0;
        //动态获取Ip地址
        IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
        IPAddress ip = ihe.AddressList[0];


        //绑定本地端口
        ServerSender = new UdpClient(ip.ToString(), PORT);
        
        try
        {
            byte[] bytes = Encoding.UTF8.GetBytes(msg.text);
            
            ServerSender.Send(bytes, bytes.Length);//只负责发送,接收的需要侦听这个ip端口
            //ServerSender.Send(bytes, bytes.Length, new IPEndPoint(IPAddress.Parse("192.168.1.121"), 2017));//三个参数的表示发送到指定端口


            Thread thread = new Thread(reciveMsgInThread);
            thread.Start();
            responcecode = 1;
        }
        catch (Exception ee)
        {
            ee.ToString();
        }
        


        return responcecode;

    }


private void reciveMsgInThread() {


        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        ServerSender.Client.ReceiveTimeout = 5000;//5秒超时

try{
        while (true)
        {
            Byte[] receiveBytes = ServerSender.Receive(ref RemoteIpEndPoint);
            //当5秒后没有数据接收,结束
            if (receiveBytes.Length!=null)
            {
                string returnData = Encoding.UTF8.GetString(receiveBytes);
                System.Diagnostics.Debug.Write("YuSong收到客户机:" + RemoteIpEndPoint.Address.ToString() + ",的回应:" + returnData + "\n");
            }
            else
            {
                break;
            }
        }

}

catch(Execption ex)

{

System.Diagnostics.Debug.Write(ex.ToString());

}finally {


            //循环结束后关闭 
            ServerSender.Close();
        }
    }



2、使用Socket发送(sendto)和接收(recivefrom):

udp是无连接的,把包丢出去谁要谁拿。而Socket绑定地址进行udp通信,所以是有连接的,首先要连接到远程主机,如果是使用send()发送首先要开端口(打洞)先调用connect(IPEndPoint),而使用sendTo(数据,数据长度,IPEndPoint);


    Socket server;
    private void useUDPbySocket(Message message)
    {
        IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
        IPAddress ip = ihe.AddressList[0];
        server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        server.Bind(new IPEndPoint(IPAddress.Parse(ip.ToString()), PORT));//绑定端口号和IP


        Thread recivethread = new Thread(ReciveMsg);//开启接收消息线程
        recivethread .Start();
        Thread sendthread = new Thread(sendMsg);//开启发送消息线程
        sendthread .Start();
    }


    void sendMsg()
    {
        int i = 5;
        while (i>0) {
            byte[] bytes = Encoding.UTF8.GetBytes("The Wearing jeans is my lover");
            server.Connect(IPAddress.Parse("127.0.0.1"), 2222);//指定远程主机,端口
            server.Send(bytes);
            i--;
        }
    }


    void ReciveMsg()
    {
        while (true)
        {
            EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
            byte[] buffer = new byte[1024];
            int length = server.ReceiveFrom(buffer, ref point);//接收数据报
            string message = Encoding.UTF8.GetString(buffer, 0, length);
            System.Diagnostics.Debug.Write("YuSong收到客户机:" + point.ToString() + ",的回应:" + message + "\n");
        }
    }




你可能感兴趣的:(asp.net)