为什么这个线程没有被执行?

时间:2014-09-27 03:39:53

标签: c# sockets network-programming client

这是我试过的服务器程序,但它不提供输出。 Service()方法未执行。我该怎么办?我使用过C#语言。

    IPAddress hostIP;
    String hostName;
    Int16 serverPort;

    private static void Service()
    {
        string frmsg = "Hi there client!!";
        Console.WriteLine("Child thread starts SERVICE..."); 
        Console.Write("{0}", frmsg);
    }

    public static void Main(string[] args)
    {
        Socket MS_socket,CS_socket;
        EndPoint serverEP;
        IPAddress hostIP;

        serverEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
        MS_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        MS_socket.Bind(serverEP);
        MS_socket.Listen(0);
    }

然后我相信这部分代码有问题。

  while (true)
  {
            // creating a connection socket.and accepting incoming connection request
            CS_socket = MS_socket.Accept();
            // starts a new thread to process the request
            ThreadStart childref = new ThreadStart(Service);
            Console.WriteLine("In Main: Creating the Child thread");
            // will invoke service method through thread.
  }

此代码中的问题在哪里?

Thread childThread = new Thread(childref);

启动线程......

childThread.Start();
Console.ReadKey();

有一个我无法弄清楚的错误。我没有输出。

0 个答案:

没有答案