C#udp套接字没有长途连接(vps到客户端)

时间:2015-07-23 18:51:35

标签: c# sockets udp receiver

我在C#上有一个udp客户端服务器源,现在我遇到了一个问题:我在localhost上测试了我的源代码,但它没有收到长途连接,例如VPS到客户端。

客户端:

private static void StartListener()
{
        bool done = false;

        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

        try
        {
            while (!done)
            {
                byte[] bytes = listener.Receive(ref groupEP);
                Thread.Sleep(100);

                byte[] dcbufresponse = new byte[512];
                dcbufresponse = Encoding.ASCII.GetBytes("0xc00901");
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipaddresssv), 9052);
                s.SendTo(dcbufresponse, dcbufresponse.Length, SocketFlags.None, ipep);
                done = true;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }

服务器:

private static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private static IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipnow), 9052);
                                    s.SendTo(dcbuf, dcbuf.Length, SocketFlags.None, ipep);
                                    Console.WriteLine("sended");
                                    sended.Add(ipnow);
                                    try
                                    {
                                        listener.Client.ReceiveTimeout = 5000;
                                        byte[] bytes = listener.Receive(ref groupEP);
                                        string dcresponse = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
                                        Console.WriteLine(dcresponse);
                                    }

问题是:客户端没有收到任何内容,客户端收到后无法发送给服务器......

编辑: 我的服务有两个IP地址! 我认为我必须将我的两个IP地址绑定到我的udp客户端: - ? 你觉得它的作品吗?

0 个答案:

没有答案