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