- Home /
android use il2cpp backend,continuous Socket.BeginReceive calls return uncontinuous content。
untiy version: unity 2017.4.11f1 android use il2cpp backend,build apk and run in device(huawei honor).
My game uses tcp socket to communicate message between client and server. The ollowing script codes show the basic socket connect and receive logic.
public class NetConnection { public void Connect(string address, ushort port, int timeout) { try { socket.BeginConnect(address, port, new AsyncCallback(this.HandleConnct), this); } catch (SocketException e) { OnConnectError(e.SocketErrorCode); }
timerConnect.Interval = timeout;
timerConnect.Elapsed += new System.Timers.ElapsedEventHandler(HandleConnectTimeout);
timerConnect.Start();
}
private void HandleConnectTimeout(object obj, System.Timers.ElapsedEventArgs args)
{
timerConnect.Stop();
timerConnect.Elapsed -= HandleConnectTimeout;
OnConnectError(SocketError.TimedOut);
}
private void HandleConnct(IAsyncResult ar)
{
try
{
timerConnect.Stop();
timerConnect.Elapsed -= HandleConnectTimeout;
socket.EndConnect(ar);
PostRecv();
}
catch (SocketException e)
{
OnConnectError(e.SocketErrorCode);
}
}
private void PostRecv()
{
NetPacket p = new NetPacket();
socket.BeginReceive(p.headBuffer.buffer, 0, NetPacket.headerLength, SocketFlags.None, new AsyncCallback(this.HandleHeaderRecv), p);
}
private void HandleHeaderRecv(IAsyncResult ar)
{
try
{
int transferd = socket.EndReceive(ar);
if (transferd == 0)
throw new SocketException((int)SocketError.ConnectionReset);
NetPacket p = ar.AsyncState as NetPacket;
p.headBuffer.length += transferd;
if (p.headBuffer.length < NetPacket.headerLength)
{
socket.BeginReceive(
p.headBuffer.buffer,
p.headBuffer.length,
NetPacket.headerLength - p.headBuffer.length,
SocketFlags.None,
new AsyncCallback(this.HandleHeaderRecv), p);
}
else
{
if (!p.decodeHeader())
{
OnError(SocketError.ProtocolNotSupported);
}
else
{
p.bodyBuffer.Resize(p.payloadLength);
socket.BeginReceive(p.bodyBuffer.buffer, 0, p.payloadLength, SocketFlags.None, new AsyncCallback(this.HandleBodyRecv), p);
}
}
}
catch (SocketException e)
{
OnError(e.SocketErrorCode);
}
}
private void HandleBodyRecv(IAsyncResult ar)
{
try
{
int transferd = socket.EndReceive(ar);
NetPacket p = ar.AsyncState as NetPacket;
p.bodyBuffer.length += transferd;
if (p.bodyBuffer.length < p.payloadLength)
{
socket.BeginReceive(
p.bodyBuffer.buffer,
p.bodyBuffer.length,
p.payloadLength - p.bodyBuffer.length,
SocketFlags.None,
new AsyncCallback(this.HandleBodyRecv), p);
}
else
{
p.decode(seed_);
FinishRecvPacket(p);
}
}
catch (SocketException e)
{
OnError(e.SocketErrorCode);
}
}
private void FinishRecvPacket(NetPacket p)
{
PostRecv();
}
private void OnError(SocketError err)
{
this.Close();
}
private void OnConnectError(SocketError err)
{
this.Close();
}
}
This script works very well when using mono backend. But once switch to il2cpp backend, I encountered this problem sometimes. THE Continuous Socket.BeginReceive calls do not return continuous content! I tried to use wireshark capturing the related TCP net package.After comparing with wireshark captured data and result of beginreceive result, I find out the continuous Socket.beginreceive calls skip some bytes in the net package. The comparing result as followed: Is this a bug of unity android il2cpp or something wrong in my code?
Any hint is appreciated, thx!
Hello crazyToby,
can you please provide some additional information:
$$anonymous$$inimum API Level
Target API Level
Scripting Runtime Version
API compatibility level
ND$$anonymous$$ version
Thank you in advance,
Thom.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Native plugin JNI_OnLoad/UnitySetGraphicsDevice not called when using IL2CPP on Android 0 Answers
Why IL2CPP increases ANR percent ? 1 Answer
Memory usage in Profiler much less than on actual device 1 Answer
Get a fatal error in unity CIL linker 1 Answer
Debug IL2CPP Android build on device 1 Answer