Socket.SendFile 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将文件和可选数据异步发送到连接的 Socket。
重载
SendFile(String) |
使用 Socket 传输标志,将文件 |
SendFile(String, Byte[], Byte[], TransmitFileOptions) |
通过指定的 TransmitFileOptions 值,将文件 |
SendFile(String, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, TransmitFileOptions) |
通过指定的 TransmitFileOptions 值,将文件 |
SendFile(String)
使用 Socket 传输标志,将文件 fileName
发送到连接的 UseDefaultWorkerThread 对象。
public:
void SendFile(System::String ^ fileName);
public void SendFile (string fileName);
public void SendFile (string? fileName);
member this.SendFile : string -> unit
Public Sub SendFile (fileName As String)
参数
例外
套接字未连接到远程主机。
Socket 对象已关闭。
Socket 对象未处于阻止模式,无法接受此同步调用。
未找到文件 fileName
。
尝试访问套接字时出错。
示例
下面的代码示例创建并连接套接字,然后将文件发送到远程主机。 文件“test.txt”位于本地计算机的根目录中。
// 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
发送到连接的套接字。 参数flags
默认为 UseDefaultWorkerThread (0) ,参数preBuffer``postBuffer
默认为 null
. 如果 fileName
位于本地目录中,则只能使用文件的名称进行标识;否则,必须指定文件的完整路径和名称。 通配符 (“。。\\myfile.txt“) 和 UNC 共享名称 (”\\\\shared directory\\myfile.txt“) 受支持。 如果未找到该文件,则会引发异常 FileNotFoundException 。
此方法使用TransmitFile
在 Windows Sockets 2 API 中找到的函数。 有关函数及其标志的详细信息TransmitFile
,请参阅 Windows Sockets 文档。
SendFile同步将文件发送到在或Accept方法中指定的Connect远程主机。 SendFile 可用于面向连接的协议和无连接协议。
如果使用无连接协议,则必须在调用此方法之前调用 Connect ,否则 SendFile 将 SocketException 引发异常。 如果使用面向连接的协议,则必须用于 Connect 建立远程主机连接,或者用于 Accept 接受传入连接。
如果使用面向连接的协议, SendFile 则在发送文件之前阻止。 在非阻止模式下, SendFile 可能会在发送整个文件之前成功完成。 不能保证你发送的数据将立即显示在网络上。 为了提高网络效率,基础系统可能会延迟传输,直到收集大量传出数据。 方法的 SendFile 成功完成意味着基础系统有空间来缓冲网络发送的数据。
备注
如果收到, SocketException请使用 SocketException.ErrorCode 该属性获取特定的错误代码。 获取此代码后,请参阅 Windows Sockets 版本 2 API 错误代码文档,获取错误的详细说明。
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪。
适用于
SendFile(String, Byte[], Byte[], TransmitFileOptions)
通过指定的 TransmitFileOptions 值,将文件 fileName
和数据缓冲区发送到连接的 Socket 对象。
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);
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
要发送的文件的路径和名称。 此参数可以为 null
。
- preBuffer
- Byte[]
在发送文件之前要发送的数据。 此参数可以为 null
。
- postBuffer
- Byte[]
发送文件后要发送的数据。 此参数可以为 null
。
- flags
- TransmitFileOptions
枚举值的按位组合,指定文件传输方式。
例外
Socket 对象已关闭。
Socket 对象未处于阻止模式,无法接受此同步调用。
未找到文件 fileName
。
尝试访问套接字时出错。
示例
下面的代码示例创建并连接套接字。 文件“test.txt”位于本地计算机的根目录中。 在此示例中,我们将创建数据准备和后缀,并使用该文件将其发送到远程主机。 使用默认值 TransmitFileOptions 。
// 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 。 该 preBuffer
参数包含要在文件前面使用的任何数据。 postBuffer
包含要关注该文件的数据。 如果 fileName
位于当前工作目录中,则只能使用文件的名称进行标识;否则,必须指定文件的完整路径和名称。 通配符 (“。。\\myfile.txt“) 和 UNC 共享名称 (”\\\\shared directory\\myfile.txt“) 受支持。
此参数 flags
为窗口套接字服务提供程序提供了有关文件传输的其他信息。 有关如何使用此参数的详细信息,请参阅 TransmitFileOptions。
此方法使用TransmitFile
在 Windows Sockets 2 API 中找到的函数。 有关函数及其标志的详细信息TransmitFile
,请参阅 Windows Sockets 文档。
SendFile同步将文件发送到在或Accept方法中指定的Connect远程主机。 SendFile 可用于面向连接的协议和无连接协议。
如果使用无连接协议,则必须在调用此方法之前调用Connect;否则SendFile将引发 。SocketException 如果使用面向连接的协议,则必须用于 Connect 建立远程主机连接,或者用于 Accept 接受传入连接。
如果使用面向连接的协议, SendFile 请阻止直到发送整个文件。 在非阻止模式下, SendFile 可能会在发送整个文件之前成功完成。 不能保证你发送的数据将立即显示在网络上。 为了提高网络效率,基础系统可能会延迟传输,直到收集大量传出数据。 方法的 SendFile 成功完成意味着基础系统有空间来缓冲网络发送的数据。
备注
如果收到, SocketException请使用 SocketException.ErrorCode 该属性获取特定的错误代码。 获取此代码后,请参阅 Windows Sockets 版本 2 API 错误代码文档,获取错误的详细说明。
备注
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪。
适用于
SendFile(String, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, TransmitFileOptions)
通过指定的 TransmitFileOptions 值,将文件 fileName
和数据缓冲区发送到连接的 Socket 对象。
public:
void SendFile(System::String ^ fileName, ReadOnlySpan<System::Byte> preBuffer, ReadOnlySpan<System::Byte> postBuffer, System::Net::Sockets::TransmitFileOptions flags);
public void SendFile (string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpan<byte> postBuffer, System.Net.Sockets.TransmitFileOptions flags);
member this.SendFile : string * ReadOnlySpan<byte> * ReadOnlySpan<byte> * System.Net.Sockets.TransmitFileOptions -> unit
Public Sub SendFile (fileName As String, preBuffer As ReadOnlySpan(Of Byte), postBuffer As ReadOnlySpan(Of Byte), flags As TransmitFileOptions)
参数
- preBuffer
- ReadOnlySpan<Byte>
一个 ReadOnlySpan<T> ,其中包含在发送文件之前要发送的数据。 此缓冲区可以为空。
- postBuffer
- ReadOnlySpan<Byte>
一个 ReadOnlySpan<T> ,其中包含在发送文件后要发送的数据。 此缓冲区可以为空。
- flags
- TransmitFileOptions
一个或多个 TransmitFileOptions 值。
例外
Socket 对象已关闭。
该 Socket 对象未连接到远程主机。
Socket 对象未处于阻止模式,无法接受此同步调用。
未找到文件 fileName
。
尝试访问套接字时出错。