本文整理汇总了C#中Socket.ConnectAPM方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ConnectAPM方法的具体用法?C# Socket.ConnectAPM怎么用?C# Socket.ConnectAPM使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.ConnectAPM方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendRecvAPM_Stream_TCP
private static void SendRecvAPM_Stream_TCP(IPAddress listenAt, bool useMultipleBuffers)
{
const int BytesToSend = 123456;
const int ListenBacklog = 1;
const int LingerTime = 10;
const int TestTimeout = 30000;
var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
server.BindToAnonymousPort(listenAt);
server.Listen(ListenBacklog);
var serverFinished = new TaskCompletionSource<bool>();
int bytesReceived = 0;
var receivedChecksum = new Fletcher32();
server.AcceptAPM(remote =>
{
Action<int> recvHandler = null;
bool first = true;
if (!useMultipleBuffers)
{
var recvBuffer = new byte[256];
recvHandler = received =>
{
if (!first)
{
if (received == 0)
{
remote.Dispose();
server.Dispose();
serverFinished.SetResult(true);
return;
}
bytesReceived += received;
receivedChecksum.Add(recvBuffer, 0, received);
}
else
{
first = false;
}
remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler);
};
}
else
{
var recvBuffers = new List<ArraySegment<byte>> {
new ArraySegment<byte>(new byte[123]),
new ArraySegment<byte>(new byte[256], 2, 100),
new ArraySegment<byte>(new byte[1], 0, 0),
new ArraySegment<byte>(new byte[64], 9, 33)
};
recvHandler = received =>
{
if (!first)
{
if (received == 0)
{
remote.Dispose();
server.Dispose();
serverFinished.SetResult(true);
return;
}
bytesReceived += received;
for (int i = 0, remaining = received; i < recvBuffers.Count && remaining > 0; i++)
{
ArraySegment<byte> buffer = recvBuffers[i];
int toAdd = Math.Min(buffer.Count, remaining);
receivedChecksum.Add(buffer.Array, buffer.Offset, toAdd);
remaining -= toAdd;
}
}
else
{
first = false;
}
remote.ReceiveAPM(recvBuffers, SocketFlags.None, recvHandler);
};
}
recvHandler(0);
});
EndPoint clientEndpoint = server.LocalEndPoint;
var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
int bytesSent = 0;
var sentChecksum = new Fletcher32();
client.ConnectAPM(clientEndpoint, () =>
{
Action<int> sendHandler = null;
var random = new Random();
var remaining = BytesToSend;
//.........这里部分代码省略.........
示例2: SendFile_APM
private void SendFile_APM(IPAddress listenAt, bool sendPreAndPostBuffers)
{
const int BytesToSend = 123456;
const int ListenBacklog = 1;
const int LingerTime = 10;
const int TestTimeout = 30000;
// Create file to send
byte[] preBuffer;
byte[] postBuffer;
Fletcher32 sentChecksum;
string filename = CreateFileToSend(BytesToSend, sendPreAndPostBuffers, out preBuffer, out postBuffer, out sentChecksum);
// Start server
var server = new Socket(listenAt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
server.BindToAnonymousPort(listenAt);
server.Listen(ListenBacklog);
var serverFinished = new TaskCompletionSource<bool>();
int bytesReceived = 0;
var receivedChecksum = new Fletcher32();
server.AcceptAPM(remote =>
{
Action<int> recvHandler = null;
bool first = true;
var recvBuffer = new byte[256];
recvHandler = received =>
{
if (!first)
{
if (received == 0)
{
remote.Dispose();
server.Dispose();
serverFinished.SetResult(true);
return;
}
bytesReceived += received;
receivedChecksum.Add(recvBuffer, 0, received);
}
else
{
first = false;
}
remote.ReceiveAPM(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, recvHandler);
};
recvHandler(0);
});
// Run client
EndPoint clientEndpoint = server.LocalEndPoint;
var client = new Socket(clientEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
client.ConnectAPM(clientEndpoint, () =>
{
client.SendFileAPM(filename, preBuffer, postBuffer, TransmitFileOptions.UseDefaultWorkerThread, () =>
{
client.LingerState = new LingerOption(true, LingerTime);
client.Dispose();
});
});
Assert.True(serverFinished.Task.Wait(TestTimeout), "Completed within allowed time");
Assert.Equal(BytesToSend, bytesReceived);
Assert.Equal(sentChecksum.Sum, receivedChecksum.Sum);
// Clean up the file we created
File.Delete(filename);
}