2015-05-16 63 views
0

我正在開發一個客戶端服務器應用程序,其中客戶端位於Android中,服務器位於C#中。在使用C#作爲服務器之前,我使用了autoIT,除了必須發送9個字符的消息之外,它還行得通,否則會得到奇怪的符號。服務器端無法正確讀取消息

現在在C#中我遇到了同樣的問題。我嘗試刪除所有空白區域,但它始終在消息的開始處留下了一些東西。

單字的消息,如:

"SendClose" 

服務器讀取,

"  SendClose" or " SendClose" if i remove the white spaces 

注意開頭的空白。這很有趣,因爲當我檢查字符串長度時,它說它只有11個字符,所以我不知道發生了什麼;也許2個選項卡?

這裏是我的C#代碼:

using System; 
using System.Text; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 

public class TcpListenerSample { 
    static void Main(string[] args) { 
    try { 
     // set the TcpListener on port 13000 
     int port = 80; 
     IPAddress localAddr = IPAddress.Parse("192.168.1.68"); 
     TcpListener server = new TcpListener(localAddr, port); 

     // Start listening for client requests 
     server.Start(); 


     // Buffer for reading data 
     byte[] bytes = new byte[1024]; 
     string data; 

     //Enter the listening loop 
     while (true) { 
     Console.Write("Waiting for a connection... "); 

     // Perform a blocking call to accept requests. 
     // You could also user server.AcceptSocket() here. 
     TcpClient client = server.AcceptTcpClient(); 
     Console.WriteLine("Connected!"); 

     // Get a stream object for reading and writing 
     NetworkStream stream = client.GetStream(); 

     int i; 

     // Loop to receive all the data sent by the client. 
     i = stream.Read(bytes, 0, bytes.Length); 

     while (i != 0) { 
      // Translate data bytes to a ASCII string. 
      data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); 
      Console.WriteLine(String.Format("Received: {0}", data)); 

      // Process the data sent by the client. 
      data = data.ToUpper(); 

      byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); 

      // Send back a response. 
      stream.Write(msg, 0, msg.Length); 
      Console.WriteLine(String.Format("Sent: {0}", data)); 

      i = stream.Read(bytes, 0, bytes.Length); 

     } 

     // Shutdown and end connection 
     client.Close(); 
     } 
    } 
    catch (SocketException e) { 
     Console.WriteLine("SocketException: {0}", e); 
    } 


    Console.WriteLine("Hit enter to continue..."); 
    Console.Read(); 
    } 

} 
+0

你breakpointed和調試,以確保'msg'值是合適的大小?你應該使用斷點來檢查你的對象。 –

+0

你是什麼意思適當的大小? –

+0

那麼msg是11字節嗎?驗證它是否首先具有「SendClose」的文字ASCII表示。 –

回答

-1
 data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); 
     Console.WriteLine(StripExtended(data));   



     static string StripExtended(string arg) 
     { 
      StringBuilder buffer = new StringBuilder(arg.Length); //Max length 
      foreach(char ch in arg) 
      { 
       UInt16 num = Convert.ToUInt16(ch);//In .NET, chars are UTF-16 
       //The basic characters have the same code points as ASCII, and the extended characters are bigger 
       if((num >= 32u) && (num <= 126u)) buffer.Append(ch); 
      } 
      return buffer.ToString(); 
     } 
    } 
} 
+0

來源: http://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string#C.23 –

相關問題