Socket.SendFile Socket.SendFile Socket.SendFile Socket.SendFile Method

定义

将文件和可选数据异步发送到连接的 SocketSends a file and optional data synchronously to a connected Socket.

重载

SendFile(String) SendFile(String) SendFile(String) SendFile(String)

使用 Socket 传输标志,将文件 fileName 发送到连接的 UseDefaultWorkerThread 对象。Sends the file fileName to a connected Socket object with the UseDefaultWorkerThread transmit flag.

SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions)

通过指定的 TransmitFileOptions 值,将文件 fileName 和数据缓冲区发送到连接的 Socket 对象。Sends the file fileName and buffers of data to a connected Socket object using the specified TransmitFileOptions value.

SendFile(String) SendFile(String) SendFile(String) SendFile(String)

使用 Socket 传输标志,将文件 fileName 发送到连接的 UseDefaultWorkerThread 对象。Sends the file fileName to a connected Socket object with the UseDefaultWorkerThread transmit flag.

public:
 void SendFile(System::String ^ fileName);
public void SendFile (string fileName);
member this.SendFile : string -> unit
Public Sub SendFile (fileName As String)

参数

fileName
String String String String

一个 String,它包含要发送的文件的路径和名称。A String that contains the path and name of the file to be sent. 此参数可以为 nullThis parameter can be null.

异常

套接字未连接到远程主机。The socket is not connected to a remote host.

Socket 对象未处于阻止模式,无法接受此同步调用。The Socket object is not in blocking mode and cannot accept this synchronous call.

未找到文件 fileNameThe file fileName was not found.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例创建并连接套接字,然后将文件发送到远程主机。The following code example creates and connects a socket and then sends a file to the remote host. 文件"test.txt"位于本地计算机的根目录中。The file "test.txt" is located in the root directory of the local machine.

// Establish the local endpoint for the socket.
IPHostEntry^ ipHost = Dns::GetHostEntry( Dns::GetHostName() );
IPAddress^ ipAddr = ipHost->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddr,11000 );

// Create a TCP socket.
Socket^ client = gcnew 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();
// Establish the local endpoint for the socket.
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();

注解

此重载将该文件发送fileName到连接的套接字。This overload sends the file fileName to the connected socket. flags参数默认为UseDefaultWorkerThread(0),并preBufferpostBuffer参数默认为nullThe flags parameter defaults to UseDefaultWorkerThread (0), and the preBuffer and postBuffer parameters default to null. 如果fileName是在本地目录中,它可能会标识只是该文件的名称; 否则,必须指定完整路径和文件的名称。If fileName is in the local directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. 通配符 ("...\\myfile.txt") 和 UNC 共享名 ("\\\\shared directory\\myfile.txt") 支持。Wildcards ("..\\myfile.txt") and UNC share names ("\\\\shared directory\\myfile.txt") are supported. 如果未找到文件,该异常FileNotFoundException引发。If the file is not found, the exception FileNotFoundException is thrown.

此方法使用TransmitFileWindows Sockets 2 API 中找到函数。This method uses the TransmitFile function found in the Windows Sockets 2 API. 有关详细信息TransmitFile函数和它的标志,请参阅Windows 套接字文档。For more information about the TransmitFile function and its flags, see the Windows Sockets documentation.

SendFile 以同步方式将文件发送到远程主机中指定ConnectAccept方法。SendFile synchronously sends a file to the remote host specified in the Connect or Accept method. SendFile 可以使用面向连接的两个和无连接协议。SendFile can be used for both connection-oriented and for connectionless protocols.

如果您使用的是无连接协议,则必须调用Connect之前调用此方法,否则SendFile引发SocketException异常。If you are using a connectionless protocol, you must call Connect before calling this method, otherwise SendFile throws a SocketException exception. 如果使用的是面向连接的协议,则必须使用Connect建立远程主机连接或使用Accept以接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection or use Accept to accept an incoming connection.

如果使用面向连接的协议SendFile阻止,直到该文件发送。If you are using a connection-oriented protocol, SendFile blocks until the file is sent. 在阻止模式下,SendFile发送整个文件之前,可能会成功完成。In nonblocking mode, SendFile may complete successfully before the entire file has been sent. 则您发送的数据将立即显示在网络不能保证。There is no guarantee that the data you send will appear on the network immediately. 若要增加网络效率,基础系统可能会延迟传输,直到收集了大量传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. 成功完成SendFile方法意味着的基础系统已为你的数据的网络发送的缓冲区的空间。A successful completion of the SendFile method means that the underlying system has had room to buffer your data for a network send.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions) SendFile(String, Byte[], Byte[], TransmitFileOptions)

通过指定的 TransmitFileOptions 值,将文件 fileName 和数据缓冲区发送到连接的 Socket 对象。Sends the file fileName and buffers of data to a connected Socket object using the specified TransmitFileOptions value.

public:
 void SendFile(System::String ^ fileName, cli::array <System::Byte> ^ preBuffer, cli::array <System::Byte> ^ postBuffer, System::Net::Sockets::TransmitFileOptions flags);
public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, System.Net.Sockets.TransmitFileOptions flags);
member this.SendFile : string * byte[] * byte[] * System.Net.Sockets.TransmitFileOptions -> unit
Public Sub SendFile (fileName As String, preBuffer As Byte(), postBuffer As Byte(), flags As TransmitFileOptions)

参数

fileName
String String String String

一个 String,它包含要发送的文件的路径和名称。A String that contains the path and name of the file to be sent. 此参数可以为 nullThis parameter can be null.

