在简单的 C# tcp client/server 程序中,我发送了 5 个字节,服务器看到超过 400,000 个。怎么了?

In simple C# tcp client/server program, I send 5 bytes, server sees over 400,000. What is happening?

我正在尝试制定一个简单的 tcp client/server 的基础知识,以便我可以实现一个几乎可以在任何地方工作并允许 "daemon" 之类的应用程序进行通信的通知系统。其中一个客户端可能是一个监视器,它查看消息并显示所有重置的状态并协调它们,例如通过暂停一个或多个,查看已处理的文件数量,数据库的状态等. 首先,我必须有一个非常简单的服务器来等待来自客户端的消息并将它们广播给其他客户端。这是我目前在服务器上的尝试(基于 http://csharp.net-informations.com/communications/csharp-chat-server.htm 中的示例)。

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

namespace erc.bre
{
    class NotificationServer
    {
        public static Hashtable clientsList = new Hashtable();

        static void Main(string[] args)
        {
            System.Net.IPAddress addr = System.Net.IPAddress.Parse("127.0.0.1");
            TcpListener serverSocket = new TcpListener(addr, 1025);
            TcpClient clientSocket = default(TcpClient);
            int counter = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            counter = 0;
            while ((true))
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                byte[] bytesFrom = new byte[10025];
                clientSocket.ReceiveBufferSize = bytesFrom.Length; // added
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
             //   Console.WriteLine("Bytes received: {0}", (int)clientSocket.ReceiveBufferSize);
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                broadcast(dataFromClient + " Joined ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " Joined chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }

        public static void broadcast(string msg, string uName, bool flag)
        {
            foreach (DictionaryEntry Item in clientsList)
            {
                TcpClient broadcastSocket;
                broadcastSocket = (TcpClient)Item.Value;
                NetworkStream broadcastStream = broadcastSocket.GetStream();
                Byte[] broadcastBytes = null;

                if (flag == true)
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
                }
                else
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(msg);
                }

                broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
                broadcastStream.Flush();
            }
        }  //end broadcast function
    }//end Main class


    public class handleClinet
    {
        TcpClient clientSocket;
        string clNo;
        Hashtable clientsList;

        public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
        {
            this.clientSocket = inClientSocket;
            this.clientSocket.ReceiveBufferSize = 10025; // added
            this.clNo = clineNo;
            this.clientsList = cList;
            Thread ctThread = new Thread(doChat);
            ctThread.Start();
        }

        private void doChat()
        {
            int requestCount = 0;
            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;
            Byte[] sendBytes = null;
            string serverResponse = null;
            string rCount = null;
            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                    rCount = Convert.ToString(requestCount);

                    NotificationServer.broadcast(dataFromClient, clNo, true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }//end while
        }//end doChat
    } //end class handleClinet
}//end namespace

您会注意到 Console.WriteLine 我在其中打印出接收到的字节数。我添加了这个,因为我在下一个声明中遇到了大小超出范围的异常。它打印出一个数字,如 408,000 字节。

这是客户端程序,修改了很多,因为在我的 Mac VisualStudio 2017 版本中,我无法使用 Windows Forms 获得任何东西,甚至无法根据例子。因为我打算在搞定机制后将代码放入 Web 应用程序中,所以我只是想让客户端输入他们的 ID,然后输入一行,让服务器接收并重新广播。其他客户端将从命令行执行相同的操作(每个新客户端一个)。它目前获取 userId 并发送它。我发送 'John',它似乎以 5 个字节发送 'John$'。

using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace erc.bre
{
    public class NotificationClient
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);
        string readData = null;
        string display = "";
        string userName = "";
        string message = "";
        bool needsInvocation = true;
        static NotificationClient nc = new NotificationClient();

        public NotificationClient()
        {
        }

        public static int Main(string[] args)
        {
            nc.msg();
            return 0;
        }

        private void sendUserJoined()
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }

        private void InputReceived()
        {
            readData = "Conected to Chat Server ...";
            msg();
            clientSocket.Connect("127.0.0.1", 1025);
            serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(userName + "$");
            Console.WriteLine("Sending {0} bytes as '{1}'", outStream.Length, System.Text.Encoding.Default.GetString(outStream);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            Thread ctThread = new Thread(getMessage);
            ctThread.Start();
        }

        private void getMessage()
        {
            while (true)
            {
                serverStream = clientSocket.GetStream();
                int buffSize = 0;
                byte[] inStream = new byte[10025];
                buffSize = clientSocket.ReceiveBufferSize;
                serverStream.Read(inStream, 0, buffSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        }

        private void msg()
        {
            if (needsInvocation)
            {
                needsInvocation = false;
                Console.Out.WriteLine("Starting Client");
                Console.Out.WriteLine("Input your callerID: ");
                nc.userName = nc.ReadLine();
                nc.InputReceived();
                nc.sendUserJoined();
                nc.getMessage();
                return;
            }
            display = display + Environment.NewLine + " >> " + readData;
            Console.Out.WriteLine(display);
        }

        private string ReadLine()
        {
            string line = "";
            char ch;
            Console.WriteLine("Enter text when desired, 'enter' to end");
            do
            {
                int x = Console.Read();
                try
                {
                    ch = Convert.ToChar(x);
                    if (ch == 0x0a)
                    {
                        return line;
                    }
                }
                catch (OverflowException e)
                {
                    Console.WriteLine("{0} Value read = {1}.", e.Message, x);
                    ch = ' ';
                }
                line += ch;
            } while (ch != 0x0a);
            return line;
        }

    }
}

服务器控制台显示:

聊天服务器已启动.... 收到的字节数:408300

未处理的异常:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:尺寸 在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小) 在 ConsoleApplication1.NotificationServer.Main(String[] args) 在 /Users/woo/Projects/erc-caml/bre/NotificationServer/NotificationServer.cs:line 33 bash: 第 1 行:45360 中止陷阱:6 "/usr/local/share/dotnet/dotnet" "/Users/woo/Projects/erc-caml/bre/NotificationServer/bin/Debug/netcoreapp2.1/NotificationServer.dll"

客户端控制台显示:

启动客户端 输入您的来电显示: 在需要时输入文本,'enter' 结束 约翰

Conected to Chat Server ... Sending 5 bytes as 'John$'

未处理Exception:Unhandled异常:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 参数名称:尺寸 在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小) 在 /Users/woo/Projects/erc-caml/bre/NotificationMonitor/NotificationClient.cs: 第 60 行中的 erc.bre.NotificationClient.getMessage() 在 /Users/woo/Projects/erc-caml/bre/NotificationMonitor/NotificationClient.cs: 第 77 行

中的 erc.bre.NotificationClient.msg()

为什么服务器看到超过 400,000 字节?它在等待消息时是否在内部吞噬东西?顺便说一句,不管我等多久,数字都是一样的。

clientSocket.ReceiveBufferSize

Returns 接收缓冲区的大小与接收到的字节数不一致。使用网络流 (serverStream) 的 Read 方法并检查其结果,它告诉您已读取了多少字节。

有关详细信息,请参阅 NetworkStream.Read(Byte[], Int32, Int32) Method and TcpClient.GetStream