Socket.SendFile 메서드

정의

연결된 Socket에 파일 및 선택적 데이터를 동기적으로 보냅니다.

오버로드

SendFile(String)

Socket 전송 플래그를 사용하여 fileName 파일을 연결된 UseDefaultWorkerThread 개체에 보냅니다.

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

지정된 TransmitFileOptions 값을 사용하여 연결된 Socket 개체에 파일 fileName 및 데이터 버퍼를 전송합니다.

SendFile(String, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, TransmitFileOptions)

지정된 TransmitFileOptions 값을 사용하여 연결된 Socket 개체에 파일 fileName 및 데이터 버퍼를 전송합니다.

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)

매개 변수

fileName
String

전송할 파일의 경로 및 이름을 포함하는 String입니다. 이 매개 변수는 null일 수 있습니다.

예외

소켓이 원격 호스트에 연결되지 않은 경우

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 연결된 소켓으로 보냅니다. 매개 변수의 기본값 UseDefaultWorkerThreadflags (0)이고, 매개 변수는 preBuffer postBuffer 기본값은 null.입니다. 로컬 디렉터리에 있는 경우 fileName 파일 이름만으로 식별될 수 있습니다. 그렇지 않으면 파일의 전체 경로와 이름을 지정해야 합니다. 와일드카드("..\\myfile.txt") 및 UNC 공유 이름("\\\\shared directory\\myfile.txt")이 지원됩니다. 파일을 찾을 수 없으면 예외 FileNotFoundException 가 throw됩니다.

이 메서드는 TransmitFile Windows Sockets 2 API에 있는 함수를 사용합니다. 함수 및 해당 플래그에 TransmitFile 대한 자세한 내용은 Windows 소켓 설명서를 참조하세요.

SendFile또는 Accept 메서드에 지정된 Connect 원격 호스트에 파일을 동기적으로 보냅니다. SendFile 는 연결 지향 프로토콜과 연결 없는 프로토콜 모두에 사용할 수 있습니다.

연결 없는 프로토콜을 사용하는 경우 이 메서드를 호출하기 전에 호출 Connect 해야 합니다. 그렇지 않으면 SendFile 예외가 SocketException throw됩니다. 연결 지향 프로토콜을 사용하는 경우 원격 호스트 연결을 설정하거나 들어오는 연결을 수락하는 데 사용해야 Accept 합니다Connect.

연결 지향 프로토콜 SendFile 을 사용하는 경우 파일이 전송될 때까지 차단합니다. 차단 해제 모드 SendFile 에서는 전체 파일이 전송되기 전에 완료할 수 있습니다. 보내는 데이터가 네트워크에 즉시 표시된다는 보장은 없습니다. 네트워크 효율성을 높이기 위해 기본 시스템은 상당한 양의 나가는 데이터가 수집될 때까지 전송을 지연할 수 있습니다. 메서드를 SendFile 성공적으로 완료하면 기본 시스템에 네트워크 전송을 위해 데이터를 버퍼링할 수 있는 공간이 있습니다.

참고

받는 SocketException경우 속성을 사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후 오류에 대한 자세한 설명은 Windows Sockets 버전 2 API 오류 코드 설명서를 참조하세요.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상

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

지정된 TransmitFileOptions 값을 사용하여 연결된 Socket 개체에 파일 fileName 및 데이터 버퍼를 전송합니다.

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

파일이 전송되는 방법을 지정하는 열거형 값의 비트 조합입니다.

예외

운영 체제가 Windows NT 이상이 아닌 경우

또는

소켓이 원격 호스트에 연결되지 않은 경우

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 Window Sockets 서비스 공급자에게 파일 전송에 대한 추가 정보를 제공합니다. 이 매개 변수를 사용하는 방법에 대한 자세한 내용은 을 참조하세요 TransmitFileOptions.

이 메서드는 TransmitFile Windows Sockets 2 API에 있는 함수를 사용합니다. 함수 및 해당 플래그에 TransmitFile 대한 자세한 내용은 Windows 소켓 설명서를 참조하세요.

SendFile또는 Accept 메서드에 지정된 Connect 원격 호스트에 파일을 동기적으로 보냅니다. SendFile 는 연결 지향 프로토콜과 연결 없는 프로토콜 모두에 사용할 수 있습니다.

연결 없는 프로토콜을 사용하는 경우 이 메서드를 호출하기 전에 호출 Connect 해야 합니다. 그렇지 않으면 SendFile SocketException. 연결 지향 프로토콜을 사용하는 경우 원격 호스트 연결을 설정하거나 들어오는 연결을 수락하는 데 사용해야 Accept 합니다Connect.

연결 지향 프로토콜 SendFile 을 사용하는 경우 전체 파일이 전송될 때까지 차단합니다. 차단 해제 모드 SendFile 에서는 전체 파일이 전송되기 전에 완료할 수 있습니다. 보내는 데이터가 네트워크에 즉시 표시된다는 보장은 없습니다. 네트워크 효율성을 높이기 위해 기본 시스템은 상당한 양의 나가는 데이터가 수집될 때까지 전송을 지연할 수 있습니다. 메서드를 SendFile 성공적으로 완료하면 기본 시스템에 네트워크 전송을 위해 데이터를 버퍼링할 수 있는 공간이 있습니다.

참고

받는 SocketException경우 속성을 사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후 오류에 대한 자세한 설명은 Windows Sockets 버전 2 API 오류 코드 설명서를 참조하세요.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상

SendFile(String, ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, TransmitFileOptions)

지정된 TransmitFileOptions 값을 사용하여 연결된 Socket 개체에 파일 fileName 및 데이터 버퍼를 전송합니다.

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)

매개 변수

fileName
String

전송할 파일의 경로 및 이름을 포함하는 String입니다. 이 매개 변수는 null일 수 있습니다.

preBuffer
ReadOnlySpan<Byte>

ReadOnlySpan<T> 파일이 전송되기 전에 전송할 데이터가 들어 있는 A입니다. 이 버퍼는 비어 있을 수 있습니다.

postBuffer
ReadOnlySpan<Byte>

ReadOnlySpan<T> 파일이 전송된 후 전송할 데이터가 포함된 A입니다. 이 버퍼는 비어 있을 수 있습니다.

flags
TransmitFileOptions

하나 이상의 TransmitFileOptions 값입니다.

예외

Socket 개체가 닫힌 경우

개체가 Socket 원격 호스트에 연결되어 있지 않습니다.

Socket 개체가 차단 모드가 아니며 이 동기 호출을 수락할 수 없는 경우

fileName 파일을 찾을 수 없는 경우

소켓에 액세스하는 동안 오류가 발생했습니다.

적용 대상