本文整理汇总了C#中System.Net.Sockets.Socket.BeginSendFile方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginSendFile方法的具体用法?C# Socket.BeginSendFile怎么用?C# Socket.BeginSendFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.BeginSendFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsyncFile
public void SendAsyncFile ()
{
Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
serverSocket.Listen (1);
var mReceived = new ManualResetEvent (false);
serverSocket.BeginAccept (AsyncCall => {
byte[] bytes = new byte [1024];
Socket listener = (Socket)AsyncCall.AsyncState;
Socket client = listener.EndAccept (AsyncCall);
client.Receive (bytes, bytes.Length, 0);
client.Close ();
mReceived.Set ();
}, serverSocket);
Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect (serverSocket.LocalEndPoint);
clientSocket.NoDelay = true;
// Initialize buffer used to create testing file
var buffer = new byte [1024];
for (int i = 0; i < 1024; ++i)
buffer [i] = (byte) (i % 256);
string temp = Path.GetTempFileName ();
try {
// Testing file creation
using (StreamWriter sw = new StreamWriter (temp)) {
sw.Write (buffer);
}
var mSent = new ManualResetEvent (false);
// Async Send File to server
clientSocket.BeginSendFile(temp, (ar) => {
Socket client = (Socket) ar.AsyncState;
client.EndSendFile (ar);
mSent.Set ();
}, clientSocket);
if (!mSent.WaitOne (1500))
throw new TimeoutException ();
if (!mReceived.WaitOne (1500))
throw new TimeoutException ();
} finally {
if (File.Exists (temp))
File.Delete (temp);
clientSocket.Close ();
serverSocket.Close ();
}
}
示例2: SendAsyncFile
public void SendAsyncFile ()
{
Socket serverSocket = StartSocketServer ();
Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect (serverSocket.LocalEndPoint);
clientSocket.NoDelay = true;
// Initialize buffer used to create testing file
var buffer = new byte [1024];
for (int i = 0; i < 1024; ++i)
buffer [i] = (byte) (i % 256);
string temp = Path.GetTempFileName ();
try {
// Testing file creation
using (StreamWriter sw = new StreamWriter (temp)) {
sw.Write (buffer);
}
var m = new ManualResetEvent (false);
// Async Send File to server
clientSocket.BeginSendFile(temp, (ar) => {
Socket client = (Socket) ar.AsyncState;
client.EndSendFile (ar);
m.Set ();
}, clientSocket);
if (!m.WaitOne (1500))
throw new TimeoutException ();
m.Reset ();
} finally {
if (File.Exists (temp))
File.Delete (temp);
clientSocket.Close ();
serverSocket.Close ();
}
}
示例3: SendFile
/// <summary>
/// Attempts to asynchronously serve a file on the specified socket.
/// Sends a HTTP 404 error on the socket if the file is not found.
/// </summary>
/// <param name="filePath">The local path of a file</param>
/// <param name="handler">The socket to send the file data on</param>
private static void SendFile(string filePath, Socket handler)
{
if (File.Exists(filePath))
{
byte[] data = File.ReadAllBytes(filePath);
// 200 OK
string header = BuildResponseHeader(GetMimeType(filePath), data.Length, HttpStatusCode.OK);
Send(handler, header);
StateObject state = new StateObject();
state.Socket = handler;
state.Data.Append(filePath);
handler.BeginSendFile(filePath, new AsyncCallback(SendFileCallback), state);
}
else
{
// 404 NotFound
string code = (int)HttpStatusCode.NotFound + " " + HttpStatusCode.NotFound;
Send(handler, BuildResponseHeader("text/plain; charset=UTF-8", code.Length, HttpStatusCode.NotFound));
Send(handler, code);
}
}