以下代码在IPV4中运行良好,并且,在IPv6(NAT64)环境中,它可以连接服务器并发送消息成功,它可以从服务器捕获数据,但是,回调 OnRead 永远不会被执行。
很长一段时间以来一直困扰着我。
private TcpClient client = null;
private const int MAX_READ = 8192;
private NetworkStream outStream = null;
private byte[] byteBuffer = new byte[MAX_READ];
public void ConnectServerByIP(string ip, int port) {
client = new TcpClient(AddressFamily.InterNetworkV6);
client.SendTimeout = 1000;
client.ReceiveTimeout = 1000;
client.NoDelay = true;
IPAddress ipAddr = IPAddress.Parse(ip);
try {
client.BeginConnect(ipAddr, port, new AsyncCallback(OnConnect), null);
} catch (Exception e) {
Close(); Debug.LogError(e.Message);
}
}
void OnConnect(IAsyncResult asr) {
outStream = client.GetStream();
client.GetStream ().BeginRead (byteBuffer, 0, MAX_READ,new AsyncCallback(OnRead), null);
NetworkManager.AddEvent(Protocal.Connect, new ByteBuffer());
}
void OnRead(IAsyncResult asr) {
Debug.Log ("Hello~");
}