1

我是字典概念的新手。我找到了一种创建能够处理多个客户端的简单服务器的方法。有没有一种方法可以使用字典/ConcurrentDictionary 来保存信息,例如建立连接的时间以及它用于各种客户端的端口。我创建了一个单独的示例字典,但我不知道如何使它与服务器应用程序一起工作,或者它是否可以工作。(服务器是使用 localhost 和端口 9090 创建的。客户端也在本地运行)。如果您需要任何澄清,请告诉我。我将如何/在哪里放置字典代码块以允许它保存已建立的连接信息,或者是否有不同/更好的方法?

Dictionary<int, DateTime> ServerDictionary = new Dictionary<int, DateTime>();

        ServerDictionary.Add(9090, DateTime.Now.AddMinutes(5));
            Console.WriteLine("Time List");

            foreach (KeyValuePair<int, DateTime> time in ServerDictionary)
            {
                Console.WriteLine("Port = {0}, Time in 5 = {1}",
                    time.Key, time.Value);
            }
        Console.ReadKey();

下面是服务器代码:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Multi_Server
{
    class Program
    {
        private static Socket _serverSocket;
        private static readonly List<Socket> _clientSockets = new List<Socket>();
        private const int _BUFFER_SIZE = 2048;
        private const int _PORT = 9090;
        private static readonly byte[] _buffer = new byte[_BUFFER_SIZE];

        static void Main()
        {
            Console.Title = "Server";
            SetupServer();
            Console.ReadLine(); // When we press enter close everything
            CloseAllSockets();
        }

        private static void SetupServer()
        {
            Console.WriteLine("Setting up server...");
            _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _serverSocket.Bind(new IPEndPoint(IPAddress.Any, _PORT));
            _serverSocket.Listen(5);
            _serverSocket.BeginAccept(AcceptCallback, null);
            Console.WriteLine("Server setup complete");
        }

        // Close all connected client 
        // (we do not need to shutdown the server socket 
        // as its connections are already closed with the clients)
        private static void CloseAllSockets()
        {
            foreach (Socket socket in _clientSockets)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            _serverSocket.Close();
        }

        private static void AcceptCallback(IAsyncResult AR)
        {
            Socket socket;

            try
            {
                socket = _serverSocket.EndAccept(AR);
            }
            catch (ObjectDisposedException) // I cannot seem to avoid this (on exit when properly closing sockets)
            {
                return;
            }

            _clientSockets.Add(socket);
            socket.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, socket);
            Console.WriteLine("Client connected, waiting for request...");
            _serverSocket.BeginAccept(AcceptCallback, null);
        }

        private static void ReceiveCallback(IAsyncResult AR)
        {
            Socket current = (Socket)AR.AsyncState;
            int received;

            try
            {
                received = current.EndReceive(AR);
            }
            catch (SocketException)
            {
                Console.WriteLine("Client forcefully disconnected");
                current.Close(); // Dont shutdown because the socket may be disposed and its disconnected anyway
                _clientSockets.Remove(current);
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(_buffer, recBuf, received);
            string text = Encoding.ASCII.GetString(recBuf);
            Console.WriteLine("Received Text: " + text);

            if (text.ToLower() == "get time") // Client requested time
            {
                Console.WriteLine("Text is a 'get time' request");
                byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
                current.Send(data);
                Console.WriteLine("Time sent to client");
            }
            else if (text.ToLower() == "exit") // Client wants to exit gracefully
            {
                // Always Shutdown before closing
                current.Shutdown(SocketShutdown.Both);
                current.Close();
                _clientSockets.Remove(current);
                Console.WriteLine("Client disconnected");
                return;
            }
            else
            {
                //Console.WriteLine("Text is an invalid request");
                byte[] data = Encoding.ASCII.GetBytes("Invalid function request");
                current.Send(data);
                //Console.WriteLine("Warning Sent");
            }

            current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current);
        }
    }
}
4

1 回答 1

3

由于多个线程可以同时访问您的字典,因此您需要手动序列化对它的访问(使用lock语句)或使用 ConcurrentDictionary。一般来说,后者会提供更好的性能。您可以将字典声明为您的类的成员并从函数中Program插入到其中。AcceptCallback

您的 _clientSockets 列表也在线程之间共享,因此它也需要一些同步。一旦您的字典正常工作,也许您可​​以完全摆脱它。

于 2013-10-11T00:26:05.253 回答