Skip to content

Unix Socket Client Send WouldBlock Server Receive Data Error #23384

@caozhiyuan

Description

@caozhiyuan
        /// <summary>
        /// Exchange a message with the host.
        /// </summary>
        /// <param name="message">Message to send.</param>
        /// <returns>Message sent by the host.</returns>
        internal void Send(string message)
        {
            if (!this.isContinue)
            {
                Console.WriteLine($"isContinue {this.isContinue}");
                return;
            }
            if (this.connected)
            {
                var nioBuffers = new List<ArraySegment<byte>>();
                nioBuffers.Add(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)));

                SocketError errorCode;
                long localWrittenBytes = this.clientSocket.Send(nioBuffers, SocketFlags.None, out errorCode);
                if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock)
                {
                    throw new SocketException((int)errorCode);
                }
                if (errorCode == SocketError.WouldBlock)
                {
                    this.isContinue = false;
                    Console.WriteLine(message);
                    var sendArgs = new SocketAsyncEventArgs();
                    sendArgs.BufferList = nioBuffers;
                    sendArgs.Completed += this.SendArgs_Completed;
                    bool pending = this.clientSocket.SendAsync(sendArgs);
                    if (!pending)
                    {
                        Console.WriteLine($"pending false BytesTransferred {sendArgs.BytesTransferred}");
                    }
                    else
                    {
                        Console.WriteLine($"pending true");
                    }
                }
                else
                {
                    Console.WriteLine($"{nioBuffers.Sum(n => n.Array.Length)} {localWrittenBytes}");
                }
            }
            else
            {
                throw new SocketException((int)SocketError.NotConnected);
            }
        }

https://github.com/caozhiyuan/DotNetty/tree/dev/examples/SocketAsyncClient

https://github.com/caozhiyuan/DotNetty/tree/dev/examples/SocketAsyncServer

qq 20170830084137

[EDIT] Add C# syntax highlighting by @karelz

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions