-1

sendfile C# 非同期ソケットの機能を (サーバーとして) 使用して、このファイルを C++ ネイティブ コード クライアントで受信しようとしています。これを使用して C# サーバーからクライアントでファイルを更新するため、要件により webServices を使用できません。これはファイルを送信するために使用しています

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);

// There is a text file test.txt located in the root directory.
string fileName = "C:\\test.txt";

// Send file fileName to remote device
Console.WriteLine("Sending {0} to the host.", fileName);
client.SendFile(fileName);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
4

1 に答える 1

0

そして...あなたの質問は正確には何ですか?

私はC#にはあまり強くありませんが、C++では、受信データをselectで永続的にリッスンするスレッドをインスタンス化し(「非同期」にもします)、同期メソッドで処理します(必要に応じてCriticalSectionを使用します) )。

これを明確にするために、これはサーバー/クライアント通信の一般的な作業方法です。

サーバー: ソケットをインスタンス化し、それ自体をポートにバインドし、着信接続をリッスンします。接続が入ると、ハンドシェイクを実行し、新しいクライアントをクライアントのコレクションに追加します。着信要求に応答する/すべてのクライアントに定期的な更新を送信する

クライアント: ソケットをインスタンス化し、'connect' を使用してサーバーに接続し、プロトコルに従って通信を開始します。

編集:念のため言っておきますが、これは誤解による些細な問題ではありません。クライアントでファイルを保存する方法を意味していますか?

次のコードを使用することをお勧めします: (スレッドで select を使用していると仮定します)

fd_set fds;             //Watchlist for Sockets
int RecvBytes;
char buf[1024];
FILE *fp = fopen("FileGoesHere", "wb");
while(true) {
    FD_ZERO(&fds);      //Reinitializes the Watchlist
    FD_SET(sock, &fds); //Adds the connected socket to the watchlist
    select(sock + 1, &fds, NULL, NULL, NULL);    //Blocks, until traffic is detected
    //Only one socket is in the list, so checking with 'FD_ISSET' is unnecessary
    RecvBytes = recv(sock, buf, 1024, 0);  //Receives up to 1024 Bytes into 'buf'
    if(RecvBytes == 0) break;   //Connection severed, ending the loop.
    fwrite(buf, RecvBytes, fp); //Writes the received bytes into a file
}
fclose(fp);                     //Closes the file, transmission complete.
于 2012-07-19T08:23:52.587 に答える