简体   繁体   English

多线程服务器

[英]Multi Threaded Server

I've implemented a server, and am now attempting to make it multi threaded however have come across a problem in that the server fails when data is received and that the StreamWriter cannot read the data from the Stream . 我已经实现了服务器,现在尝试使其成为多线程服务器,但是遇到一个问题,即服务器在接收数据时发生故障,并且StreamWriter无法从Stream读取数据。

The code is as follows: 代码如下:

class whereisServer
{
    static Socket connection;
    static object threadLock = new object();

    static void Main(string[] args)
    {
        Thread t = new Thread(new ThreadStart(Run));
        t.Start();
    }

    static void Run()
    {
        NetworkStream socketStream;
        TcpListener listener;
        const int PORT = 43;
        try
        {
            listener = new TcpListener(IPAddress.Any, PORT);
            listener.Start();
            while (true)
            {
                connection = listener.AcceptSocket();
                socketStream = new NetworkStream(connection);
                try
                {
                    connection.ReceiveTimeout = 1000;
                    connection.SendTimeout = 1000;
                    Thread t = new Thread(new ParameterizedThreadStart(DoRequest));
                    t.Start(socketStream);
                    //DoRequest(socketStream);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message + "\nTimeOut Handled");
                }
                finally
                {
                    socketStream.Close();
                    connection.Close();
                }
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine(e.Message);
        }
    }

    static void DoRequest(object stream)
    {
        NetworkStream socketStream = (NetworkStream)stream;
        StreamWriter writer = new StreamWriter(socketStream);
        StreamReader reader = new StreamReader(socketStream);

        lock (threadLock)
        {
             //DO SERVER STUFF HERE
        }

When calling the DoRequest method without threads the program runs fine, however when commenting that out and calling the threaded version this is when the problems occur. 在没有线程的情况下调用DoRequest方法时,程序可以正常运行,但是在注释掉并调用线程版本时才出现问题。

I would be grateful if someone could suggest as to why the Stream is unable to function. 如果有人可以提出关于Stream无法运行的原因,我将不胜感激。

In multithreaded case you call the Close methods sooner than DoRequest does its work. 在多线程情况下,您比DoRequest更快地调用Close方法。

It is because these two lines are in finally block. 这是因为这两行最后处于阻塞状态。

socketStream.Close();
connection.Close();

These two lines are executed immediately after the Start method starts the new thread, but before that thread completes its work in DoRequest . Start方法启动新线程之后,但在该线程完成其在DoRequest工作之前,立即执行这两行。

I would recommend you to call these methods inside DoRequest . 我建议您在DoRequest调用这些方法。 You can pass connection object into DoRequest , then create socketStream internally in DoRequest and when you are finished, close both these objects before finishing the DoRequest method. 您可以通过connection对象为DoRequest ,然后创建socketStream在内部DoRequest ,当你完成后,关闭既完成之前,这些对象DoRequest方法。 (Or you can use any other solution, this is just one option.) (或者您可以使用任何其他解决方案,这只是一个选择。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM