1

我是 c# 编程的新手...我试图制作一个服务器/多个客户端,但我有这个错误,我不知道如何解决它。如果有人知道,请帮忙。这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      TcpListener serverSocket = new TcpListener(1000);
      TcpClient clientSocket = default (TcpClient);
      int counter = 0;
      serverSocket.Start();
      Console.WriteLine(" >> " + "Server Started");
      counter = 0;
      while (true) {
        counter += 1;
        clientSocket = serverSocket.AcceptTcpClient();
        Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
        handleClinet client = new handleClinet();
        client.startClient(clientSocket, Convert.ToString(counter));
      }
      clientSocket.Close();
      serverSocket.Stop();
      Console.WriteLine(" >> " + "exit");
      Console.ReadLine();
    }
  }
  public class handleClinet {
    TcpClient clientSocket;
    string clNo;
    public void startClient(TcpClient inClientSocket, string clineNo) {
      this.clientSocket = inClientSocket;
      this.clNo = clineNo;
      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);
          serverResponse = "Server to clinet(" + clNo + ") " + rCount;
          sendBytes = Encoding.ASCII.GetBytes(serverResponse);
          networkStream.Write(sendBytes, 0, sendBytes.Length);
          networkStream.Flush();
          Console.WriteLine(" >> " + serverResponse);
        } catch (Exception ex) {
          Console.WriteLine(" >> " + ex.ToString());
        }
      }
    }
  }
}

错误在这一行:

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

非常感谢:D

4

0 回答 0