我有一個問題,我嘗試通過套接字讀取一個字符串在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>
你的回答是完全有幫助的。由於某些未知的原因,我不能投票給你。優秀的答案! – PrOgrAMmer