TCP 클라이언트에 문제가 있습니다.이 서비스는 서버에 연결하여 수신 및 처리 된 메시지를 수신합니다. 약간의 디버깅 후에 나는 "데이터의 전체 패킷을 읽을 수 없습니다!"라는 것을 발견했습니다. 오류 (코드)C# TCP 클라이언트에서 비동기 메시지 처리 중 오류가 발생했습니다.
저는 서버가 보내는 것을 제어 할 수 없습니다. 내 생각에 전체 메시지를받지 못했습니다 (나는 더 긴 메시지에서는 실패하지만 볼륨으로 인해 불확실하다고 생각합니다).
내가 이것을 고려할 수있는 가장 좋은 방법은 무엇입니까? 또는 다른 도움말이 있습니까? 나는 모든 행동과 문제가있는 곳에 나의받는 방법을 가지고있다.
나는 너무 두려워하지 말고,이 TCP 내 첫 번째 시도입니다 죄송
public void Receive(IAsyncResult ar)
{
if (ar != null)
{
try
{
byte[] mLenBytes = (byte[])ar.AsyncState;
byte[] mDataBytes = null;
int bytes_read = mTcpClient.Client.EndReceive(ar);
if (bytes_read != 4)
throw new Exception("Unable to read whole header!");
int len = mLenBytes[3] + ((mLenBytes[2] + (mLenBytes[1] + (mLenBytes[0] << 8)) << 8) << 8);
mDataBytes = new byte[len];
bytes_read = mTcpClient.Client.Receive(mDataBytes, len, SocketFlags.None);
if (bytes_read != len)
throw new Exception("Unable to read whole packet of data!" + "Expected " + len + " Got " + Convert.ToString(bytes_read) + "\r\n" + (System.Text.Encoding.ASCII.GetString(mDataBytes)));
//This is the error that is raised!.
// raise an event
PhoneBoxEventArgs e1 = new PhoneBoxEventArgs(System.Text.Encoding.UTF8.GetString(mDataBytes));
Console.WriteLine("Data received is = " + e1.Data);
OnPassEvent(e1);
}
catch (Exception ex)
{
}
}
byte[] mLenBytes_new = new byte[4];
mTcpClient.Client.BeginReceive(mLenBytes_new, 0, 4, SocketFlags.None, new AsyncCallback(Receive), mLenBytes_new);
}
예 머리글을 얻고 총계를받지 못했지만 시도를 계속합니다. 팁 주셔서 감사. – user685590