2014-03-02 32 views
0

我的遊戲有兩個客戶端&一臺服務器。 client1通過將數據放置在服務器& client2從服務器檢索數據發送到client2。同樣,client2通過服務器將數據發送到client1。從網絡接收無關數據使用tcp套接字第二次調用網絡

當我從Client1發送數據到client2時,一切正常,但是當client2發送數據給client1時,它收到不相關的數據。

我相信什麼是第一通信過程中,無論數據是在服務器不清除&因此而Clien2將數據發送回客戶端1,數據與服務器上的數據&將不相關的數據重疊。

我對所有客戶端使用相同的代碼,但客戶端代碼的多個實例。

下面是我使用的代碼: -

 private void OnKeyDown1(object sender, KeyEventArgs e) 
     { 
     tbxno = 1; 
     if (e.Key == Key.Enter || e.Key == Key.Return) 
     { 
      // Disable further edit on textbox 
      playerTbx1.IsReadOnly = true; 

      // Compare answers entered by user 
      char[] letters = new char[playerTbx1.Text.Length]; 
      StringReader sr = new StringReader(playerTbx1.Text); 
      sr.Read(letters, 0, playerTbx1.Text.Length); 
      int c = 0; 
      Console.WriteLine(word[i - 1]); 
      string label = MainWordLabel.Content.ToString(); 

      while (label.Contains(letters[c].ToString())) 
      { 
       c++; 
       if (c == letters.Length) 
        break; 
      } 
      int count = c; 
      MessageBox.Show("No. of words matching : " + c); 
      // Score calculation 
      score += c * 5; 
      PlayerScoreTbx.Text = score.ToString(); 

      //Send data to server 
      byte[] buffer = System.Text.Encoding.ASCII.GetBytes(playerTbx1.Text); 

      byte[] length = new byte[2]; 
      length[0] = (byte)(playerTbx1.Text.Length % 256); 
      length[1] = (byte)(playerTbx1.Text.Length/256); 


      byte[] usernameBuffer = System.Text.Encoding.ASCII.GetBytes(Username_Textbox.Text); 

      byte[] unamelength = new byte[2]; 
      unamelength[0] = (byte)(Username_Textbox.Text.Length % 256); 
      unamelength[1] = (byte)(Username_Textbox.Text.Length/256); 

      byte[] scoreBuffer = System.Text.Encoding.ASCII.GetBytes(PlayerScoreTbx.Text); 

      byte[] scoreLength = new byte[2]; 
      scoreLength[0] = (byte)(PlayerScoreTbx.Text.Length % 256); 
      scoreLength[1] = (byte)(PlayerScoreTbx.Text.Length/256); 

      byte[] tno = new byte[1]; 
      tno[0] = (byte)tbxno; 

      tcpClient.Client.Send(length); 
      tcpClient.Client.Send(buffer); 
      tcpClient.Client.Send(unamelength); 
      tcpClient.Client.Send(usernameBuffer); 
      tcpClient.Client.Send(scoreLength); 
      tcpClient.Client.Send(scoreBuffer); 
      tcpClient.Client.Send(tno); 
     } 
    } 

而recieving數據從服務器返回: -

無效的GetData() {

 while (tcpsocket.Connected) 
     { 

      NetworkStream stream = tcpClient.GetStream(); 

      byte[] userName = new byte[1024]; 
      byte[] buffer = new byte[256]; 
      byte[] length = new byte[2]; 
      byte[] userlength = new byte[2]; 
      byte[] scoreBuffer = new byte[1024]; 
      byte[] scoreLength = new byte[2]; 

      // read length of username 
      int readUserlength = stream.Read(userlength, 0, 2); 
      int userLength = userlength[0] + (userlength[1] * 256); 

      // read username 
      stream.Read(userName, 0, userLength); 
      string userReceived = System.Text.Encoding.ASCII.GetString(userName, 0, userLength); 

      // read length of score 
      int readUScore = stream.Read(scoreLength, 0, 2); 
      int lengthOfScore = scoreLength[0] + (scoreLength[1] * 256); 

      // read score 
      stream.Read(scoreBuffer, 0, lengthOfScore); 
      string score = System.Text.Encoding.ASCII.GetString(scoreBuffer, 0, lengthOfScore); 

      // check if the same client is not receiving data 
      this.Dispatcher.Invoke(() => 
      { 
       if (userReceived.Equals(Username_Textbox.Text) == false) 
       { 
        int readlength = stream.Read(length, 0, 2); 
        int dataLength = length[0] + (length[1] * 256); 
        stream.Read(buffer, 0, dataLength); 
        string data = System.Text.Encoding.ASCII.GetString(buffer,0,dataLength); 

        byte[] tbox = new byte[1]; 
        stream.Read(tbox, 0, 1); 


        int count = tbox[0]; 

        this.Dispatcher.Invoke(() => 
        { 
         OppTbx1.Text = data; 
         OppScoreTbx.Text = score; 
        }); 


       } 
      }); 

     } 
    } 

