异步服务器套接字丢失了第一个缓冲区流

时间:2015-07-09 07:16:29

标签: c# sockets asynchronous

我使用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&#39;,服务器端将只接收&#39; klmn&#39;。我第二次发送&#39; abcdefghijklmn&#39;,服务器端将收到整个数据流。

2 个答案:

答案 0 :(得分:1)

在你的BeginAccept方法中,你传入一个缓冲区大小来读取,你不会在回调中处理它。尝试使用不带缓冲区大小的BeginAccept方法作为第一个参数

答案 1 :(得分:0)

您忽略了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