如何在 C++ 中为此类 C# StreamProxyApplication 函数创建模拟使用Boost库?

发布于 2024-09-17 21:25:10 字数 2731 浏览 4 评论 0原文

我还没有在 C/C++ 中做过一点工作,只是想知道是否有人可以帮助我使用 Boost 库 (Boost.Asio) 将这个 .Net C# 代码移植到 C++ 中,

所以我有一个函数:

 private const int bufferSize = 8192;

/// <summary>
/// The entry point of the application.
/// </summary>
/// <param name="args">The input arguments of the application. The first argument should be a valid http address of the source server. The second argument should be a valid http address of the destination server.</param>
public static void Main(string[] args)
{
    // Check the amount of the arguments.
    if (args.Length < 2)
    {
        Console.Write("Not enough arguments. Expected 2, recieved " + args.Length + ".");
        return;
    }

    // Try to get the source server URI.
    Uri sourceUri;
    try
    {
        sourceUri = new Uri(args[0]);
    }
    catch (UriFormatException)
    {
        Console.Write("Invalid source server address.");
        return;
    }

    // Try to get the destination server URI.
    Uri destinationUri;
    try
    {
        destinationUri = new Uri(args[1]);
    }
    catch (UriFormatException)
    {
        Console.Write("Invalid destination server address.");
        return;
    }

    // Try to connect to the source server.
    Stream sourceStream;
    try
    {
        sourceStream = WebRequest.Create(sourceUri).GetResponse().GetResponseStream();
    }
    catch (Exception exception)
    {
        Console.Write("Cannot connect to the source server. Details: " + exception.Message);
        return;
    }

    // Try to connect to the destination server.
    NetworkStream destinationStream;
    try
    {
        destinationStream = new TcpClient(destinationUri.Host, destinationUri.Port).GetStream();
    }
    catch (Exception exception)
    {
        Console.Write("Cannot connect to the destination server. Details: " + exception.Message);
        return;
    }

    // Start redirecting the data.
    int read = 0;
    byte[] buffer = new byte[bufferSize];
    do
    {
        try
        {
            read = sourceStream.Read(buffer, 0, bufferSize);
        }
        catch (Exception exception)
        {
            Console.Write("An error occurred while receiving data from the source server. Details: " + exception.Message);
            return;
        }

        try
        {
            destinationStream.Write(buffer, 0, read);
        }
        catch (Exception exception)
        {
            Console.Write("An error occurred while sending data to the destination server. Details: " + exception.Message);
            return;
        }
    }
    while (read > 0);

    Console.Write("All data redirected.");
}

如何创建它在 C++ 中的简单\一般模拟?

这里的主要思想是将实时流数据从一个广播源传输到另一个广播源。

I haven't done work in C/C++ for a little bit and was just wondering if any one can help me with porting this .Net C# code into C++ using Boost library (Boost.Asio)

So I have one function:

 private const int bufferSize = 8192;

/// <summary>
/// The entry point of the application.
/// </summary>
/// <param name="args">The input arguments of the application. The first argument should be a valid http address of the source server. The second argument should be a valid http address of the destination server.</param>
public static void Main(string[] args)
{
    // Check the amount of the arguments.
    if (args.Length < 2)
    {
        Console.Write("Not enough arguments. Expected 2, recieved " + args.Length + ".");
        return;
    }

    // Try to get the source server URI.
    Uri sourceUri;
    try
    {
        sourceUri = new Uri(args[0]);
    }
    catch (UriFormatException)
    {
        Console.Write("Invalid source server address.");
        return;
    }

    // Try to get the destination server URI.
    Uri destinationUri;
    try
    {
        destinationUri = new Uri(args[1]);
    }
    catch (UriFormatException)
    {
        Console.Write("Invalid destination server address.");
        return;
    }

    // Try to connect to the source server.
    Stream sourceStream;
    try
    {
        sourceStream = WebRequest.Create(sourceUri).GetResponse().GetResponseStream();
    }
    catch (Exception exception)
    {
        Console.Write("Cannot connect to the source server. Details: " + exception.Message);
        return;
    }

    // Try to connect to the destination server.
    NetworkStream destinationStream;
    try
    {
        destinationStream = new TcpClient(destinationUri.Host, destinationUri.Port).GetStream();
    }
    catch (Exception exception)
    {
        Console.Write("Cannot connect to the destination server. Details: " + exception.Message);
        return;
    }

    // Start redirecting the data.
    int read = 0;
    byte[] buffer = new byte[bufferSize];
    do
    {
        try
        {
            read = sourceStream.Read(buffer, 0, bufferSize);
        }
        catch (Exception exception)
        {
            Console.Write("An error occurred while receiving data from the source server. Details: " + exception.Message);
            return;
        }

        try
        {
            destinationStream.Write(buffer, 0, read);
        }
        catch (Exception exception)
        {
            Console.Write("An error occurred while sending data to the destination server. Details: " + exception.Message);
            return;
        }
    }
    while (read > 0);

    Console.Write("All data redirected.");
}

How to create its simple\general analog in C++?

main Idea here is to transmit live streaming data from one broadcasticg source to another.

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

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

发布评论

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

评论(1

も让我眼熟你 2024-09-24 21:25:10

我认为移植到 C++ 一点也不简单。 C# 中的一行操作需要 C++ 中的一整套基础结构,因为您可以在 CLR 中轻松掌握整个 .Net 框架。我相信 boost::asio 是一个比您需要的更低级别的接口。如果您确实必须在 C++ 中执行此操作,那么 ATL ServerWinHTTP 这可能很有用。

有没有什么方法可以将此处所需的代码封装在一个小型 C# 程序集中,并使用 COM 互操作从 C++ 调用它?

I don't think this is going to be simple at all to port to C++. What takes one line in C# requires a whole bunch of infrastructure in C++, because you have the entire .Net framework at your fingertips in the CLR. I believe boost::asio is a lower-level interface than you need for this. If you do have to do this in C++ then there is stuff in ATL Server or WinHTTP which could prove useful.

Is there any way you can encapsulate the code from here that you need in a small C# assembly, and call into it from C++ using COM interop?

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