服务器拒绝接受来自客户端的请求

本文关键字:客户端 请求 拒绝 服务器 | 更新日期: 2023-09-27 18:29:59

这是一个从文件中搜索字符串的程序。客户端所需的字符串是从客户端提供的,在我的情况下,使用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();
     }
 }

服务器拒绝接受来自客户端的请求

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

不知道你代码中的Stream混乱是从哪里来的,但我猜你尝试了各种教程/片段,却忘记了清理;)