socketasynceventargs

SocketAsyncEventArgs in a duplex server environment?

冷暖自知 提交于 2020-06-29 11:52:09
问题 I am learning to use the SocketAsyncEventArgs stuff using the MSDN tutorial. I was just wondering a few things, namely how I could go about implementing the server with full-duplex capabilities. Currently, the MSDN example teaches you to create a server that first listens, then when something is received, to send it back. Only then does the server start listening again. The problem I am having coming up with my own solution is that the SocketAsyncEventArgs object has only one event, Completed

SocketAsyncEventArgs SendAsync callback doesn't work

自古美人都是妖i 提交于 2020-01-25 11:57:13
问题 I've two SocketAsyncEventArgs per Client like this: void Accepted(object s, SocketAsyncEventArgs e) { e.Completed -= Accepted; Client c = new Client() { SendArgs = e, RecvArgs = new SocketAsyncEventArgs() }; c.RecvArgs.AcceptSocket = e.AcceptSocket; c.RecvArgs.Completed += Received; c.SendArgs.Completed += Sent; SetBuffer(c.RecvArgs, bufferLength); c.RecvArgs.AcceptSocket.ReceiveAsync(c.RecvArgs); Accept(); } When Client sends data, Received function gets called and in Received function I'm

How to cancel custom awaitable

为君一笑 提交于 2019-12-24 10:44:45
问题 I've read Stephen Toub's blog about making a custom awaitable for SocketAsyncEventArgs. This works all fine. But what I need is a cancellable awaitable and the blog doesn't cover this topic. Also Stephen Cleary unfortunately doesn't cover in his book how to cancel async methods that don't support cancellation. I tried to implement it myself, but I fail with the TaskCompletionSource and Task.WhenAny because with the awaitable I'm not actually awaiting a task. This is what I would like to have:

.NET Async Sockets: any benefit of SocketAsyncEventArgs over Begin/End in this scenario?

一世执手 提交于 2019-12-22 06:59:46
问题 Socket has these new async methods since .NET 3.5 for use with SocketAsyncEventArgs (e.g. Socket.SendAsync()), benefits being under the hood they use IO completion ports and avoid the need to keep allocating. We have made a class called UdpStream with a simple interface - just StartSend and a Completed event. It allocates two SocketAsyncEventArgs, one for send and one for receiving. The StartSend simply dispatches a message using SendAsync, and is called about 10 times a second. We use the

TcpListener vs SocketAsyncEventArgs

假装没事ソ 提交于 2019-12-19 09:04:08
问题 Is there a valid reason to not use TcpListener for implementing a high performance/high throughput TCP server instead of SocketAsyncEventArgs ? I've already implemented this high performance/high throughput TCP server using SocketAsyncEventArgs went through all sort of headaches to handling those pinned buffers using a big pre-allocated byte array and pools of SocketAsyncEventArgs for accepting and receiving, putting together using some low level stuff and shiny smart code with some TPL Data

Reading byte[] in Client/Server TCP app in a single call

蹲街弑〆低调 提交于 2019-12-13 02:57:54
问题 Client app creates a List<Person> data from an ObservableCollection<Person> , then converts the data into a byte[] array and sends the Length of the array to the server before sending the actual array like this: void SendData(object o) { var data = new List<Person>(ListOfPerson); var array = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); socket.Send(BitConverter.GetBytes(array.Length)); socket.Send(array); ListOfPerson.Clear(); } and server first reads the length from e.Buffer

TcpListener vs SocketAsyncEventArgs

邮差的信 提交于 2019-12-11 13:48:34
问题 Is there a valid reason to not use TcpListener for implementing a high performance/high throughput TCP server instead of SocketAsyncEventArgs ? I've already implemented this high performance/high throughput TCP server using SocketAsyncEventArgs went through all sort of headaches to handling those pinned buffers using a big pre-allocated byte array and pools of SocketAsyncEventArgs for accepting and receiving, putting together using some low level stuff and shiny smart code with some TPL Data

How to use Socket.SendAsync to send large data

余生长醉 提交于 2019-12-07 17:49:36
问题 private void ProcessReceive(SocketAsyncEventArgs e) { // Check if the remote host closed the connection. if (e.BytesTransferred > 0) { if (e.SocketError == SocketError.Success) { Token token = e.UserToken as Token; token.SetData(e); Socket s = token.Connection; if (s.Available == 0) { Boolean willRaiseEvent = false; // GET DATA TO SEND byte[] sendBuffer = token.GetRetBuffer(); // this.bufferSize IS SocketAsyncEventArgs buffer SIZE byte[] tempBuffer = new byte[this.bufferSize]; int offset = 0;

How to use Socket.SendAsync to send large data

雨燕双飞 提交于 2019-12-05 22:31:35
private void ProcessReceive(SocketAsyncEventArgs e) { // Check if the remote host closed the connection. if (e.BytesTransferred > 0) { if (e.SocketError == SocketError.Success) { Token token = e.UserToken as Token; token.SetData(e); Socket s = token.Connection; if (s.Available == 0) { Boolean willRaiseEvent = false; // GET DATA TO SEND byte[] sendBuffer = token.GetRetBuffer(); // this.bufferSize IS SocketAsyncEventArgs buffer SIZE byte[] tempBuffer = new byte[this.bufferSize]; int offset = 0; int size = (int)Math.Ceiling((double)sendBuffer.Length / (double)this.bufferSize); for (int i = 0; i <

.NET Async Sockets: any benefit of SocketAsyncEventArgs over Begin/End in this scenario?

半腔热情 提交于 2019-12-05 11:35:17
Socket has these new async methods since .NET 3.5 for use with SocketAsyncEventArgs (e.g. Socket.SendAsync() ), benefits being under the hood they use IO completion ports and avoid the need to keep allocating. We have made a class called UdpStream with a simple interface - just StartSend and a Completed event. It allocates two SocketAsyncEventArgs, one for send and one for receiving. The StartSend simply dispatches a message using SendAsync, and is called about 10 times a second. We use the Completed event on the receive SocketAsyncEventArgs, and after each event is handled we all ReceiveAsync