1

我从一个简单的 UDPClient 程序开始使用套接字编程来发送一些数据。大代码片段如下:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class ShowIP
{
    public static void Main(string[] args)
    {
        string name = Dns.GetHostName();
        //name = "GSL1460";
        name = "GSL1296";
        try
        {
            IPAddress[] addrs = Dns.GetHostEntry(name).AddressList;
            foreach (IPAddress addr in addrs)
                Console.WriteLine("{0}/{1}", name, addr);

            Console.WriteLine("Started listening");
            Thread listenerThread = new Thread(new ThreadStart(StartListeningUDP));
            listenerThread.Start();

            Console.WriteLine("Started sending");
            for (int counter = 0; counter <= 3; counter++)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Sending {0} time", counter.ToString());
                StartSendingUDP(addrs[0]);
            }
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    private static void StartListeningUDP()
    {
        UdpClient udpListener = null;
        IPEndPoint nwPoint = new IPEndPoint(IPAddress.Any, 12345);

        while (true)
        {
            try
            {
                udpListener = new UdpClient(12345);
                Console.WriteLine("Waiting to receive");
                Byte[] receivedBytes = udpListener.Receive(ref nwPoint);
                string receivedData = Encoding.ASCII.GetString(receivedBytes);

                Console.WriteLine("Data received : " + receivedData);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                udpListener.Close();
            }
        }
    }

    private static void StartSendingUDP(IPAddress clientAddress)
    {
        UdpClient udpSender = new UdpClient();
        try
        {
            Byte[] sendBytes = Encoding.ASCII.GetBytes("Say HI to Papa...");

            Console.WriteLine("Data Sent : Say HI to Papa...");
            udpSender.Send(sendBytes, sendBytes.Length, new IPEndPoint(clientAddress, 12345));
        }
        finally
        {
            udpSender.Close();
        }

    }
}

该示例在本地计算机上运行良好,但无法将数据发送到 Intranet 上的另一台计算机。

在测试期间

  • 我正在取消注释适当的代码以将数据发送到他的机器
  • 我正在他的机器上运行接收器位
  • 已检查他的机器上是否打开了所需的端口

我错过了什么吗?请建议。

4

4 回答 4

1

udpSender.Flush?

于 2009-03-03T20:13:19.300 回答
0

UDP 侦听器可能仅在本地主机上侦听。你可以尝试更换

udpListener = new UdpClient(12345)

在 StartListeningUDP() 中

udpListener = new UdpClient(new IPEndPoint(IPAddress.Any,12345))
于 2009-03-04T14:36:11.380 回答
0

I'm not a C# person, so I can't comment too much on your code, but it looks basically okay. Make sure that the IP address you're sending to is being resolved correctly to your receiving machine.

Also, see if Windows has firewalled your internet connection, and try disabling the firewall if so. And, I know that Microsoft has some ideas about "safe" code that have caused us some problems in the past. I don't have any specifics, but there might be settings in the project that keep it from being able to access the network.

于 2009-03-03T14:00:33.440 回答
0

如果不做一些事情,你就不能真正通过互联网发送 UDP。你会在路上得到太多的udp过滤器。即使您将禁用防火墙,您的路由器/提供商调制解调器也可以设置为阻止它。否则 - 您的提供商服务器将阻止它。所以实际上你必须确保这个端口对 UDP 开放,就像在你的本地主机上它不会工作,除非你在防火墙中打开这个端口和/或安装环回适配器

于 2009-10-26T19:35:45.033 回答