c# - 套接字通信程序测试

标签 c# .net testing sockets network-programming

我从使用简单的 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 上的另一台机器。

测试期间

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

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

最佳答案

udpSender.Flush?

关于c# - 套接字通信程序测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/606213/

相关文章:

asp.net - 测试使用成员资格的 asp.net 站点?

c# - 进程无法访问该文件,因为它正在被另一个进程在第二次调用时使用

c# - xpath-查找元素的多个顺序出现

.net - 你能在 mdbg 中看到评估堆栈吗?

java - 包 grails.plugins.springsecurity 在 jUnit 测试中不存在

testing - 在步骤定义类中包含非步骤方法是否可以接受?

c# - 这个 Composition 是否违反了面向对象编程的任何原则?

c# - 如何将 Microsoft Application Insights 与 NLog 结合使用(找不到目标 : 'ApplicationInsights' )

.net - 反编译DLL时缺少名称

.net - 为什么 WPF 支持绑定(bind)到对象的属性,但不支持绑定(bind)字段?