私は2つのWindows UWP Appsを持っています。そのうちの1台(「サーバー」)はWindows IoT(10586.0)のRaspberry Pi 2で動作しています。もう一方(「クライアント」)は、同じネットワーク内のWindows 10デバイス上で実行されています。UWP Appsとのネットワーク接続
私が望むのは、お互いに「話す」ようにすることです。現時点では、単純なStringをクライアントからサーバーに送信したいだけです。その後、シリアル化されたデータはネットワークを介して転送されるべきです。
これは、サーバーアプリケーションのコードです:あなたが見ることができるように
namespace LCARSHomeAutomation
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
try {
EstablishNetworking();
txb_Events.Text += "Server Running";
}catch (Exception ex)
{
txb_Events.Text += ex.Message;
}
}
private async void EstablishNetworking()
{
await StartListener();
}
public async Task StartListener()
{
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnection;
listener.Control.KeepAlive = true;
try
{
await listener.BindServiceNameAsync("5463");
}
catch (Exception ex)
{
if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
//Logs.Add(ex.Message);
txb_Events.Text += ex.Message;
}
}
private async void OnConnection(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
Stream inStream = args.Socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(inStream);
string request = await reader.ReadLineAsync();
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
txb_Events.Text += (String)request;
});
}
private async Task ConnectSocket()
{
StreamSocket socket = new StreamSocket();
socket.Control.KeepAlive = false;
HostName host = new HostName("localhost");
try
{
await socket.ConnectAsync(host, "5463");
Stream streamOut = socket.OutputStream.AsStreamForWrite();
StreamWriter writer = new StreamWriter(streamOut);
string request = "Test Self App \n";
await writer.WriteLineAsync(request);
await writer.FlushAsync();
socket.Dispose();
}
catch (Exception ex)
{
txb_Events.Text += ex.Message;
//Logs.Add(ex.Message)
}
}
private async void btn_Send_Click(object sender, RoutedEventArgs e)
{
await ConnectSocket();
}
}
}
、私は同じホスト上で同じアプリでネットワーク接続を確立し、文字列「テスト自己のApp」をお送りしています。これはかなりの時間が正常に動作しますが、しばらくすると、私はエラーを取得:
Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll
WinRT information: No connection could be made because the target machine actively refused it.
だから、これは私の最初の質問です:何、このエラーがあるとどのように私はこの問題を解決することができますか?
もう1つは:サーバーとクライアントの間にネットワーク接続を確立できません。私は間違っていることを知らない。これは、 "クライアント" のコードです:
namespace LCARSRemote
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void btn_Send_Click(object sender, RoutedEventArgs e)
{
StreamSocket socket = new StreamSocket();
HostName host = new HostName("localhost"); //Replace with coorect hostname when running on RPi
try
{
try {
await socket.ConnectAsync(host, "5463");
}
catch(Exception ex)
{
txb_Events.Text += ex.Message;
}
Stream streamOut = socket.OutputStream.AsStreamForWrite();
StreamWriter writer = new StreamWriter(streamOut);
string request = "Remote App Test";
await writer.WriteLineAsync(request);
await writer.FlushAsync();
socket.Dispose();
}
catch (Exception ex)
{
txb_Events.Text += ex.Message;
//Logs.Add(ex.Message)
}
}
}
}
btn_Sendに私をクリックすると、私は私が間違って何をやっているエラーメッセージ
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
と
A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)
を取得しますか?たぶん私は、ネットワーク接続、ソケットなどをプログラミングする上で比較的新しいと言うべきです。
ありがとうございました!
良い方法かもしれませんしたい場合は、あなたは賢明な何かにホスト名を変更しましたか?ファイアウォールのルールを確認しましたか? –
ファイアウォールもオフにしてみました。私は "127.0.0.1"をホスト名として試しましたが、私も "minwinpc"(LAN内のRPiの名前)を試しましたが、何も動作していないようです。 – drummercrm
あなたのPiのIPアドレスを試しましたか? –