如何通过网络发送原始数据?

发布于 2024-08-26 15:30:58 字数 140 浏览 11 评论 0原文

我有一些数据存储在字节数组中。数据包含一个 IPv4 数据包(其中包含一个 UDP 数据包)。

我想使用 C#(首选)或 C++ 通过网络发送该数组原始数据。例如,我不想使用 C# 的 udp-client。

有谁知道如何执行此操作?

I have some data stored in a byte-array. The data contains an IPv4 packet (which contains a UDP packet).

I want to send this array raw over the network using C# (preferred) or C++. I don't want to use C#'s udp-client for example.

Does anyone know how to perform this?

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

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

发布评论

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

评论(4

青萝楚歌 2024-09-02 15:30:58

尝试原始套接字(指定 SOCK_RAW 作为套接字类型)。
您还将负责计算 IP 校验和。这可能有点烦人。

Try raw sockets (specify SOCK_RAW for the socket type).
You will be responsible for calculating the IP checksums as well. This can be a little annoying.

悟红尘 2024-09-02 15:30:58
using System.Net;
using System.Net.Sockets;

public class Test
{
    public void Send(byte[] rawData, IPEndPoint target)
    {
        // change what you pass to this constructor to your needs
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);

        try
        {
            s.Connect(target);
            s.Send(rawData);
        }
        catch(Exception ex)
        {
            // handle this exception
        }
    }
}
using System.Net;
using System.Net.Sockets;

public class Test
{
    public void Send(byte[] rawData, IPEndPoint target)
    {
        // change what you pass to this constructor to your needs
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);

        try
        {
            s.Connect(target);
            s.Send(rawData);
        }
        catch(Exception ex)
        {
            // handle this exception
        }
    }
}
明明#如月 2024-09-02 15:30:58

这是通过 NIC http://www.codeproject.com/ 发送原始数据的方法KB/IP/sendrawpacket.aspx 如上所述,Windows 限制原始套接字操作,您必须修改 NDIS 驱动程序才能发送您想要的任何内容。当然,您会遇到 Vista/7 上的数字驱动程序签名问题(可以通过测试模式暂时绕过)。

Here is a way to send raw data over NIC http://www.codeproject.com/KB/IP/sendrawpacket.aspx As mentioned above Windows is restricting raw socket operations, you have to modify NDIS driver to be able to send whatever you want. Of course you will then have a problem with digital driver signing on Vista/7 (can be temporary bypassed with test mode).

美羊羊 2024-09-02 15:30:58

当您有原始数据(即字节数组)并且想要通过网络发送它时,您需要某种编码:

  1. 如果您发送多个块(整个数组),接收者需要能够区分一个的结束和下一个的开始。
  2. 如果数据确实很大,最好将其分成较小的块(是的,数据包),以便与网络的其他用户良好地配合。
  3. 您需要知道客户端的数据没有错误,因为网络往往在您的错误时间变得不可靠。

编码解决了上面的第一点。
TCP是后两点的常规解决方案。

编码示例如下:

  • HTTP 将长度编码为 cr 分隔行,然后编码为纯二进制 blob。
  • 文本文件可以用 ctrl-z 分隔。
  • XML 可以简单地通过其标记语法来分隔。

When you have raw data (ie a byte-array) and you want to send it over a network, then you need some sort of encoding:

  1. If you send multiple blocks (whole arrays), the recipient needs to be able to differentiate between the end of one and the start of the next.
  2. If the data is really large its is better to split it into smaller blocks (yes, packets) to play well with other users of the network.
  3. You need to know that the data is error-free at the client as networks have the tendency to be unreliable at exactly the wrong time for you.

Encoding solves the first point above.
TCP is the conventional solution to the second two points.

Examples of encoding are:

  • HTTP encodes the length in cr delimited lines, then a pure binary blob.
  • Text Files could be ctrl-z delimited.
  • XML can be delimited simply by its syntax of tags.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文