Socket.BeginSendFile メソッド (String, AsyncCallback, Object)
アセンブリ: System (system.dll 内)

Public Function BeginSendFile ( _ fileName As String, _ callback As AsyncCallback, _ state As Object _ ) As IAsyncResult
Dim instance As Socket Dim fileName As String Dim callback As AsyncCallback Dim state As Object Dim returnValue As IAsyncResult returnValue = instance.BeginSendFile(fileName, callback, state)
public function BeginSendFile ( fileName : String, callback : AsyncCallback, state : Object ) : IAsyncResult
戻り値
非同期送信を表す IAsyncResult オブジェクト。


このオーバーロードは、接続されているソケットにファイル fileName を送信します。fileName がローカル ディレクトリに格納されている場合は、ファイルの名前だけで識別できます。それ以外の場合は、完全パスとファイルの名前を指定する必要があります。ワイルドカード ("..\\myfile.txt") および UNC 共有名 ("\\\\shared directory\\myfile.txt") がサポートされます。ファイルが見つからない場合は、例外 FileNotFoundException がスローされます。
このメソッドは、Windows Sockets 2 API にある TransmitFile 関数を使用します。TransmitFile 関数とフラグの詳細については、MSDN ライブラリで Windows Sockets のドキュメントを参照してください。
BeginSendFile メソッドは、Connect、BeginConnect、Accept、および BeginAccept の各メソッドで確立されたリモート ホストへの非同期送信操作を開始します。最初に Accept、BeginAccept、Connect、または BeginConnect を呼び出さないと、BeginSendFile は例外をスローします。BeginSendFile メソッドを呼び出すと、個別の実行スレッド内でファイルを送信できます。
操作を完了するには、AsyncCallback デリゲート パラメータによって呼び出されるコールバック メソッドを作成できます。これを行うには、少なくとも、通信に使用されている Socket オブジェクトが state パラメータに格納されている必要があります。他の情報がコールバックに必要な場合は、クラスまたは構造体を作成して Socket などの必要な情報を保持します。このカスタム オブジェクトのインスタンスを、state パラメータを使用して BeginSendFile メソッドに渡します。
コールバック メソッドは、EndSendFile メソッドを呼び出す必要があります。アプリケーションが BeginSendFile を呼び出すと、指定したコールバック メソッドが個別のスレッドを使用して実行され、EndSendFile は、Socket がファイル全体を送信するか例外をスローするまでブロックします。コールバック メソッドの記述に関する追加情報については、「Callback のサンプル」を参照してください。
BeginSendFile は、コネクション指向のプロトコルを想定していますが、Connect メソッドまたは BeginConnect メソッドを呼び出して既定のリモート ホストを確立しておけば、コネクションレスのプロトコルでも使用できます。コネクションレスのプロトコルを使用している場合は、ファイルのサイズが、基になるサービス プロバイダの最大パケット サイズを超えないことを確認する必要があります。最大パケット サイズを超える場合は、データグラムは送信されず、BeginSendFile によって SocketException 例外がスローされます。
![]() |
---|
SocketException 例外が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

