私が取り組んでいる小さなプロジェクトのために、ネットワーク経由でオブジェクトをシリアル化することに取り組んでいます。私が使用しているコードはほとんど問題なく動作します。バイトが正しく読み込まれないことがありますが、UI をリロードするだけで修正されます。ただし、同じシステムで通信しているときに、別のシステムに移動するとすぐに、コードは部分的にしか機能しません。ネットワーク経由でサイズ 30 の文字列配列を送信しようとするとすぐに、サーバーは受信パックの報告を停止し、奇妙なことに一貫してエラーをスローするものは何もありません。ときどきポップアップするエラーがいくつかあります。
System.InvalidOperationException: Cannot block a call on this socket while an earlier asynchronous call is in progress.
at System.Net.Sockets.Socket.ValidateBlockingMode()
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode)
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.Socket.Receive(Byte[] buffer)
at DMAssist.AsynchronousClient.Receive(Socket client) in C:\Users\crud4\documents\visual studio 2015\Projects\DMAssist\DMAssist\asyncsclient.cs:line 160
at DMAssist.AsynchronousClient.StartClient(Object obj, Boolean expectresponse) in C:\Users\crud4\documents\visual studio 2015\Projects\DMAssist\DMAssist\asyncsclient.cs:line 119
これは、コンソールに表示される主なエラーであり、対処方法がわかりません。私はネットワークプログラミングの経験があまりないので、どこが間違っているのか頭を悩ませています。
私の主な懸念は、何らかの理由で、クライアントが 330 バイトを超えて送信しようとすると、サーバーが受信バイトの報告を停止し、全体的なグリッチ動作を引き起こすことです。サーバーは確実に接続を受信していることを示していますが、シンプルは何らかの理由ですぐにそれを強制終了します。何が起こっているのか、またはそれを修正する方法を知っている人がいれば、助けていただければ幸いです!
前もって感謝します!
クライアント:
private netObject Receive(Socket client)
{
byte[] buffer = new byte[1024];
int iRx = client.Receive(buffer);
object rawbytes = ByteArrayToObject(buffer);
netObject recvobject = (netObject)rawbytes;
receiveDone.Set();
return recvobject;
}
public netObject StartClient(Object obj, bool expectresponse)
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 25599);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, ObjectToByteArray(obj));
sendDone.WaitOne();
Send(client, Encoding.ASCII.GetBytes("<EOF>"));
sendDone.WaitOne();
// Receive the response from the remote device.
if (expectresponse)
{
netObject serverdata = Receive(client);
receiveDone.WaitOne();
return serverdata;
}
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
return new netObject();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return new netObject();
}
}
private static void Send(Socket client, byte[] data)
{
byte[] byteData = data;
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
サーバ:
public void ReadCallback(IAsyncResult ar)
{
try {
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
Console.WriteLine(bytesRead);
if (bytesRead > 0)
{
if (bytesRead != 5)
{
Array.Copy(state.buffer, state.cleanbuffer, bytesRead);
}
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
Console.WriteLine("SERVER: Recieved " + bytesRead + " bytes");
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
/*Console.WriteLine("Read {0} bytes from socket. Data : {1}",
content.Length, content);*/
object rawbytes = ByteArrayToObject(state.cleanbuffer);
netObject recvobject = (netObject)rawbytes;
}
// Echo the data back to the client.
Send(handler, ObjectToByteArray((object)recvobject));
}
else {
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
} catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
private void Send(Socket handler, byte[] data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = data;
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}