【问题标题】:Server Problems C#服务器问题 C#
【发布时间】:2016-04-20 14:12:35
【问题描述】:

我想用 C# 创建一个简单的客户端-服务器聊天。 我的问题是:当我尝试将客户端连接到服务器时。我收到了这个错误:

Chat Server Started ....
Unhandled exception: System.ArgumentOutOfRangeException: Specified argument out of range.
Parameter name: size
   in System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   in ConsoleApplication1.Program.Main(String[] args) in D:\Visual Studio\Chat Client-Server\ConsoleApplication1\Program.cs:line 57

P.s 我把错误翻译成英文,可能会有错误。

第 57 行:networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

完整的程序:

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

namespace ConsoleApplication1

{

class Program

{

    public static Hashtable clientsList = new Hashtable();



    static void Main(string[] args)

    {
        IPAddress ipAd = IPAddress.Parse("192.168.1.7");

        TcpListener serverSocket = new TcpListener(ipAd, 8001);

        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];

            string dataFromClient = null;



            NetworkStream networkStream = clientSocket.GetStream();

            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.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);



                Program.broadcast(dataFromClient, clNo, true);

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

        }//end while

    }//end doChat

} //end class handleClinet

}//end namespace

我该如何解决这个问题?提前致谢。

【问题讨论】:

    标签: c# visual-studio client-server


    【解决方案1】:

    你可以改变

    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
    

    networkStream.Read(bytesFrom, 0, 10025);
    

    匹配声明数组的大小。

    【讨论】:

    • 谢谢,现在我遇到了一个新错误。 System.InvalidOperationException:未连接的套接字上不允许操作。在 System.Net.Sockets.TcpClient.GetStream() 中 ConsoleApplication1.handleClinet.doChat() 在 D:\Visual Studio\Chat Client-Server\Consoleapplication1\Program.cs:line 206
    • 第 206 行:NetworkStream networkStream = clientSocket.GetStream(); P.s 我也改变了第 208 行,等于第 57 行
    • 在调用 GetStream() 之前,您需要在 clientSocket 上调用 Connect()。
    • 我添加了它,但是: System.Net.Sockets.SocketException (0x80004005): 连接请求转发到 System.Net.Sockets.TcpClient.Connect 中已连接的套接字(字符串主机名,Int32 端口) 在 D:\Visual Studio\Chat Client-Server\ConsoleApplication1\Program.cs:line 205 中的 ConsoleApplication1.handleClinet.doChat() 中
    猜你喜欢
    • 2016-09-16
    • 2018-07-18
    • 2016-03-30
    • 2011-11-23
    • 2014-08-06
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多