如何将从另一个IP地址发送的UDP数据写入文本文件而不丢失丢失任何数据

发布于 2024-12-11 07:06:23 字数 986 浏览 0 评论 0原文

实际上,在 console.writeline 中,我从 UDPserver 获取从另一个 ipaddress 发送的所有数据。将传输的数据写入我的文本文件时,某些数据丢失了,但它没有写入,那么如何将该数据写入我的文本文件而不丢失

bool done = false;
private const int listenPort = 9050;
string strDestinationfileName = AppDomain.CurrentDomain.BaseDirectory + "Sample.txt";
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
FileStream fout = new FileStream(strDestinationfileName,   
                                 FileMode.OpenOrCreate,
                                 FileAccess.Write, FileShare.ReadWrite);
try
{
    while (!done)
    {
        Console.WriteLine("Waiting for broadcast");
        byte[] bytes = listener.Receive(ref groupEP);
        fout.Write(bytes, 0, bytes.Length);
        System.Threading.Thread.Sleep(100);

        Console.WriteLine("Received broadcast from {0} :\n {1}\n",
            groupEP.ToString(),
            Encoding.ASCII.GetString(bytes, 0, bytes.Length));
    }
}

Actually in console.writeline i am getting all the data from UDPserver which is send from another ipaddress . While writing that transfered data into my text file somes data is missing it doesn't write so how to write that data into my textfile without lost

bool done = false;
private const int listenPort = 9050;
string strDestinationfileName = AppDomain.CurrentDomain.BaseDirectory + "Sample.txt";
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
FileStream fout = new FileStream(strDestinationfileName,   
                                 FileMode.OpenOrCreate,
                                 FileAccess.Write, FileShare.ReadWrite);
try
{
    while (!done)
    {
        Console.WriteLine("Waiting for broadcast");
        byte[] bytes = listener.Receive(ref groupEP);
        fout.Write(bytes, 0, bytes.Length);
        System.Threading.Thread.Sleep(100);

        Console.WriteLine("Received broadcast from {0} :\n {1}\n",
            groupEP.ToString(),
            Encoding.ASCII.GetString(bytes, 0, bytes.Length));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-18 07:06:23

UDP 是一种基于数据包的不可靠协议。对于您想做的事情来说,这是最糟糕的选择。

UDP 数据包可能按顺序到达,但也可能根本不到达,或无序到达。如果它们太大,它们也可能会被默默地丢弃。

使用 TCP:它是一种基于流的协议,字节按照发送的顺序到达,并且会可靠地到达。为此,您可以使用 .NET 的内置 Socket 或 TCP 类。

(如果您确实需要使用 UDP,则需要发明自己的数据包编码方案和重试协议,以确保数据包到达,并以正确的顺序到达。这对于初学者来说是一项非常复杂的任务,基本上就是 TCP 的任务只需使用 TCP!)

编辑:对于 xixonia,因为显然指出一个关键缺陷是不值得的:您从未将 done 设置为 true,所以您的循环永远不会退出,以及数据的最后部分永远不会刷新到磁盘。

UDP is a packet-based, unreliable protocol. It is the worst choice possible for what you want to do.

UDP packets might arrive in order, but they might also not arrive at all, or arrive out-of-order. They might also be silently dropped if they are too large.

Use TCP: it's a stream-based protocol, where bytes arrive in the order sent, and will arrive reliably. You can use .NET's built-in Socket or TCP classes for this.

(If you do need to use UDP, you'll need to invent your own packet encoding scheme and retry protocol to ensure packets arrive, and arrive in the correct order. That is a very complex task for a beginner, and is basically what TCP does for you. Just use TCP!)

Edit: for xixonia, since apparently pointing out a crucial flaw isn't worthwhile: Your never set done to true, so your loop never exits, and the final parts of your data are never flushed to disk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文