Windows Phone 用のアプリケーションを作成しており、サーバーと通信してデータを送信する必要があります。SERVER は C++ で書かれており、変更できません。クライアントは私が書かなければならないものです。サーバーは、クライアントがサーバーに接続してデータを送信するように設計されています。接続はすべての送信に対して開いたままです。C# でコードを記述することにより、サーバーからデータを受信できますが、最初の受信後、バッファーで読み取ったデータは常に同じです。したがって、新しいデータを受信できるように、入力バッファーをフラッシュする方法が必要です (データは継続的に送信されます)。ここで定義されているクラスを使用しています:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202858%28v=vs.105%29.aspx
どうもありがとう !!
SocketClient.cs での受信にこのコードを使用しました。
public string Receive()
{
string response = "Operation Timeout";
// We are receiving over an established socket connection
if (_socket != null)
{
// Create SocketAsyncEventArgs context object
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
// Setup the buffer to receive the data
socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
// Inline event handler for the Completed event.
// Note: This even handler was implemented inline in order to make
// this method self-contained.
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
// *********************************************
// THIS part of the code was added to receive
// a vector of 3 double
Double[] OdomD = new Double[3];
for (int i = 0; i < 3; i++)
{
OdomD[i] = BitConverter.ToDouble(e.Buffer, 8 * i);
}
// *********************************************
}
else
{
response = e.SocketError.ToString();
}
_clientDone.Set();
});
// Sets the state of the event to nonsignaled, causing threads to block
_clientDone.Reset();
// Make an asynchronous Receive request over the socket
_socket.ReceiveAsync(socketEventArg);
// Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
// If no response comes back within this time then proceed
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
else
{
response = "Socket is not initialized";
}
return response;
}
Connect() メソッドは、上記のリンクで報告されているものとまったく同じです。したがって、アプリケーションが起動すると、Connect() メソッドが次のように呼び出されます。
SocketClient client = new SocketClient();
// Attempt to connect to server for receiving data
Log(String.Format("Connecting to server '{0}' over port {1} (data) ...", txtRemoteHost.Text, 4444), true);
result = client.Connect(txtRemoteHost.Text, 4444);
Log(result, false);
これは最初に 1 回だけ行われ、その後、毎秒更新される 3 つの double の配列を受け取る必要があります。だから私は使用します:
Log("Requesting Receive ...", true);
result = client.Receive();
Log(result, false);
問題は、コードをデバッグして Receive() 内で実行を停止した場合も、サーバーから送信された最初の値である同じ値を常に読み取ることです。私が期待しているのは、client.Receive() を呼び出すたびに新しい値を取得することですが、これは追加されません。
Matlab環境で同じクライアントを作成することで、同様の問題が発生しました。入力バッファを読み取る前に関数 flushinput(t) を使用して問題を解決しました。このようにして、サーバーから送信された最後のデータを常に読み取ることができました。私はそれに似た機能を探しています..
入力バッファーのサイズは、受信する予定のデータと同じに固定されています。その場合、24 バイト ( 3* sizeof(double) ) ..
ありがとうございました!!