【问题标题】:Testing of socket communication program套接字通信程序的测试
【发布时间】:2009-03-03 12:32:26
【问题描述】:

我开始使用简单的 UDPClient 程序发送一些数据的套接字编程。大代码sn-p如下:

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 上的另一台机器。

测试中

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

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

【问题讨论】:

    标签: c# .net testing sockets network-programming


    【解决方案1】:

    udpSender.Flush?

    【讨论】:

      【解决方案2】:

      我不是 C# 人,所以我不能对你的代码发表太多评论,但它看起来基本没问题。确保您发送到的 IP 地址正确解析到您的接收计算机。

      另外,查看 Windows 是否为您的 Internet 连接设置了防火墙,如果是,请尝试禁用防火墙。而且,我知道微软有一些关于“安全”代码的想法,这些想法在过去给我们带来了一些问题。我没有任何细节,但项目中可能存在使其无法访问网络的设置。

      【讨论】:

        【解决方案3】:

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

        udpListener = new UdpClient(12345)
        

        在 StartListeningUDP() 中使用

        udpListener = new UdpClient(new IPEndPoint(IPAddress.Any,12345))
        

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 2013-02-10
            • 1970-01-01
            • 2020-10-23
            • 1970-01-01
            • 2017-03-09
            • 2017-08-03
            • 1970-01-01
            • 2016-10-20
            • 2020-04-08
            相关资源
            最近更新 更多