C# socket异步通信请求被重复执行的问题 10
本人做的一个socket服务,已经运行半年多,之前没有出现错误的情况,单单昨天出现了一笔离奇的交易记录,客户端请求操作记录是一条,但服务端的监听记录是两条,所以也就执行了...
本人做的一个socket服务,已经运行半年多,之前没有出现错误的情况,单单昨天出现了一笔离奇的交易记录,客户端请求操作记录是一条,但服务端的监听记录是两条,所以也就执行了两次,两条请求的间隔相差0.247秒。问题很纠结,差了两天的资料也没什么收获,各位大侠请指教,在线等.....
服务端代码如下
Socket _listener;
Thread _acceptWorkThread;
byte[] Xmlbuffer = new byte[65535];
byte[] BackBuffer = new byte[65535];
XmlDocument bxml = new XmlDocument();
public void ServiceStart(int SERVER_END_POINT_PORT)
{
if (_listener != null) return;
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry hostEntry = Dns.GetHostByName(Dns.GetHostName());
IPAddress address = hostEntry.AddressList[0];
IPEndPoint localEP = new IPEndPoint(address, SERVER_END_POINT_PORT);
_listener.Bind(localEP); _listener.Listen(100);
_acceptWorkThread = new Thread(AcceptWorkThread);
_acceptWorkThread.Start();
}
private void AcceptWorkThread()
{
while (true)
{
Socket ServiceSocket = _listener.Accept();
ServiceSocket.BeginReceive(Xmlbuffer, 0, (int)Xmlbuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), ServiceSocket);
}
}
private void ReceiveCallBack(IAsyncResult ar)
{
Socket csocket = (Socket)ar.AsyncState;
int endi = csocket.EndReceive(ar);
csocket.SendTimeout = 60000;
if (csocket.Connected)
{
string ReceviceMsg = Encoding.UTF8.GetString(Xmlbuffer, 0, endi);
if (ReceviceMsg.Length > 0)
{
string SendMsg = GetReturnValue(ReceviceMsg);
byte[] bytes = Encoding.UTF8.GetBytes(SendMsg);
csocket.Send(bytes, 0, bytes.Length, 0);
}
}
//关闭客户端Sockte对象
csocket.Shutdown(SocketShutdown.Both);
csocket.Close();
}
注:服务器是虚拟机 展开
服务端代码如下
Socket _listener;
Thread _acceptWorkThread;
byte[] Xmlbuffer = new byte[65535];
byte[] BackBuffer = new byte[65535];
XmlDocument bxml = new XmlDocument();
public void ServiceStart(int SERVER_END_POINT_PORT)
{
if (_listener != null) return;
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry hostEntry = Dns.GetHostByName(Dns.GetHostName());
IPAddress address = hostEntry.AddressList[0];
IPEndPoint localEP = new IPEndPoint(address, SERVER_END_POINT_PORT);
_listener.Bind(localEP); _listener.Listen(100);
_acceptWorkThread = new Thread(AcceptWorkThread);
_acceptWorkThread.Start();
}
private void AcceptWorkThread()
{
while (true)
{
Socket ServiceSocket = _listener.Accept();
ServiceSocket.BeginReceive(Xmlbuffer, 0, (int)Xmlbuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), ServiceSocket);
}
}
private void ReceiveCallBack(IAsyncResult ar)
{
Socket csocket = (Socket)ar.AsyncState;
int endi = csocket.EndReceive(ar);
csocket.SendTimeout = 60000;
if (csocket.Connected)
{
string ReceviceMsg = Encoding.UTF8.GetString(Xmlbuffer, 0, endi);
if (ReceviceMsg.Length > 0)
{
string SendMsg = GetReturnValue(ReceviceMsg);
byte[] bytes = Encoding.UTF8.GetBytes(SendMsg);
csocket.Send(bytes, 0, bytes.Length, 0);
}
}
//关闭客户端Sockte对象
csocket.Shutdown(SocketShutdown.Both);
csocket.Close();
}
注:服务器是虚拟机 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询