【问题标题】:Cannot send dgram to external IP无法将 dgram 发送到外部 IP
【发布时间】:2012-04-09 11:10:59
【问题描述】:

我需要帮助:)

我做了很多谷歌搜索,没有找到答案,希望有人可以在这里帮助我:)

所以,我正在编写简单的 P2P 视频聊天客户端,它从文本框创建 IP 并连接到端口 5096 上的“其他”。所以,当我尝试连接到外部 IP 时,问题就开始了。听众不会收到包裹,我无法传输我的图像。有人可以帮帮我吗?

这是我的 UdpPictureSender 的代码:

class UDPPictureSender
    {
        bool closedSender = false;
        Socket sending_socket;
        IPAddress send_to_address;
        IPEndPoint sending_end_point;
        MemoryStream imageMemoryStream;


        public UDPPictureSender()
        { 
            initializeUdpPictureSender();

        }

        public void initializeUdpPictureSender()
        {
            sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            send_to_address = IPAddress.Parse(ConnectionSettings.ipAdress);
            sending_end_point = new IPEndPoint(send_to_address, 5096);
        }

        public void sendImage(Image image)
        {
            if (closedSender)
            {
                initializeUdpPictureSender();
            }

            imageMemoryStream = new MemoryStream();
            image.Save(imageMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            Byte[] bytes = imageMemoryStream.GetBuffer();
            sending_socket.SendTo(bytes, sending_end_point);
            imageMemoryStream.Flush();
        }

        public void closeConnection()
        {
            sending_socket.Close();
            send_to_address = null;
            sending_end_point = null;
            closedSender = true;
        }
    }

这是UDPListener的代码:

 class UDPListener
    {
        private const int listenPort = 5096;
        bool done = false;
        bool stoped = false; 

        UdpClient listener;
        IPEndPoint groupEP;
        Image received_data;
        byte [] receive_byte_array;
        MemoryStream currentMemoryStream;

        public UDPListener()
        {
            startupSettings();
        } 

        public  void startupSettings() 
        {
            groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            listener = new UdpClient(listenPort);
        }
        public Image listenForImages()
        {
            if (stoped)
            {
                startupSettings();
                stoped = false;
            }
            try
            {
                while (!done)
                {

                    receive_byte_array = listener.Receive(ref groupEP);
                    currentMemoryStream = new MemoryStream(receive_byte_array);
                    received_data = Image.FromStream(currentMemoryStream);
                    return received_data;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            listener.Close();
            return null;
        }

        public void closeConnection()
        {
            listener.Close();
            stoped = true;
        }
    } 

谁能帮帮我?

提前致谢

【问题讨论】:

    标签: c# udp client ip


    【解决方案1】:

    由于ConnectionSettings.ipAdress 不是有效的 IPv4 或 IPv6 地址,因此从 IPAddress.Parse 引发了该异常。

    您使用的不是有效的 IP 地址。也许你有一个嵌入式空间或类似的东西。

    【讨论】:

    • 我检查过,使用了 trim(),但仍然没有。
    • @hrza 有什么难理解的?您将无效地址(可能)传递给 IPAddress.Parse。 An invalid IP address was specified.检查你的代码,看看通过了什么。
    • 你能给我一个我应该解析的字符串的例子吗?
    • 我编辑了帖子并添加了一个 UDPListener,如果有人可以检查我是否做错了什么,我将非常感谢 :)))
    • @hrza 您的 UDPListener 代码无关紧要 - 您将无效的 IP 地址传递给 IPAddress.Parse。它应该是任何 IP 地址,例如“173.194.73.106”(www.google.com)
    【解决方案2】:

    在您的数据发送代码中,尝试发送数据几次。没有必要在一开始就在接收器端接收数据。它是 UDP 而不是 TCP,因此除非您有某种机制来确保交付,否则很少能确保交付。确保您有一个固定的缓冲区大小,并尝试以块而不是整体的形式发送图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2018-07-21
      • 2014-04-11
      • 2011-07-20
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多