有关UDP数据包传输和C ++的问题 [英] Questions on UDP Packet Transfers and C++

查看:197
本文介绍了有关UDP数据包传输和C ++的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code Project社区,



我正在尝试使用UDP,但我有以下问题:



1)我正在尝试构建一个函数,它将收到一个简单的UDP数据包,其字符串如/ location 7 5,并解析浮点值7和5并将其存储到数组中。有没有关于如何做到这一点的例子?



2)尝试从https://code.google.com/p/oscpack/使用OSCPacket几个小时后,我无法解决此处显示的任何编译器错误: http://i.tylian.net/untitloxo.png



如错误消息所示,错误来自OSCPack,而不是我的代码。是否有人熟悉这个或者是否有更好的方法来实现UDP数据包传输?



3)我只使用ws32.dll库,但还有其他吗?我应该使用的图书馆除了那个?



随意回答部分或全部的多部分问题,如果需要更多详细信息,请告诉我!另外,我的目标平台是Windows 7 64位。



非常感谢你的时间!

解决方案

WinSock库提供所有必需的功能。没有必要使用其他包。请参阅Web中有关如何设置UDP侦听器的大量WinSock常见问题解答和教程之一。设置监听器后,只需调用 WSARecvFrom()



问题是你不要知道何时以及是否有数据进入。所以你应该使用一个工作线程来接收使用重叠的IO(如果 WSARecvFrom()正在等待,等待IO事件并调用 GetOverlappedResult())。但这也由常见问题解答和教程处理。



另一个问题是你的UDP数据包长度可能不同。通常的做法是使用某种标识数据类型和大小的标题。但是对于短字符串,你应该只读一遍。


按编号解答你的问题:

1.这里有很多例子。互联网,只是谷歌UDP C ++示例,你会得到大量的点击。基本上你只是设置你的套接字并且只是开始监听(UDP实际上没有握手过程),每个数据包都是一组字节(数据报),你可以把这些字节转换成一个结构将其分解为解释数据。



2.您可以使用解决方案1中所述的Windows套接字,也可以使用第三方库。通常第三方库只是试图使过程更容易或提供跨平台套接字。无法帮助你使用这个特定的库但是看错误,你可能有某种设置问题(不包括你需要的文件或类似的东西)。



3. MSDN告诉你需要知道的一切。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v = vs.85).aspx [ ^ ]

Hello Code Project Community,

I am trying to use UDP, but I have the following questions:

1) I am trying to build a function that would recieve a simple UDP packet with a string like "/location 7 5" and parse out the float value 7 and 5 out and store it into an array. Are there any examples on how to do this?

2) After trying to use OSCPacket from https://code.google.com/p/oscpack/ for hours, I cannot get past any compiler errors shown here: http://i.tylian.net/untitloxo.png

As the error message suggests, the error comes from the OSCPack and not my code. Is anyone familiar with this or is there a better method to implement UDP packet transfers?

3) I am only using the ws32.dll library, but are there any other libraries I should be using besides that?

Feel free to respond to some or all of the multipart question and let me know if more detail is needed! Also, my target platform is Windows 7 64-bit.

Thank you very much for your time!

解决方案

The WinSock library provides all required functions. There is no need to use other packages. See one of the plenty WinSock FAQs and Tutorials in the web on how to setup an UDP listener. After setting up the listener, just call WSARecvFrom().

The problem is that you don''t know when and if data are coming in. So you should use a worker thread for receiving using overlapped IO (if WSARecvFrom() is pending, wait for the IO event and call GetOverlappedResult()). But this is also handled by the FAQs and tutorials.

Another problem is that your UDP packets may vary in their length. It is common practice to use some kind of header that identifies the type and size of data. But with short strings, you should get them with one read.


Addressed your questions by number:
1. There''s a ton of examples of this on the internet, just google "UDP C++ example" and you''ll get a ton of hits. Basically you just set up your socket and just start listening (UDP doesn''t really have a handshake process), every packet is a set of bytes (a datagram), it''s up to you to cast these bytes onto a structure that breaks it out into interpreted data.

2. You can use the Windows sockets as described in Solution 1 or you can use a third-party library. Usually third party libraries just try to make the process easier or provide cross-platform sockets. Can''t help you with this particular library but looking at the error, you probably have some sort of setup problem (didn''t include files you needed or something similar).

3. MSDN tells you everything you need to know.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740673(v=vs.85).aspx[^]


这篇关于有关UDP数据包传输和C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