非同期通信のソケットを作成して接続するコード例を次に示します。まず、ファイル "text.txt" がリモート ホストに対して非同期的に送信されます。コールバック デリゲートが EndSendFile を呼び出し、伝送を完了します。
public static void AsynchronousFileSend() { // Send a file to a remote device. // Establish the remote endpoint for the socket. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); // Wait for connect. connectDone.WaitOne(); // There is a text file test.txt in the root directory. string fileName = "C:\\test.txt"; // Send file fileName to the remote device. Console.WriteLine(fileName); client.BeginSendFile(fileName, new AsyncCallback(FileSendCallback), client); // Release the socket. client.Shutdown(SocketShutdown.Both); client.Close(); } private static void FileSendCallback(IAsyncResult ar) { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete sending the data to the remote device. client.EndSendFile(ar); sendDone.Set(); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket.BeginSendFile メソッド (String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object)
アセンブリ: System (system.dll 内)

Public Function BeginSendFile ( _ fileName As String, _ preBuffer As Byte(), _ postBuffer As Byte(), _ flags As TransmitFileOptions, _ callback As AsyncCallback, _ state As Object _ ) As IAsyncResult
Dim instance As Socket Dim fileName As String Dim preBuffer As Byte() Dim postBuffer As Byte() Dim flags As TransmitFileOptions Dim callback As AsyncCallback Dim state As Object Dim returnValue As IAsyncResult returnValue = instance.BeginSendFile(fileName, preBuffer, postBuffer, flags, callback, state)
public IAsyncResult BeginSendFile ( string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, Object state )
public: IAsyncResult^ BeginSendFile ( String^ fileName, array<unsigned char>^ preBuffer, array<unsigned char>^ postBuffer, TransmitFileOptions flags, AsyncCallback^ callback, Object^ state )
public IAsyncResult BeginSendFile ( String fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, Object state )
public function BeginSendFile ( fileName : String, preBuffer : byte[], postBuffer : byte[], flags : TransmitFileOptions, callback : AsyncCallback, state : Object ) : IAsyncResult
戻り値
非同期操作を表す IAsyncResult オブジェクト。


このオーバーロードには、送信するファイルの名前と TransmitFileOptions の値のビットごとの組み合わせが必要です。preBuffer パラメータには、ファイルの前に送信するデータを格納します。postBuffer には、ファイルの後に送信するデータを格納します。fileName がローカル ディレクトリに格納されている場合は、ファイルの名前だけで識別できます。それ以外の場合は、完全パスとファイルの名前を指定する必要があります。ワイルドカード ("..\\myfile.txt") および UNC 共有名 ("\\\\shared directory\\myfile.txt") がサポートされます。ファイルが見つからない場合は、例外 FileNotFoundException がスローされます。
flags パラメータによって、ファイル転送に関する追加情報と共に Windows Sockets サービス プロバイダが提供されます。このパラメータの使用方法の詳細については、TransmitFileOptions のトピックを参照してください。
このメソッドは、Windows Sockets 2 API にある TransmitFile 関数を使用します。TransmitFile 関数とフラグの詳細については、MSDN ライブラリで Windows Sockets のドキュメントを参照してください。
BeginSendFile メソッドは、Connect、BeginConnect、Accept、および BeginAccept の各メソッドで確立されたリモート ホストへの非同期送信操作を開始します。最初に Accept、BeginAccept、Connect、または BeginConnect を呼び出さないと、BeginSendFile は例外をスローします。BeginSendFile メソッドを呼び出すと、個別の実行スレッド内でファイルを送信できます。
操作を完了するには、AsyncCallback デリゲート パラメータによって呼び出されるコールバック メソッドを作成できます。これを行うには、少なくとも、通信に使用されている Socket オブジェクトが state パラメータに格納されている必要があります。他の情報がコールバックに必要な場合は、クラスまたは構造体を作成して Socket などの必要な情報を保持します。このカスタム オブジェクトのインスタンスを、state パラメータを使用して BeginSendFile メソッドに渡します。
コールバック メソッドは、EndSendFile メソッドを呼び出す必要があります。アプリケーションが BeginSendFile を呼び出すと、指定したコールバック メソッドが個別のスレッドを使用して実行され、EndSendFile は、Socket がファイル全体を送信するか例外をスローするまでブロックします。コールバック メソッドの記述に関する追加情報については、「Callback のサンプル」を参照してください。
BeginSendFile は、コネクション指向のプロトコルを想定していますが、Connect メソッドまたは BeginConnect メソッドを呼び出して既定のリモート ホストを確立しておけば、コネクションレスのプロトコルでも使用できます。コネクションレスのプロトコルを使用している場合は、ファイルのサイズが、基になるサービス プロバイダの最大パケット サイズを超えないことを確認する必要もあります。最大パケット サイズを超える場合は、データグラムは送信されず、BeginSendFile によって SocketException 例外がスローされます。
![]() |
---|
SocketException 例外が発生した場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

非同期通信のソケットを作成して接続し、リモート ホストにファイル "text.txt" の非同期的な送信を開始するコード例を次に示します。この例では、データの preBuffer および postBuffer が作成されて、ファイルと共に送信されます。また、既定の TransmitFileOptions 値が使用されます。コールバック デリゲートが EndSendFile を呼び出し、伝送を完了します。
public static void AsynchronousFileSendWithBuffers() { // Send a file asynchronously to the remote device. Send a buffer before the file and a buffer afterwards. // Establish the remote endpoint for the socket. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); // Wait for connect. connectDone.WaitOne(); // Send a file fileName to the remote device with preBuffer and postBuffer data. // Create the preBuffer data. string string1 = String.Format("This is text data that precedes the file.{0}", Environment.NewLine); byte[] preBuf = Encoding.ASCII.GetBytes(string1); // Create the postBuffer data. string string2 = String.Format("This is text data that will follow the file.{0}", Environment.NewLine); byte[] postBuf = Encoding.ASCII.GetBytes(string2); // There is a file test.txt in the root directory. string fileName = "C:\\test.txt"; //Send file fileName with buffers and default flags to the remote device. Console.WriteLine(fileName); client.BeginSendFile(fileName, preBuf, postBuf, 0, new AsyncCallback(AsynchronousFileSendCallback), client); // Release the socket. client.Shutdown(SocketShutdown.Both); client.Close(); } private static void AsynchronousFileSendCallback(IAsyncResult ar) { // Retrieve the socket from the state object. Socket client = (Socket) ar.AsyncState; // Complete sending the data to the remote device. client.EndSendFile(ar); sendDone.Set(); }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket.BeginSendFile メソッド

名前 | 説明 |
---|---|
Socket.BeginSendFile (String, AsyncCallback, Object) | UseDefaultWorkerThread フラグを使用して、接続されている Socket オブジェクトにファイル fileName を送信します。 |
Socket.BeginSendFile (String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object) | 接続された Socket オブジェクトに、ファイルとデータのバッファを非同期的に送信します。 |

Weblioに収録されているすべての辞書からSocket.BeginSendFileを検索する場合は、下記のリンクをクリックしてください。

- Socket.BeginSendFileのページへのリンク