2015-06-23 86 views
0

我有这个问题,我不知道为什么它不工作。其服务器客户端代码为 。套接字C#线程

这是客户端

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

的问题是,客户端行的事recive任何东西,所以是服务器 请帮助我。 我试图调试它,问题肯定是在线程我会apriciate如果你会建议我如何使它工作 谢谢

回答

0

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

+0

我觉得很蠢谢谢你! –