socket如何异步接收数据?
想和服务器进行通信,现在的问题我是我可以向服务器发送请求,但是收不到服务器的返回数据,下面是代码,还请好心人给分析下怎么修改。程序代码:
public bool CheckVersion(string comNum) //查看终端版本信息 { try { IPAddress address = null; if (IPAddress.TryParse(userIpAddr.Trim(), out address)) { if (sockCenter != null) { sockCenter.Close(); } EndPoint remoteEP = new IPEndPoint(address, userPort); sockCenter = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sockCenter.Connect(remoteEP); } if((this.sockCenter != null) && this.sockCenter.Connected) { sockCenter.BeginSend(temp, 0, temp.Length, SocketFlags.None, null, null); return true; } } catch (Exception) { return false; } } public void GetVersion() { if (threadCenter != null) { threadCenter.Abort(); } threadCenter = new Thread(new ThreadStart(ReadCenterData)); threadCenter.IsBackground = true; threadCenter.Start(); } public void ReadCenterData() { int num = 0; byte[] buffer = new byte[0xc800]; //51200 //sockCenter.Accept(); try { while (true) { num = sockCenter.Receive(buffer, 0, sockCenter.Available, SocketFlags.None); //接收指定的字节数,参数3 if (num == 0) { num = sockCenter.Receive(buffer, 0, sockCenter.Available, SocketFlags.None); if (num == 0) { if (sockCenter != null) { sockCenter.Close(); } return; } } } } catch { if (sockCenter != null) { sockCenter.Close(); } } }发送正常,就是receive的时候,sockCenter.Available一直为空,要怎么修改呢?