2017-10-10 123 views
0

我試圖與XMPP通過TCP網絡套接字(StreamSocket)(Jabber的)服務器進行通信,我用下面的代碼讀什麼服務器已經發送到我讀XML:UWP C#從StreamSocket

StreamSocket tcpSocket; 
StreamReader reader; 
int BUFFER_SIZE = 4096; 

// Connecting to a remote XMPP server .... 

reader = new StreamReader(tcpSocket.InputStream.AsStreamForRead()); 
string result; 
while (true) 
{ 
    result = ""; 
    while (true) 
    { 
     char[] buffer = new char[BUFFER_SIZE]; 
     await reader.ReadAsync(buffer, 0, BUFFER_SIZE); 
     string data = new string(buffer); 

     // Detecting if all elements in the buffer array got replaced => there is more to read 
     if (data.IndexOf("\0") >= 0 || reader.EndOfStream) 
     { 
      result + data.Substring(0, data.IndexOf("\0")); 
      break; 
     } 
     result += data; 
    } 
    Debug.WriteLine(result); 
} 

我的代碼適用於長度爲< 4096個字符的字符串,但只要字符串獲取的時間超過4096個字符,它就會失敗(不會檢測到消息結束)。它會等待,直到它收到一個新的字符串< 4096個字符,連接兩個字符串並將它們返回爲一個字符串。

有沒有辦法獲得一個字符串的實際長度並連續讀取它們?

回答

0

我終於想通了讀長消息: 我要用戶reader.Peek()。 。

/// <summary> 
/// How many characters should get read at once max. 
/// </summary> 
private static readonly int BUFFER_SIZE = 4096; 

private StreamSocket socket; 
private StreamWriter writer; 
private StreamReader reader; 

protected async Task<string> readNextStringAsync() 
{ 
    string result = ""; 
    while (true) 
    { 
     char[] buffer = new char[BUFFER_SIZE + 1]; 

     // Read next BUFFER_SIZE chars: 
     reader.ReadAsync(buffer, 0, BUFFER_SIZE); 

     // Cut the read string and remove everything starting from the first "\0": 
     string data = new string(buffer).Substring(0, buffer.Length - 1); 
     int index = data.IndexOf("\0"); 
     if (index < 0) 
     { 
      index = data.Length; 
     } 
     result += data.Substring(0, index); 

     // Is data left to read? 
     if (reader.Peek() < 0) 
     { 
      // No? then break: 
      break; 
     } 
    } 
    return result; 
} 
0

您已將4096設置爲BUFFER_SIZE,並將其設置爲StreamReader.ReadAsync和char陣列中的count參數。當字符串包含超過4096個字符時,它將失敗。

您應該能夠獲得流中的實際長度,我們可以使用Stream.Length以字節爲單位獲取流的長度。 Array的最後一個字符是「\ 0」。當你創建char數組時,你應該能夠設置Stream.Length加一個char數組。

例如:

StreamSocket socket; 
StreamSocket tcpSocket; 
StreamReader reader; 

reader = new StreamReader(tcpSocket.InputStream.AsStreamForRead()); 
var BUFFER_SIZE=(int)(tcpSocket.InputStream.AsStreamForRead()).Length; 
string result; 
while (true) 
{ 
    result = ""; 
    while (true) 
    { 
     char[] buffer = new char[BUFFER_SIZE+1]; 
     await reader.ReadAsync(buffer, 0, BUFFER_SIZE); 
     string data = new string(buffer); 
     if (data.IndexOf("\0") >= 0 || reader.EndOfStream) 
     { 
      result = data.Substring(0, data.IndexOf("\0")); 
      break; 
     } 
     result += data; 
    } 
    Debug.WriteLine(result); 
} 

如果你想讀取從當前位置到流的末尾所有字符,你可以使用StreamReader.ReadToEndStreamReader.ReadToEndAsync方法。

+0

感謝您的幫助,但如果我使用'BUFFER_SIZE =(INT)(tcpSocket.InputStream.AsStreamForRead())長;',我得到一個System.NotSupportedException:該流不支持長度屬性因爲它不是可尋求的。 如果我使用'await reader.readToEndAsync()'它不會返回任何東西 - 它只會等待 – COM8

+0

你可以通過'tcpSocket.InputStream.AsStreamForRead()'方法獲取流嗎? –

+0

是的,它返回一個流對象,但是''''''''''對象不適用於它返回的這種流。 – COM8