异步服务器套接字缺少第一个缓冲流

Posted

技术标签:

【中文标题】异步服务器套接字缺少第一个缓冲流【英文标题】:Asynchronous Server Socket missing the first buffer stream 【发布时间】:2015-09-27 10:05:35 【问题描述】:

我正在使用 C# 异步服务器套接字,它总是错过第一个缓冲流。我已经调试了客户端,它没有显示丢失数据的迹象。我怀疑问题出在服务器上,但我似乎无法确定问题所在。

服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace ChatServer

    class TCPServer
    
        private List<Socket> socketList;
        private const int PORT = 1337;
        private Socket serverSocket;
        const int BUFFER_SIZE = 1;
        byte[] receiveBuffer = new byte[BUFFER_SIZE];
        public TCPServer()
        
            socketList = new List<Socket>();
        

        public void Go()
        
            SetupChat();
            StartupNetwork();
            AcceptConnections();
        

        private void HandleDisconnections()
        
            // throw new NotImplementedException();
        

        private void Update()
        
            // throw new NotImplementedException();
        

        private void DumpGarbageSocket()
        

        

        private void ProcessMessages()
        

        

        private void SendToAllClients(string inMsg)
        
            foreach (Socket clientSock in socketList)
            
                SendMSGToClient(clientSock, inMsg);
            
        

        private void AcceptConnections()
        
            serverSocket.BeginAccept(ClientSocket.BUFFER_SIZE, StartAccepting, serverSocket);
        

        private void StartAccepting(IAsyncResult inAsyncResult)
        
            try
            
                Socket serverSock = (Socket)inAsyncResult.AsyncState;
                Socket clientSocket = serverSock.EndAccept(inAsyncResult);
                socketList.Add(clientSocket);
                ClientSocket cSocket = new ClientSocket();
                cSocket.clientSocket = clientSocket;
                Console.WriteLine("[SYSTEM] Socket has been connected.");
                clientSocket.BeginReceive(cSocket.buffer_stream, 0, ClientSocket.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(StartReceiving), cSocket);
            
            catch (Exception ex)
            
                Console.WriteLine("[ERROR] " + ex.Message);
            
            finally
            
                serverSocket.BeginAccept(ClientSocket.BUFFER_SIZE, StartAccepting, serverSocket);
            
        

        private void StartReceiving(IAsyncResult inAsyncResult)
        
            try
            
                string content = String.Empty;
                ClientSocket cSocket = (ClientSocket)inAsyncResult.AsyncState;
                Socket clientSocket = cSocket.clientSocket;
                int bytesRead = clientSocket.EndReceive(inAsyncResult);
                if (bytesRead > 0)
                
                    // There  might be more data, so store the data received so far.
                    cSocket.message.Append(Encoding.ASCII.GetString(
                        cSocket.buffer_stream, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read 
                    // more data.
                    content = cSocket.message.ToString();
                    if (content.IndexOf("<EOF>") > -1)
                    
                        // All the data has been read from the 
                        // client. Display it on the console.
                        Console.WriteLine(content.Substring(0, content.Length - 5));
                        SendToAllClients(content.Substring(0, content.Length - 5) + "<EOF>");
                        cSocket.message.Remove(0, cSocket.message.Length);
                    
                    else
                    
                        // Not all data received. Get more.
                    
                    clientSocket.BeginReceive(cSocket.buffer_stream, 0, ClientSocket.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(StartReceiving), cSocket);
                
            
            catch (Exception err)
            
                Console.WriteLine("[ERROR] " + err.Message);
            
        

        private void SendMSGToClient(Socket inSocketToSend, string message)
        
            byte[] dataBuffer = Encoding.ASCII.GetBytes(message);
            inSocketToSend.BeginSend(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(MsgFinishSending), inSocketToSend);
        

        private void MsgFinishSending(IAsyncResult inAsyncResult)
        
            Socket handler = (Socket)inAsyncResult.AsyncState;
            int bytesSent = handler.EndSend(inAsyncResult);
        

        private void StartupNetwork()
        
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
            Console.WriteLine("SERVER::STARTING SOCKET");
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(localEndPoint);
            serverSocket.Listen(100);
            Console.WriteLine("SERVER::BINDED");
            Console.WriteLine("SERVER::IP ADDRESS::" + localEndPoint.Address.ToString());
        

        private void SetupChat()
        
            //throw new NotImplementedException();
        


    

示例:缓冲区大小为 10,因此如果我通过“abcdefghijklmn”发送,服务器端将只接收“klmn”。我第二次发送'abcdefghijklmn'时,服务器端将接收到整个数据流。

【问题讨论】:

【参考方案1】:

在您的BeginAccept 方法中,您传递了要读取的缓冲区大小,并且您不在回调中处理它。尝试使用BeginAccept 方法,不使用缓冲区大小作为第一个参数

【讨论】:

【参考方案2】:

您忽略了BeginReceive 的返回值,这是一个常见错误。 BeginReceive 操作很可能是同步完成的,在这种情况下,根本不会调用回调。

试试这样的:

var result = clientSocket.BeginReceive
             (
               cSocket.buffer_stream, 0, ClientSocket.BUFFER_SIZE, SocketFlags.None, 
               new AsyncCallback(StartReceiving), cSocket
             );

if (result.CompletedSynchronously) StartReceiving(result);

将其编码为IAsyncResult 上的扩展方法非常方便(如果合适,使用BeginInvoke 而不是Invoke)。或者,更好的是,开始使用基于 Task 的新异步 API - 这会将所有这些样板转换为简单的 await ReceiveAsync(或者更好的是,使用 TcpClientReadAsync)。

【讨论】:

以上是关于异步服务器套接字缺少第一个缓冲流的主要内容,如果未能解决你的问题,请参考以下文章

使用流异步读取文件时如何同步处理每一行/缓冲区[重复]

NodeJS:如何为测试服务器创建一个假的 tcp 套接字

阻塞和非阻塞同步和异步

如何异步与服务器通话?

使用 SSL 的流式套接字

WSAEWOULDBLOCK 处理