preBuffer
Byte[]

一个 Byte 数组,它包含发送文件前要发送的数据。A Byte array that contains data to be sent before the file is sent. 此参数可以为 nullThis parameter can be null.

postBuffer
Byte[]

一个 Byte 数组,它包含发送文件后要发送的数据。A Byte array that contains data to be sent after the file is sent. 此参数可以为 nullThis parameter can be null.

异常

操作系统不是 Windows NT 或更高版本。The operating system is not Windows NT or later.

- 或 --or- 套接字未连接到远程主机。The socket is not connected to a remote host.

Socket 对象未处于阻止模式,无法接受此同步调用。The Socket object is not in blocking mode and cannot accept this synchronous call.

未找到文件 fileNameThe file fileName was not found.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例创建并连接套接字。The following code example creates and connects a socket. 文件"test.txt"位于本地计算机的根目录中。The file "test.txt" is located in the root directory of the local machine. 在此示例中,我们创建缓冲区和数据后,并将其与文件一起发送到远程主机。In this example, we create a prebuffer and postbuffer of data and send them to the remote host with the file. 默认值TransmitFileOptions使用。The default TransmitFileOptions are used.

// Establish the local endpoint for the socket.
IPHostEntry^ ipHost = Dns::GetHostEntry( Dns::GetHostName() );
IPAddress^ ipAddr = ipHost->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddr,11000 );

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

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

// Send file fileName to the remote host with preBuffer and postBuffer data.
// There is a text file test.txt located in the root directory.
String^ fileName = "C:\\test.txt";

// Create the preBuffer data.
String^ string1 = String::Format( "This is text data that precedes the file.{0}", Environment::NewLine );
array<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 );
array<Byte>^postBuf = Encoding::ASCII->GetBytes( string2 );

//Send file fileName with buffers and default flags to the remote device.
Console::WriteLine( "Sending {0} with buffers to the host.{1}", fileName, Environment::NewLine );
client->SendFile( fileName, preBuf, postBuf, TransmitFileOptions::UseDefaultWorkerThread );

// Release the socket.
client->Shutdown( SocketShutdown::Both );
client->Close();
// Establish the local endpoint for the socket.
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);

// Send file fileName to the remote host with preBuffer and postBuffer data.
// There is a text file test.txt located in the root directory.
string fileName = "C:\\test.txt";

// 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);

//Send file fileName with buffers and default flags to the remote device.
Console.WriteLine("Sending {0} with buffers to the host.{1}", fileName, Environment.NewLine);
client.SendFile(fileName, preBuf, postBuf, TransmitFileOptions.UseDefaultWorkerThread);

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

注解

此重载需要的名称以及你想要发送的文件的按位组合TransmitFileOptions值。This overload requires the name of the file you want to send and a bitwise combination of TransmitFileOptions values. preBuffer参数包含你想要前加上该文件的任何数据。The preBuffer parameter contains any data you want to precede the file. postBuffer 包含你想要遵照该文件的数据。postBuffer contains data you want to follow the file. 如果fileName是在当前工作目录,它可能会标识只是该文件的名称; 否则,必须指定完整路径和文件的名称。If fileName is in the current working directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. 通配符 ("...\\myfile.txt") 和 UNC 共享名 ("\\\\shared directory\\myfile.txt") 支持。Wildcards ("..\\myfile.txt") and UNC share names ("\\\\shared directory\\myfile.txt") are supported.

flags参数提供了关于文件传输的其他信息的窗口套接字服务提供程序。The flags parameter provides the Window Sockets service provider with additional information about the file transfer. 有关如何使用此参数的详细信息,请参阅TransmitFileOptionsFor more information about how to use this parameter, see TransmitFileOptions.

此方法使用TransmitFileWindows Sockets 2 API 中找到函数。This method uses the TransmitFile function found in the Windows Sockets 2 API. 有关详细信息TransmitFile函数和它的标志,请参阅Windows 套接字文档。For more information about the TransmitFile function and its flags, see the Windows Sockets documentation.

SendFile 以同步方式将文件发送到远程主机中指定ConnectAccept方法。SendFile synchronously sends a file to the remote host specified in the Connect or Accept method. SendFile 可以使用面向连接的两个和无连接协议。SendFile can be used for both connection-oriented and for connectionless protocols.

如果您使用的是无连接协议,则必须调用Connect之前调用此方法; 否则为SendFile引发SocketExceptionIf you are using a connectionless protocol, you must call Connect before calling this method; otherwise SendFile throws a SocketException. 如果使用的是面向连接的协议,则必须使用Connect建立远程主机连接,或使用Accept以接受传入连接。If you are using a connection-oriented protocol, you must either use Connect to establish a remote host connection, or use Accept to accept an incoming connection.

如果使用面向连接的协议SendFile阻止,直到发送整个文件。If you are using a connection-oriented protocol, SendFile blocks until the entire file is sent. 在阻止模式下,SendFile发送整个文件之前,可能会成功完成。In nonblocking mode, SendFile may complete successfully before the entire file has been sent. 则您发送的数据将立即显示在网络不能保证。There is no guarantee that the data you send will appear on the network immediately. 若要增加网络效率,基础系统可能会延迟传输,直到收集了大量传出数据。To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. 成功完成SendFile方法意味着的基础系统已为你的数据的网络发送的缓冲区的空间。A successful completion of the SendFile method means that the underlying system has had room to buffer your data for a network send.

备注

如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码有关错误的详细说明的文档。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅在.NET Framework 中进行网络跟踪For more information, see Network Tracing in the .NET Framework.

适用于