套接字C#线程

时间:2015-06-23 18:57:03

标签: c# multithreading sockets

我有这个问题,我无法弄清楚为什么它不起作用。 它是一个服务器客户端代码。

这是客户端

0

这是服务器端

   public void starter()
    {

       String message = read();
        sp.todoaddlist(message);
        Thread waitforMessage = new Thread(new ThreadStart(CollectInfo));

        send("hello");



        communication.Close();
        //this.server.closeThread(this.seq);//CHECK IF WORKS 
    }

   private String read()
    {
        byte [] recived = new byte[1024];




         communication.Receive(recived);
         MemoryStream mms = new MemoryStream(recived);
         BinaryFormatter b = new BinaryFormatter();


        //cPacketa.
         String fill = (b.Deserialize(mms) as CPacket).textUserName;

        return fill;


    }

  private void CollectInfo()
    {
        BinaryFormatter b = new BinaryFormatter();
        byte[] bytym = new byte[1024];
        while (true)
        {
            if (!IsConnected(communication))
                break;
            communication.Receive(bytym);
            MemoryStream ms = new MemoryStream(bytym);
            CPacket clientInfo = b.Deserialize(ms) as CPacket;
            if (clientInfo.MessageFromClient != "")
            {
                this.sp.SetMessage(clientInfo.textUserName + ":" + clientInfo.MessageFromClient);
            }
            Thread.Sleep(100);
        }

    }

问题是客户端会回收任何东西,服务器也是如此 请帮帮我。 我试图调试它,如果你建议我如何使它工作,问题肯定在线程我会apriciate 谢谢

1 个答案:

答案 0 :(得分:0)

你没有开始这个帖子.. 调用waitforMessage.Start()方法