精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
You have a similar problem set to the one that I am currently working. Thank you for posting your code...I learned something from how you display data to a service UI.在我目前的工作中有你发布的类似的问题设置。感谢您发布您的代码。我从中学到如何在服务UI显示数据。
You indicate that you start a new thread to process the inbound data...can you please point out where that thread is generated? 你说明开始一个新的线程来处理入站数据...可以请你指出其中产生的线程么?
I have a similar UDP server running, managing a large number of GPRS/CDMA devices. I too have the general sequence: 我有一个类似的UDP服务器在运行,管理大量的GPRS / CDMA设备。我也有一般的顺序:
// Start listening for incoming data
serverSocket.BeginReceiveFrom(this.dataBuffer, 0, this.dataBuffer.Length, SocketFlags.None, ref senderEndpoint, new AsyncCallback(OnClientDataReceived), senderEndpoint);
On Receive...
// catch all client data into async result, and捕获所有客户端数据到异步结果里,
// client connection into clientEndpoint客户端连接到clientEndpoint
int byteCount = serverSocket.EndReceiveFrom(asyncResult, ref clientEndpoint);
byte[] receiveBuffer = new byte[byteCount];
Array.Copy(this.dataBuffer, 0, receiveBuffer, 0, byteCount);
// PROCESSING GOES HERE
byte[] responseBuffer = ProcessReceivedMessage(receiveBuffer, clientEndpoint);
if (responseBuffer != null)
{
// Send response
serverSocket.BeginSendTo(responseBuffer, 0, responseBuffer.Length, SocketFlags.None, clientEndpoint,
new AsyncCallback(this.OnResponseSent), clientEndpoint);
}
// (catch blocks omitted for brevity)
finally
{
// continue listening
ListenForData();
}
I don't see that the UDP server is really multi-threading - meaning that (in my own case) it appears as though the server is serializing the inbound UDP requests with BeginReceiveFrom() and EndReceiveFrom(). In your experience, what happens if two devices show up at the server at the same time? Does one request get blocked while the first one in gets processed, or should two independent threads be generated?
我没有看到UDP服务器确实用了真正的多线程 - 这意味着(我自己的情况)它出现了,尽管服务器用BeginReceiveFrom()和EndReceiveFrom()序列化入站UDP请求入站UDP请求。以你的经验来看,如果两个设备在同一时间让服务器显示会发生什么?第一个在处理时,下一个请求被阻塞,还是应该产生两个独立的线程?
Hi Fred Barrett, 嗨,弗雷德·巴雷特,
Thanks for your reply. We are using Async programming model here. If you are thinking we are creating each thread for each device then this is not what we are doing here. This practice is very bad which is also provane by many programmers. The BeginRecieve and BeginEnd methods are developed for Asynchronous Threading programming modal. What it does is explain below
感谢您的回复。我们在这里使用异步编程模型。如果你正在考虑为每个设备创建每个线程,那么我们不要这样做。这种做法很不好,它也是由许多程序员使用。BeginRecieve和BeginEnd方法服务于异步线程编程模式。它所做的在下面有解释。
1. The BeginReceive method will start listening. BeginReceive方法将启动监听。Now about below function. The ProcessReceivedMessage method should work very quick. 现在关于下面的功能,ProcessReceivedMessage方法应该工作非常快。
This Function is Entry point of the received data. Now what I have done is to be able to support the multiple devices is I have created one Interface Isession. Using that Isession you can inherit the different types of devices. Like Device type Mobile or any Sensor. The mobile and sensor should inherit the interface which inherit the Isession interface. I put one interface in between because in case of updating individual function I have the men tenability in my hand. Then I have created one list of Isession in Session Manager.
此功能是接收数据的入口点。现在我所做的是能够支持创建一个接口的ISession的多个设备。使用的ISession可以继承不同类型的设备。向设备类型或任何传感器移动。移动和传感器应该继承ISession接口的接口。我把一个接口放在中间,因为在更新单个功能的情况下,我在会话管理器中创建ISession的一个列表。
If call comes from the 3 mobile devices. Within one second. Frist call comes we pass the data in ProcessReceivedMessage method we create Instance of the object which is capable of handling your Mobile class. Generate the request what you want or extract from the mobile and send send the data to devices with the collected endpoint. And start begin receive.
如果调用来自3个移动设备。在一秒钟内。弗里斯特电话打进来,我们通过我们创建一个能够处理你的手机类对象的实例ProcessReceivedMessage方法中的数据。生成你想要的或从移动提取数据的请求,并发送数据到所收集的端点设备。并开始接收。
This is how I manage my server and it works fine. . And it is able to give me plenty of calls per second and I am able to handle that traffic load. 这个模式并发量大。
If the devices are using NAT or in simple Network system to send still no problem. Once they send the call you need to read the bytes and store in the session variable’s data collector. I have also implemented the Endpoint
如果设备使用NAT或简单的网络系统仍然不会有问题。一旦他们发送呼叫,你需要读取字节数和存储数据到会话变量中的数据采集器里。我还实施了端点
6. byte[] responseBuffer = ProcessReceivedMessage(receiveBuffer, clientEndpoint);I hope this will give you the answer of you question .Still I don’t know how your devices works. What are the constraints of your Hardware device. Is it possible for you to use different UDP ports ? if yes then that can also be plus point. My application works with only one port as receiver and sender.
我希望这会给你你问题的答案。不过我不知道你的设备是如何工作的。你的硬件设备的限制是什么。你是否有可能使用不同的UDP端口?如果是的话也可以加点。我的应用程序的工作只有一个端口接收器和发送器。
Note I have not revisited my answer to review in case I have made any mistakes please ignore.
注意:我还没有重新审视我的答案的情况下,审查出我犯的任何错误,请忽略。