在异步socket开发开发中,参数传递问题(BeginReceive参数传递)
先上代码:辅助类
public class Packet
{
public Packet ( Socket client, Socket current, string szDatas )
{
this.clientSocket = client;
this.currentSocket = current;
this.strValue = szDatas;
}
public System.Net.Sockets.Socket clientSocket;
public byte [] dataBuffer = new byte [1024];
public System.Net.Sockets.Socket currentSocket;
public string strValue = string.Empty;
}
public void OnDataReceived(IAsyncResult ar)
{
Socket m_clientSocket = new Socket ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
ipEnd = new IPEndPoint ( IPAddress.Parse ( BasicConfig.Server ), BasicConfig.Port );
try
{
//连接到Server端
m_clientSocket.Connect ( ipEnd );
if ( m_clientSocket.Connected )
{
//发送到Server
m_clientSocket.Send ( BasicConfig.Analyzer.AnalyzerBuffer ( strValue ) );
Packet packet = new Packet ( m_clientSocket, socketData.m_currentSocket, szData );
//接收返回消息
WaitForReceived ( m_clientSocket, socketData.m_currentSocket, strTelegram );
}
}
catch ( SocketException ex )
{
//log
}
}
public void WaitForReceived ( Socket clientSocket, Socket currentSocket, string strValues )
{
try
{
if ( m_pfnCallBack == null )
{
m_pfnCallBack = new AsyncCallback ( OnClientDataReceived );
}
Packet theSocPkt = new Packet ( clientSocket, currentSocket, strValues );
m_result = clientSocket.BeginReceive ( theSocPkt.dataBuffer,
0, theSocPkt.dataBuffer.Length,
SocketFlags.None,
m_pfnCallBack,
theSocPkt );
}
catch ( SocketException se )
{
LoggerFactory.Logger.ErrorFormat ( "WaitForData連接發生錯誤:{0}", se.ErrorCode );
}
}
public void OnClientDataReceived ( IAsyncResult asyn )
{
Packet theSockId = ( Packet ) asyn.AsyncState;
try
{
int iRx = theSockId.clientSocket.EndReceive ( asyn );
char [] chars = new char [iRx + 1];
System.Text.Decoder d = BasicConfig.Analyzer.GetDecoder ( );
int charLen = d.GetChars ( theSockId.dataBuffer, 0, iRx, chars, 0 );
System.String szData = new System.String ( chars );
//这儿如果取出theSockId.strValue
doSome(theSockId.strValue);
}
catch ( SocketException se )
{
LoggerFactory.Logger.ErrorFormat ( "WaitForData連接發生錯誤:{0}", se.ErrorCode );
}
}
大致描述如下:
1> 该程序既是一个Server,它接收Client段发送过来的消息,在OnDataReceived中接收.
2> 接收后组合消息,发送到Server端,并等待消息返回
问题:
1> 如果在比较短的时间内发送两笔消息,如:110|1111,110|1112
2> OnDataReceived会执行两次,//接收返回消息
WaitForReceived ( m_clientSocket, socketData.m_currentSocket, strTelegram );
也会执行两次
3> 在OnClientDataReceived 中,如果110|1111这笔消息的消息还没有返回,此时110|1112也会进入
到OnClientDataReceived中,这时theSockId.strValue的值却为110|1111,而不是110|1112,
不知道什么原因,还请前辈高手帮忙解答,非常感谢.
4> 另外,想请问下异步回调,是否会出现如上面那样,比如:
A ---> 发出异步CallBack,传参数A
B ---> 发出异步CallBack,传参数B
有不有可能出现在B的异步回调中,捞到的资料确实A呢? --------------------编程问答-------------------- 看一下两次调用WaitForReceived的时候,strTelegram的值是否有问题。 --------------------编程问答-------------------- 有跟踪调试,在传的时候没问题,调试的时候发现问题是在第二次OnClientDataReceived执行的时候,
消息变为了第一次传到Packet中的值了. --------------------编程问答-------------------- 应该异步操作问题
参考
参考 --------------------编程问答-------------------- lg --------------------编程问答-------------------- 4> 另外,想请问下异步回调,是否会出现如上面那样,比如:
A ---> 发出异步CallBack,传参数A
B ---> 发出异步CallBack,传参数B
有不有可能出现在B的异步回调中,捞到的资料确实A呢?
这个 需要 通讯规约 来控制, 需要有 发送->确认 , 如果没有确认-> 重发. socket 只负责 数据 发送 接受数据, 至于 数据如何处理,防止 丢失, 需要你的 通讯 规约来保证, 建议 楼主 找一本 101规约 看看
补充:.NET技术 , C#