代碼服務器上: -

public delegate void斷開連接(ClientManager項目);

public delegate void MessageReceived(string item, string username, string score, byte textbox); 

public class ClientManager 
{ 
    public Socket socket { get; set; } 
    private NetworkStream networkStream; 

    public event Disconnected OnDisconnected; 
    public event MessageReceived OnMessage; 


    public ClientManager(Socket clientSocket) 
    { 
     this.socket = clientSocket; 
     this.networkStream = new NetworkStream(this.socket); 
     Task.Factory.StartNew(() => 
     { 
      while (socket.Connected) 
      { 
       // TextBox Data 
       byte[] dataBuffer = new byte[1024]; 
       byte[] length = new byte[2]; 
       int readlength = this.networkStream.Read(length, 0, 2); 
       if (readlength == 0) 
        break; 
       int dataLength = length[0] + (length[1] * 256); 
       int readDataBytes = this.networkStream.Read(dataBuffer, 0, dataLength); 
       if (readDataBytes == 0) 
        break; 
       string data = System.Text.Encoding.ASCII.GetString(dataBuffer, 0, dataLength); 

       // Username Information 
       byte[] username = new byte[1024]; 

       byte[] userlength = new byte[2]; 
       int readLengthbytes = this.networkStream.Read(userlength, 0, 2); 
       if (readLengthbytes == 0) 
        break; 
       int userLength = userlength[0] + (userlength[1] * 256); 

       int readUsername = this.networkStream.Read(username, 0, userLength); 
       if (readUsername == 0) 
        break; 

       string usernameString = System.Text.Encoding.ASCII.GetString(username, 0, userLength); 

       // score 

       byte[] scoreBuffer = new byte[1024]; 

       byte[] scorLength = new byte[2]; 
       int readscorelength = this.networkStream.Read(scorLength, 0, 2); 
       if (readscorelength == 0) 
        break; 
       int lenghtOfScore = scorLength[0] + (scorLength[1] * 256); 

       int readscore = this.networkStream.Read(scoreBuffer, 0, lenghtOfScore); 
       if (readscore == 0) 
        break; 

       string score = System.Text.Encoding.ASCII.GetString(scoreBuffer, 0, lenghtOfScore); 

       byte[] tbx = new byte[1]; 

       this.networkStream.Read(tbx,0,1); 

       //send data,sername & score 
       OnMessage(data, usernameString, score, tbx[0]); 


      } 

      //socket.Disconnect(false); 
      if (OnDisconnected != null) 
       OnDisconnected(this); 
     }); 
    } 
} 

}

發送到客戶機回: -

空隙SendChatMessage(字符串消息,用戶名字符串,字符串的分數,字節TBOX) { 字節[]數據= System.Text.Encoding .ASCII.GetBytes(消息); byte [] length = new byte [2]; length [0] =(byte)(message.Length%256); length [1] =(byte)(message.Length/256);

 byte[] usernameBuffer = System.Text.Encoding.ASCII.GetBytes(username); 
     byte[] userlength = new byte[2]; 
     userlength[0] = (byte)(username.Length % 256); 
     userlength[1] = (byte)(username.Length/256); 

     byte[] scoreBuffer = System.Text.Encoding.ASCII.GetBytes(score); 
     byte[] scoreLength = new byte[2]; 
     scoreLength[0] = (byte)(score.Length % 256); 
     scoreLength[1] = (byte)(score.Length/256); 

     byte[] tbxCount = new byte[1]; 
     tbxCount[0] = tbox; 
     Task.Factory.StartNew(() => 
     { 
      lock (tcpClients) 
      { 
       foreach (var item in tcpClients) 
       { 
        try 
        { 
         item.socket.Send(userlength); 
         item.socket.Send(usernameBuffer); 
         item.socket.Send(scoreLength); 
         item.socket.Send(scoreBuffer); 
         item.socket.Send(length); 
         item.socket.Send(data); 
         item.socket.Send(tbxCount); 

        } 
        catch (Exception ex) 
        { 
         ex.GetBaseException(); 
        } 


       } 
      } 

     }); 
    } 


} 

回答

0

正如invarably看到這樣的問題,你是無視Read()返回的計數和假設讀填充的緩衝器。沒有指定這麼做,或者在非阻塞模式下傳輸多於一個字節:零字節。你需要循環,直到你有所有的數據預期或需要。