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

时间:2012-04-05 05:00:06

标签: c# client-server tcpclient serversocket tcplistener

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

1 个答案:

答案 0 :(得分: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%确定你要做什么(我想你希望你的文本文件逐行发送到你的终端) - 所以你可能不得不在这里和那里改变一些位。 / p>

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