本文整理汇总了C#中System.Net.Sockets.Socket.ReceiveAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ReceiveAsync方法的具体用法?C# Socket.ReceiveAsync怎么用?C# Socket.ReceiveAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.ReceiveAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Client
public Client(Socket sock, ushort size)
{
try
{
Initialize();
Items[0].SetBuffer(new byte[size], 0, size);
_handle = sock;
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
_handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
_handle.NoDelay = true;
BufferSize = size;
_endPoint = (IPEndPoint)_handle.RemoteEndPoint;
Connected = true;
if (!_handle.ReceiveAsync(Items[0]))
Process(null, Items[0]);
}
catch
{
Disconnect();
}
}
示例2: Receive
public IObservable<string> Receive(Socket connectedSocket, SocketAsyncEventArgs socketEventArgs, IScheduler scheduler, byte[] buffer)
{
if (connectedSocket == null)
throw new ArgumentNullException("connectedSocket");
if (socketEventArgs == null)
throw new ArgumentNullException("socketEventArgs");
return Observable.Create<string>(observer =>
{
var disposable = new CompositeDisposable();
var disposableEventSubscription = socketEventArgs.CompletedObservable().ObserveOn(scheduler).Subscribe(_ =>
{
SendNotificationToObserver(observer, socketEventArgs);
});
var disposableActions = scheduler.Schedule(() =>
{
socketEventArgs.SetBuffer(buffer, 0, buffer.Length);
if (!connectedSocket.ReceiveAsync(socketEventArgs))
{
SendNotificationToObserver(observer, socketEventArgs);
}
});
disposable.Add(disposableEventSubscription);
disposable.Add(disposableActions);
return disposable;
});
}
示例3: Client
public Client(Socket socket)
{
this.socket = socket;
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += RecieveComplete;
args.SetBuffer(new byte[4000], 0, 4000);
socket.ReceiveAsync(args);
}
示例4: receive
private void receive(Socket socket)
{
if (!checkSocketConnect(socket))
return;
var client = dic_client[socket];
client.context_head.SetBuffer(new byte[packageHeadLen], 0, packageHeadLen);
socket.ReceiveAsync(client.context_head);
}
示例5: ClientWorker
public ClientWorker(Socket client)
{
_client = client;
eventArgs = new SocketAsyncEventArgs();
buffer = new byte[1024 * 1024];//1Mb
eventArgs.SetBuffer(buffer, 0, buffer.Length);
eventArgs.Completed += eventArgsCompleted;
_client.ReceiveAsync(eventArgs);
IP = client.RemoteEndPoint.ToString();
}
示例6: ReadMessage
public string ReadMessage(Socket socket)
{
message = string.Empty;
socketEventArgs = new SocketAsyncEventArgs();
socketEventArgs.RemoteEndPoint = socket.RemoteEndPoint;
socketEventArgs.SetBuffer(new Byte[2048], 0, 2048);
socketEventArgs.Completed += OnReadCompleted;
clientDone.Reset();
socket.ReceiveAsync(socketEventArgs);
clientDone.WaitOne();
return message;
}
示例7: TunnelClient
public TunnelClient(Socket client)
{
Client = client;
SendArgs = TunnelServer.Instance.PopArgs();
ReceiveArgs = TunnelServer.Instance.PopArgs();
SendArgs.Completed += Send_Completed;
ReceiveArgs.Completed += Receive_Completed;
ReceiveArgs.SetBuffer(new byte[1500], 0, 1500);
if (!Client.ReceiveAsync(ReceiveArgs))
{
Receive_Completed(Client, ReceiveArgs);
}
}
示例8: EnsureSocket
//internal Socket (AddressFamily family, SocketType type, ProtocolType proto, IntPtr sock)
static bool EnsureSocket ()
{
lock (_lock) {
if (nl_sock != null)
return true;
IntPtr fd = CreateNLSocket ();
if (fd.ToInt64 () == -1)
return false;
nl_sock = new Socket (0, SocketType.Raw, ProtocolType.Udp, fd);
nl_args = new SocketAsyncEventArgs ();
nl_args.SetBuffer (new byte [8192], 0, 8192);
nl_args.Completed += OnDataAvailable;
nl_sock.ReceiveAsync (nl_args);
}
return true;
}
示例9: SocksClient
public SocksClient(short id, Socket client)
{
ID = id;
Client = client;
SendArgs = SocksServer.Instance.PopArgs();
ReceiveArgs = SocksServer.Instance.PopArgs();
SendArgs.Completed += Send_Completed;
ReceiveArgs.Completed += Receive_Completed;
ReceiveArgs.SetBuffer(new byte[16384], 0, 16384);
if (!Client.ReceiveAsync(ReceiveArgs))
{
Receive_Completed(Client, ReceiveArgs);
}
}
示例10: ReadAsync
async Task ReadAsync(Socket s)
{
// Reusable SocketAsyncEventArgs and awaitable wrapper
var args = new SocketAsyncEventArgs();
args.SetBuffer(new byte[0x100000], 0, 0x100000);
var awaitable = new SocketAwaitable(args);
while (true)
{
await s.ReceiveAsync(awaitable);
int bytesRead = args.BytesTransferred;
if (bytesRead <= 0) throw new Exception("Raw socket is disconnected");
var ipPacket = new IPv4Packet(new ByteArraySegment(args.Buffer, 0, bytesRead));
if (ipPacket.Version != IpVersion.IPv4 || ipPacket.Protocol!=IPProtocolType.TCP)
continue;
OnPacketReceived(ipPacket);
}
}
示例11: Connection
public Connection(Socket socket)
{
this.socket = socket;
RemoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.SetBuffer(new byte[1000], 0, 1000);
e.DisconnectReuseSocket = true;
e.Completed += new EventHandler<SocketAsyncEventArgs>(OnMessageReceived);
Console.WriteLine("Connected to " + RemoteEndPoint.Address.ToString());
if (!socket.ReceiveAsync(e))
{
Disconnect();
}
}
示例12: Client
public Client(Socket sock, ushort size)
{
try
{
Initialize();
Items[0].SetBuffer(new byte[size], 0, size);
Handle = sock;
Size = size;
_EndPoint = (IPEndPoint)Handle.RemoteEndPoint;
_Connection = true;
if (!Handle.ReceiveAsync(Items[0]))
Process(null, Items[0]);
}
catch
{
Disconnect();
}
}
示例13: ReadMessage
public string ReadMessage(Socket socket)
{
string message = string.Empty;
socketEventArgs = new SocketAsyncEventArgs();
socketEventArgs.RemoteEndPoint = socket.RemoteEndPoint;
socketEventArgs.SetBuffer(new Byte[2048], 0, 2048);
socketEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(
delegate(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
message = Encoding.Unicode.GetString(e.Buffer, e.Offset, e.BytesTransferred);
message = message.Trim('\0');
}
clientDone.Set();
});
clientDone.Reset();
socket.ReceiveAsync(socketEventArgs);
clientDone.WaitOne(5000);
return message;
}
示例14: SendingSocketCreator
private Socket SendingSocketCreator(IPEndPoint target)
{
var s = new Socket(target.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
s.Connect(target);
// Prep the socket so it will reset on close and won't Nagle
s.LingerState = new LingerOption(true, 0);
s.NoDelay = true;
WriteConnectionPreamble(s, Constants.SiloDirectConnectionId); // Identifies this client as a direct silo-to-silo socket
// Start an asynch receive off of the socket to detect closure
var receiveAsyncEventArgs = new SocketAsyncEventArgs
{
BufferList = new List<ArraySegment<byte>> { new ArraySegment<byte>(new byte[4]) },
UserToken = new Tuple<Socket, IPEndPoint, SocketManager>(s, target, this)
};
receiveAsyncEventArgs.Completed += ReceiveCallback;
bool receiveCompleted = s.ReceiveAsync(receiveAsyncEventArgs);
NetworkingStatisticsGroup.OnOpenedSendingSocket();
if (!receiveCompleted)
{
ReceiveCallback(this, receiveAsyncEventArgs);
}
}
catch (Exception)
{
try
{
s.Dispose();
}
catch (Exception)
{
// ignore
}
throw;
}
return s;
}
示例15: GetPolicyStream
static Stream GetPolicyStream (IPEndPoint endpoint)
{
MemoryStream ms = new MemoryStream ();
ManualResetEvent mre = new ManualResetEvent (false);
// Silverlight only support TCP
Socket socket = new Socket (endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Application code can't connect to port 943, so we need a special/internal API/ctor to allow this
SocketAsyncEventArgs saea = new SocketAsyncEventArgs (true);
saea.RemoteEndPoint = new IPEndPoint (endpoint.Address, PolicyPort);
saea.Completed += delegate (object sender, SocketAsyncEventArgs e) {
if (e.SocketError != SocketError.Success) {
mre.Set ();
return;
}
switch (e.LastOperation) {
case SocketAsyncOperation.Connect:
e.SetBuffer (socket_policy_file_request, 0, socket_policy_file_request.Length);
socket.SendAsync (e);
break;
case SocketAsyncOperation.Send:
byte [] buffer = new byte [256];
e.SetBuffer (buffer, 0, buffer.Length);
socket.ReceiveAsync (e);
break;
case SocketAsyncOperation.Receive:
int transfer = e.BytesTransferred;
if (transfer > 0) {
ms.Write (e.Buffer, 0, transfer);
// Console.Write (Encoding.UTF8.GetString (e.Buffer, 0, transfer));
}
if ((transfer == 0) || (transfer < e.Buffer.Length)) {
ms.Position = 0;
mre.Set ();
} else {
socket.ReceiveAsync (e);
}
break;
}
};
socket.ConnectAsync (saea);
// behave like there's no policy (no socket access) if we timeout
if (!mre.WaitOne (Timeout))
return null;
return ms;
}