2015-06-23 89 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

我覺得很蠢謝謝你! –