2016-10-11 54 views
0

我有一個問題,我嘗試通過套接字讀取一個字符串在uwp應用程序從一個node.js服務器沒有成功。我得到了一個System.Runtime.InteropServices.COMException,我不知道如何解決它。我設法從uwp客戶端向連接節點服務器發送一個字符串,但我不能做相反的事情。這是我的代碼我做錯了什麼?無法通過Node.js的套接字接收數據到UWP應用程序

這是我的node.js服務器代碼

var _HOST = '192.168.1.67'; 
    var _PORT = 1337; 
    var _address; 

    var net = require('net'); 

    var server = net.createServer(function(socket) { 
     socket.on('connect', (e) => { 
      console.log('client connected ' + 
       socket.remoteAddress + ':' + 
       socket.remotePort); 
     }); 

     socket.on('data', function(data) { 
      console.log('clients says' + ': ' + data); 

      var send = "hello client" 
      console.log('Server says' + ': ' + send); 
      socket.write(send + '\n'); 


     }); 

     socket.on('error', function(data) { 
      console.log('client on error', data); 

     }); 

     socket.on('close', (e) => { 
      console.log('client disconnected'); 
      socket.end; 
     }); 

    }); 

這是我的UWP代碼

public async Task connect() 
     { 


      try 
      { 
       HostName hostName; 

       hostName = new HostName(SocksParameters.Host); 
       socket.Control.NoDelay = false; 
       var cts = new CancellationTokenSource(); 
       cts.CancelAfter(SampleConfiguration.timeout); 

       // Connect to the server 
       var connectAsync = socket.ConnectAsync(hostName, SocksParameters.Port); 
       var connectTask = connectAsync.AsTask(cts.Token); 
       await connectTask; 
      } 
      catch (Exception e) 
      { 
       Debug.WriteLine(e.ToString()); 
       await new MessageDialog("Make sure your Server is open and make sure you follow Instructions To connect localhost").ShowAsync(); 
       Application.Current.Exit(); 
      } 

     } 

public async Task<String> read() 
     { 
      DataReader reader; 
      StringBuilder strBuilder; 



      using (reader = new DataReader(socket.InputStream)) 
      { 

       try 
       { 
        strBuilder = new StringBuilder(); 
        reader.InputStreamOptions = Windows.Storage.Streams.InputStreamOptions.Partial; 

        reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; 
        reader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian; 

        await reader.LoadAsync(256); 
        while (reader.UnconsumedBufferLength > 0) 
        { 
         strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength)); 
         await reader.LoadAsync(256);//System.Runtime.InteropServices.COMException Exception caught 
        } 

        reader.DetachStream(); 
        return strBuilder.ToString(); 
       } 
       catch (Exception e) 
       {  
        return (e.ToString()); 

       } 
      } 

     } 

Activated Event Time Duration Thread 
    Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll ("The I/O operation has been aborted because of either a thread exit or an application request. 

The I/O operation has been aborted because of either a thread exit or an application request. 
") 0.55s  [18700] <No Name> 

回答

1

我測試的代碼,我發現,如果沒有更多的數據可以通過DataReader對象reader來讀取,代碼行await reader.LoadAsync(256);會一直wa它直到從服務器發送新消息並且DataReader可以讀取它。在你的代碼中,如果來自服務器的數據大小小於256,則第二次調用LoadAsync方法可能會導致問題。

await reader.LoadAsync(256); 
while (reader.UnconsumedBufferLength > 0) 
{ 
    strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));  
    await reader.LoadAsync(256); 
     //System.Runtime.InteropServices.COMException Exception caught 
} 

我在這裏提供的解決方法是判斷是否有剩餘的數據可以讀取。更新的代碼如下:

var loadsize = await reader.LoadAsync(256); 
while (loadsize >= 256) 
{ 
    loadsize = await reader.LoadAsync(256); 
} 
if (reader.UnconsumedBufferLength > 0) 
{ 
    strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength)); 
} 

在我看來,好像你可以使用StreamReader這也是您的方案,而不是DataReader工作,因爲它更簡單易用。類似的代碼如下:UWP約StreamSocket

Stream streamIn = socket.InputStream.AsStreamForRead(); 
StreamReader reader = new StreamReader(streamIn); 
string response = await reader.ReadLineAsync(); 
return response; 

更多細節請參考official sample

+0

你的回答是完全有幫助的。由於某些未知的原因,我不能投票給你。優秀的答案! – PrOgrAMmer

相關問題