使用udp在android上接收数据

时间:2018-06-09 22:26:41

标签: android unity3d udp udpclient

我是创建服务器UDP 它非常适合计算机到计算机(发送和接收)。

但是在Android工作中只发送到服务器(android到计算机服务器) 为什么android不接收数据?

我的代码服务器在端口8888上运行

    private void OnIncomingData_From_Udp()
{
    Server.BeginReceive(OnReceive, null);
}
private void OnReceive(IAsyncResult ar)
{
    var ClientEp = new IPEndPoint(IPAddress.Any, 0);
    var ClientRequestData = Server.EndReceive(ar,ref ClientEp);
    var ClientRequest = Encoding.ASCII.GetString(ClientRequestData);

    Debug.Log("Recived " + ClientRequest + "from" + ClientEp.Address.ToString());
    UDP_Broadcasting(ClientEp);
    Server.BeginReceive(OnReceive, null);
}

代码客户端

    private void OnIncomingData_From_Udp()
{
    Client_udp = new UdpClient();
    Client_udp.EnableBroadcast = true;
    Client_udp.BeginReceive(OnReceive, null);
}
private void OnReceive(IAsyncResult ar)
{
    var ServerEp = new IPEndPoint(IPAddress.Any, 0);
    var ServerResponseData = Client_udp.EndReceive(ar,ref ServerEp);
    var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
    Debug.Log("ServerResponse=" + ServerResponse + " ServerEp=" + ServerEp.Address.ToString());
    Client_udp.BeginReceive(OnReceive, null);
}

发送客户端

        var RequestData = Encoding.ASCII.GetBytes("SomeRequestData_client");
    Client_udp.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));

0 个答案:

没有答案