简体   繁体   English

UDP数据包传输和C ++

[英]UDP Packet Transfers and C++

Hello Stackoverflow Community, 您好Stackoverflow社区,

I have the following questions when trying to implement UDP transfers using C++: 尝试使用C ++进行UDP传输时,我遇到以下问题:

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. 1)我正在尝试构建一个函数,该函数将使用字符串“ / location 7 5”接收一个简单的UDP数据包,并解析出float值7和5并将其存储到数组中。 Are there any examples on how to do this? 是否有有关如何执行此操作的示例?

2) After trying to use OSCPacket from https://code.google.com/p/oscpack/ for hours, but I cannot get past any compiler errors shown here: http://i.tylian.net/untitloxo.png 2)尝试使用https://code.google.com/p/oscpack/中的 OSCPacket数小时后,但我无法摆脱此处显示的任何编译器错误: http : //i.tylian.net/untitloxo.png

As the error message suggests, the error comes from the OSCPack and not my code. 如错误消息所示,错误来自OSCPack,而不是我的代码。 Is anyone familiar with this or is there a better method to implement UDP packet transfers? 有谁熟悉此方法,还是有更好的方法来实现UDP数据包传输?

3) I am only using the ws32.dll library, but are there any other libraries I should be using besides that? 3)我只使用ws32.dll库,但是除此之外,还有其他我应该使用的库吗?

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. 另外,我的目标平台是Windows 7 64位。

Thank you very much for your time! 非常感谢您的宝贵时间!

Here are some of the codes I done with OSCPacket: 以下是我使用OSCPacket完成的一些代码:

includes: 包括:

#include <iostream>
#include <cstring>

#if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug
 namespace std {using ::__strcmp__; }  // avoid error: E2316 '__strcmp__' is not a member of 'std'.
#endif

#include "osc/OscReceivedElements.h"
#include "osc/OscPacketListener.h"
#include "ip/UdpSocket.h"

#define PORT 7000

packet listener 数据包监听器

float* coordinateFromOSC = (float*) malloc (2);
class PacketListener : public osc::OscPacketListener {
protected:

    virtual void ProcessMessage( const osc::ReceivedMessage& m, 
                const IpEndpointName& remoteEndpoint )
    {
        (void) remoteEndpoint; // suppress unused parameter warning

        try{
            //the return value, array of 2, [0] for xValue, [1] for yValue
    //will be stored at global variable coordinateFromOSC

    // Trying to parse the packet. osc::OsckPacketListener
            if( strcmp( m.AddressPattern(), "/location" ) == 0 ){
                // Streaming input as argument
                osc::ReceivedMessageArgumentStream args = m.ArgumentStream();
                float xValue, yValue;
                args >> xValue >> yValue >> osc::EndMessage;

            coordinateFromOSC[0] = xValue;
            coordinateFromOSC[1] = yValue;
            }

        }catch( osc::Exception& e ){
            // any parsing errors such as unexpected argument types, or 
            // missing arguments get thrown as exceptions.
            std::cout << "Invalid format: "
                << m.AddressPattern() << ": " << e.what() << "\n";
        }
    }
};

and lastly in the function where I need the stored values 最后在需要存储值的函数中

PacketListener listener;
UdpListeningReceiveSocket s(IpEndpointName( IpEndpointName::ANY_ADDRESS, PORT ), &listener );

If you continue to have issues with your current library, you might want to take a look at Boost and the ASIO library. 如果您当前的库仍然存在问题,则可能需要看一下Boost和ASIO库。

This page UDP Echo Server Example might help you get started. 此页面UDP Echo Server示例可能会帮助您入门。

To parse strings in C++, try the stream operators (aside: does your input actually need to be strings?): 要解析C ++中的字符串,请尝试使用流运算符(除了:您的输入实际上是否需要为字符串?):

std::string str("/location 7 5");
std::stringstream ss(str);
std::string cmd;
float x, y;
ss >> cmd >> x >> y;

For OSCPacket, it's possible that you have not set up the project settings properly. 对于OSCPacket,可能您没有正确设置项目设置。 Why not just use Winsock? 为什么不只使用Winsock?

For an example of using Winsock, see http://www.adp-gmbh.ch/win/misc/sockets.html 有关使用Winsock的示例,请参见http://www.adp-gmbh.ch/win/misc/sockets.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM