繁体   English   中英

套接字C#线程化

[英]Sockets C# Threading

我有这个问题,我不知道为什么它不起作用。 它是服务器客户端代码。

这是客户端

   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);
        }

    }

这是服务器端

 public void startClient()
    {
        this.clientSocket.Connect(this.serverIp, this.serverPort);
        Thread sending = new Thread(new ThreadStart(firstsend));

        byte[] bytess = new byte[800 * 600 * 3];

        while (true)
        {
            if (!Threadwork.IsConnected(this.clientSocket))
                break;
            this.read(bytess);
            Thread.Sleep(100);
        }
        this.clientSocket.Close();
     //shutdown > close   
    }
    public void firstsend( )
    {
       // this.clientSocket.Send(GetBytes(Form1.t1.Text));
        CPacket Clientpacket= new CPacket(Form1.t1.Text);
        while (true)
        {
           if (!IsConnected(clientSocket))
             break;
            if (possibleMessage != "")
            {
                Clientpacket.MessageFromClient = possibleMessage;
                possibleMessage = "";
            }
            BinaryFormatter b = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            b.Serialize(ms, Clientpacket);
            this.clientSocket.Send(ms.ToArray());
            Thread.Sleep(100);   
        }
    }

问题是客户端无法接收任何内容,因此服务器也可以帮助我。 我试图调试它,问题肯定出在线程中,如果您建议我如何使其工作,我会非常感谢。

您没有启动线程。.调用waitforMessage.Start()方法

暂无
暂无

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

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