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-method 中交换了一些部分 - 这是一个应该完成这项工作的版本:

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% 确定您要做什么(我认为您希望您的文本文件逐行发送到您的终端) - 所以您可能需要在这里和那里更改一些位。

不知道代码中的 Stream-messes 来自哪里,但我猜您尝试了各种教程/片段并忘记清理;)

于 2012-04-05T13:11:31.310 回答