我实际上不知所措,我现在有这个问题已经有一段时间了。我搜索了Stackoverflow并找不到解决方案。
问题如下:我没有收到任何数据,并且从来自Socket(多播)的BeginReceive或BeginReceiveFrom的回调从未被调用,并且数据永远不会被处理,我似乎无法找到原因。我无法看到森林中的树木:(
如果我逐步调试应用程序,我有时会收到数据,有时则不会。如果我在没有断点的情况下进行调试而没有“一步一步”它就永远无法工作。
当我用wireshark嗅探组播地址时,我可以看到包裹到达,我知道服务器每500毫秒发送一次包。
这是UnitTest程序和套接字初始化,连接和接收的代码。
也许有人可以帮助我找到错误或指出我正确的方向......
这是我初始化套接字的方式:
public void Initialize()
{
IPAddress multicastIP = IPAddress.Parse(mcastConfig.RemoteIPAddress);
IPAddress localIP = IPAddress.Parse(mcastConfig.LocalIPAddress);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint localEP = new IPEndPoint(localIP, mcastConfig.RemotePort);
_socket.Bind(localEP);
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 5);
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)localIP.Address);
multicastEndPoint = new IPEndPoint(multicastIP, mcastConfig.RemotePort);
MulticastOption mcastOption = new MulticastOption(multicastIP, localIP);
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption);
Connect();
}
AsyncConnect工作正常,它看起来像这样:
public void Connect()
{
if (!Connected)
{
_socket.BeginConnect(multicastEndPoint, new AsyncCallback(ConnectCallback), _socket);
connectDone.WaitOne();
Connected = true;
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
connectDone.Set();
}
catch (Exception e)
{
_log.Error("### Error with the Connection: {0}", e.ToString());
}
}
问题从BeginReceive开始,它被调用一次,但没有任何反应,只有在套接字关闭时才调用回调本身(因为它应该是,已经在这里读过):
public void Receive()
{
byte[] data = new byte[_buffer.Length];
if (!Connected)
{
Connect();
}
try
{
mcastEP = multicastEndPoint;
IPAddress localIP = IPAddress.Parse(mcastConfig.LocalIPAddress);
EndPoint localEP = new IPEndPoint(localIP, mcastConfig.RemotePort);
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), _socket);
//_socket.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref localEP, new AsyncCallback(ReceiveCallback), _socket);
Array.Copy(_buffer, data, _buffer.Length);
pDUHelper.ProcessData(data); //This processes the data and sends an event if the data was correct received
}
catch (SocketException se)
{
_log.Error("### SocketException ErrorCode: {0}, see: https://support.microsoft.com/en-us/help/819124/windows-sockets-error-codes-values-and-meanings", se.ErrorCode.ToString());
}
catch(Exception e)
{
_log.Error(e);
}
}
private void ReceiveCallback(IAsyncResult ar)
{
Socket socket = ar.AsyncState as Socket;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = ipep;
try
{
int reicevedBytes = _socket.EndReceive(ar);
//int reicevedBytes = _socket.EndReceiveFrom(ar, ref ep);
ipep = ep as IPEndPoint;
if(reicevedBytes > 0)
{
byte[] data = new byte[reicevedBytes];
Array.Copy(_buffer, data, reicevedBytes);
pDUHelper.ProcessData(data); //This processes the data and sends an event if the data was correct received
receiveDone.Set();
}
}
catch (ObjectDisposedException ode)
{
_log.Error(ode);
}
catch (SocketException se)
{
_log.Error(se);
}
catch(Exception e)
{
_log.Error(e);
}
finally
{
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), _socket);
//_socket.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveCallback), _socket);
}
}
我的单元测试计划
class Program
{
static ConcurrentQueue<CallListLine> _workerQueue = new ConcurrentQueue();
static void Main(string[] args)
{
Console.WriteLine("Create Configuration");
ConfigurationHelper.Configuration.LoadOrCreateConfiguration();
_workerQueue.AddProvider("Testsystem");
MulticastConfiguration mc = new MulticastConfiguration
{
LocalIPAddress = "192.168.0.15",
LocalPort = 0,
RemoteIPAddress = "233.233.233.233",
RemotePort = 43238,
MaxSegmentSize = 200,
ReceiveBufferSize = 8192,
SocketBufferSize = 64000,
IsConfigured = true
};
Console.WriteLine(mc);
MulticastReceiver mr = new MulticastReceiver(mc);
Console.WriteLine("Init");
mr.Initialize();
Console.WriteLine("Creating PDU Helper");
mr.NewCallListReady += CallListReadyToProcess;
mr.Receive();
int counter = 0;
while (true)
{
Console.WriteLine("Messages for Testsystem: {0}, Counter: {1}", _workerQueue.Count, counter);
Thread.Sleep(1000);
if (counter == 20)
break;
counter++;
}
Console.WriteLine("Press any Key to End!");
Console.ReadLine();
mr.Stop();
}
static void CallListReadyToProcess(object sender, EventArgs e)
{
MTPEventArgs test = e as MTPEventArgs;
Console.WriteLine("Received new Line: {0}", test.CallList);
CallListLine tempLine = new CallListLine
{
Provider = "Testsystem",
Data = test.CallList,
TimeStamp = DateTimeOffset.Now
};
_workerQueue.Enqueue(tempLine);
}
}