2010-09-08 81 views
0

我還沒有在C/C++中做一點工作,只是想知道是否有人可以幫助我使用Boost庫(Boost.Asio)將C#代碼移植到C++中。一個功能:如何使用Boost庫在C++中爲C#StreamProxyApplication函數創建模擬類型?

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++中創建其簡單\通用模擬器?

主要想法是將直播數據從一個廣播源傳輸到另一個廣播源。

回答

0

我不認爲這將是簡單的所有端口到C++。 C#中的一行代碼需要C++中的大量基礎結構,因爲在CLR中您可以輕鬆掌握整個.Net框架。我相信boost :: asio是一個比你需要的更低級別的界面。如果你必須在C++中做到這一點,那麼在ATL ServerWinHTTP中有東西可以證明是有用的。

有沒有什麼辦法可以將你需要的代碼封裝在一個小的C#程序集中,並使用COM interop從C++調用它?

+0

我很好奇接受答案的匿名遲到downvote。小心解釋你不喜歡的東西? – 2010-09-21 10:59:13

相關問題