0

ファイルから文字列を検索するプログラムです。クライアントが必要とする文字列は、私の場合はtelnetを使用してクライアント側から提供されます。私が書いたプログラムはサーバーサイドのものです。複数のクライアントを受け入れます。しかし、私が修正できない問題は-

  • ファイルからの文字列はチェックしません。
  • クライアントが接続されるとすぐに、クライアントはその特定のファイルで検索する文字列を入力できなくなります。
  • 応答をクライアントに送り返しません(つまり、文字列がファイルに存在するかどうか)。サーバー側にのみ表示されます。

どうすればさらに先に進むことができますか?誰かが私がどこで間違っているのか教えてもらえますか?誰かがコードを手伝ってくれませんか?これは私のプログラムでの試みです。

class Program
{

    static void Main(string[] args)
    {
        IPAddress ipad = IPAddress.Parse("192.168.0.181");
        TcpListener serversocket = new TcpListener(ipad, 8888);
        TcpClient clientsocket = default(TcpClient);
        Byte[] bytes = new Byte[256];

        serversocket.Start();

        Console.WriteLine(">> Server Started");
        while(true)
        {
            clientsocket = serversocket.AcceptTcpClient();
            Console.WriteLine("Accepted Connection From Client");

            LineMatcher lm = new LineMatcher(clientsocket);
            Thread thread = new Thread(new ThreadStart(lm.Run));
            thread.Start();
            Console.WriteLine("Client connected");
        }


        Console.WriteLine(" >> exit");
        Console.ReadLine();
        clientsocket.Close();
        serversocket.Stop();

    }
}


public class LineMatcher //I've jumbled it up here. Don't know what exactly to do..
{
     public string fileName = "c:/myfile2.txt";
     private TcpClient _client;

     public LineMatcher(TcpClient client)
     {
         _client = client;
     }

     public void Run()
     {
         try
         {
              StreamReader sr = new StreamReader("c:/myfile2.txt");
              using (var reader = new StreamReader(_client.GetStream()))
              {
                  string line ="";
                  int lineNumber = 0;
                  while (null != (line = sr.ReadLine()))
                         {
                             lineNumber += 1;
                             byte[] data = new byte[1024];
                             NetworkStream stream = _client.GetStream();
                             //if (line.Equals(line))
                             for (int ct = stream.Read(data,0, data.Length-1); 0 < ct; ct = stream.Read(data,0,data.Length-1))

                                 line += Encoding.ASCII.GetString(data, 0, ct);
                             line = line.Trim();

                             {
                                 lineNumber.ToString();

                                 data = Encoding.ASCII.GetBytes(line);
                                 _client.Client.Send(data, data.Length, SocketFlags.None);
                                 Console.WriteLine("Line {0} matches {1}", lineNumber, line);
                             }
                         }
             }
         }
         catch (Exception ex)
         {
             Console.Error.WriteLine(ex.ToString());
         }
         Console.WriteLine("Closing client");
         _client.Close();
     }
 }
4

1 に答える 1

1

Runメソッドの一部が交換されたと思います-これがその仕事をするはずのバージョンです:

public void Run()
{
     byte[] data;
     try
     {
        using (var r = new StreamReader("c:/myfile2.txt"))
        {
            string line ="";
            int lineNumber = 0;
            while (null != (line = r.ReadLine()))
            {
                data = Encoding.ASCII.GetBytes(line + "\n");
                _client.Client.Send(data, data.Length, SocketFlags.None);
            }
        }
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine(ex.ToString());
     }
     Console.WriteLine("Closing client");
     _client.Close();
}

何をしようとしているのか100%わからないことに注意してください(テキストファイルをターミナルに1行ずつ送信する必要があると思います)。そのため、あちこちでビットを変更する必要があるかもしれません。

コード内のStream-messesがどこから来たのかわかりませんが、さまざまなチュートリアル/スニペットを試し、クリーンアップするのを忘れたと思います;)

于 2012-04-05T13:11:31.310 に答える