【问题标题】:UDP Listener using sockets generating type errorUDP 侦听器使用套接字生成类型错误
【发布时间】:2012-09-06 00:28:21
【问题描述】:

一般来说,我是 Sockets 和 C# 的新手,并且很难实现一个简单的 upd 侦听器函数。我花了很多时间在网上搜索,但未能成功地整合在线众多示例中的任何一个。因此,任何建议、链接、示例将不胜感激!

此时,我有一个第三方应用程序通过端口 6600 广播一条通用 UPD 消息,其中包含有关应用程序服务器位置(服务器名称、IP 地址等)的信息。我想设计我的侦听器客户端应用程序来捕获 UPD 广播并生成可用于未来处理的可用服务器集合。

我遇到的问题是,当我尝试使用 listener.Listen(0) 创建侦听器时,如果失败并生成一般类型错误。如果我尝试使用 UdpClient 类,我的应用程序将挂起并且永远不会返回任何数据。下面列出了这两个示例的代码:

namespace UDPListener
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            listener.Bind(new IPEndPoint(IPAddress.Any, 6600));
            listener.Listen(6);
            Socket socket = listener.Accept();
            Stream netStream = new NetworkStream(socket);
            StreamReader reader = new StreamReader(netStream);

            string result = reader.ReadToEnd();
            Console.WriteLine(result);
            socket.Close();
            listener.Close();

        }
    }
}

还有 UdpClient:

private void IdentifyServer()
    {
        //Creates a UdpClient for reading incoming data.
        UdpClient receivingUdpClient = new UdpClient(6600);

        //Creates an IPEndPoint to record the IP Address and port number of the sender.  
        // The IPEndPoint will allow you to read datagrams sent from any source.
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        try
        {

            // Blocks until a message returns on this socket from a remote host.
            Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

            string returnData = Encoding.ASCII.GetString(receiveBytes);

            Output.Text = ("This is the message you received " +
                                        returnData.ToString());
            Output.Text = ("This message was sent from " +
                                        RemoteIpEndPoint.Address.ToString() +
                                        " on their port number " +
                                        RemoteIpEndPoint.Port.ToString());
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }

【问题讨论】:

  • 您将 UDP 与 TCP 混淆了。简而言之,UDP 基于 Bind、ReceiveFrom 和 SendTo,而 TCP 使用 Bind、Listen、Accept、Connect、Send and Receive 和 Streams 等。看看 MSDN 和那里的示例以了解 UDP 的工作原理。
  • 您使用 UDP 协议与 TCP 协议有什么具体原因吗?
  • 感谢您的帖子!我想我可能错误地实现了 UPD,但 msdn 上的示例并没有太大帮助…… UdpClient 示例与 msdn 网站上提供的示例几乎完全相同。它的问题是它会停止并挂起并且从不捕获任何数据。
  • @Dan 广播服务器(第三方应用程序)正在使用 UDP。我只是想捕获消息数据。
  • 好吧,我也很怀疑。不知道你的经验水平,我问如果 TCP 会是你更好的选择。祝你好运。

标签: c# sockets udp listener udpclient


【解决方案1】:

dtb 绝对是写的!经过大量研究和朋友的帮助后,我意识到我真正在寻找的是多播的解决方案。我将包括下面的链接。

@dtb,感谢您帮助我指明了正确的方向!

http://www.codeproject.com/Articles/1705/IP-Multicasting-in-C

http://codeidol.com/csharp/csharp-network/IP-Multicasting/Csharp-IP-Multicast-Support/

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2020-03-25
    相关资源
    最近更新 更多