Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C+创建UDP服务器+;嵌入跨平台iOS和Android应用程序_Android_C++_Ios_C++11_Cocos2d X - Fatal编程技术网

使用C+创建UDP服务器+;嵌入跨平台iOS和Android应用程序

使用C+创建UDP服务器+;嵌入跨平台iOS和Android应用程序,android,c++,ios,c++11,cocos2d-x,Android,C++,Ios,C++11,Cocos2d X,我正在使用cocos2d-x开发一款跨平台的手机游戏(iOS和Android)。 我的代码大部分是用C++编写的,在ObjtoVC/C/java/SWIFT中使用OS指定的代码。 我想知道有没有人用任何C++库来在他们的应用程序中托管一个UDP服务器? 编辑:到目前为止,我已经发现了许多平台特定的解决方案(使用java for Android,以及COOCASYNC等),但是没有什么特别的C++应用于跨平台的应用程序。 编辑:我更喜欢没有增压的解决方案。最好包括一些简单的内容,比如向项目中添加几

我正在使用cocos2d-x开发一款跨平台的手机游戏(iOS和Android)。 我的代码大部分是用C++编写的,在ObjtoVC/C/java/SWIFT中使用OS指定的代码。 我想知道有没有人用任何C++库来在他们的应用程序中托管一个UDP服务器?

<>编辑:到目前为止,我已经发现了许多平台特定的解决方案(使用java for Android,以及COOCASYNC等),但是没有什么特别的C++应用于跨平台的应用程序。
编辑:我更喜欢没有增压的解决方案。最好包括一些简单的内容,比如向项目中添加几个文件。

您最有可能使用Valve的GameNetworkingSocket

它们的外部依赖性非常有限,因此您应该能够为iOS和Android编译它们


您还可以查看以下列表:,其中有一个库列表,您可以选择尝试。

您可以使用该库。它与可用的库相同,但不需要任何其他boost库。我在这种类型的项目(Android/iOS)中使用过,它是迄今为止最优秀的解决方案。

以下是我的结论:

h文件:

#include "Queue.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include <array>
#include <iostream>
#include <thread>

using namespace std;

#define MAXBUFFER_SIZE 1024

class UDPServer {
public:
    /**
     * Constructor
     *
     * @port the port on which the UDP server is listening for packets.
     */
    explicit UDPServer(unsigned short port);

    /**
     * Destructor
     */
    ~UDPServer() = default;

    /**
     * Setup the server.
     */
    void setupServer();

    /**
     * Get a single message.
     * For demonstration purposes, our messages is expected to be a array of int
     */
    bool getMessage(std::array<int, 4>& message);

    bool getIPAddress(std::array<int, 4>& message);

    void setFoundIP();

    bool isReady();

    void nextPort();

    int getPort();

private:
    bool _isBoundToPort = false;
    /**
     * The server port.
     */
    unsigned short port_;
    bool isFoundIP = false;
    /**
     * The thread-safe message queue.
     */
    Queue queue_;
    Queue _ipAddresses;
    /**
     * The UDP server function.
     */
    int UDPServerFunc();
};
#包括“Queue.h”
#包括
#包括
#包括,稍后还会添加一些示例

上面的代码非常简单,有两个解析函数用于提取IP地址,或者一组由四个分号分隔的数字。上面的代码非常简单,可以为自己的自定义消息修改自己


队列。h只是一个简单的线程安全队列。< /P>我相信Boost(谷歌只是IT,它是跨平台C++库)有套接字的版本,UDP和TCP.libuv都是不错的选择。

#include "UDPServer.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

using namespace std;

/**
 * This function parses an incoming message with the following format: 1;234;-89;-53;
 *
 * A valid message consists of 4 integer values separated by semicolons.
 */
inline std::array<int, 4> parseMessage(const std::string& input);
inline std::array<int,4> parseIp(const std::string& input);

UDPServer::UDPServer(unsigned short port)   {
    port_ = port;
}

bool UDPServer::getMessage(std::array<int, 4>& message) {
    return queue_.pop(message);
}

bool UDPServer::getIPAddress(std::array<int, 4>& message) {
    return _ipAddresses.pop(message);
}

void UDPServer::setFoundIP(){
    isFoundIP = true;
}

bool UDPServer::isReady(){
    return _isBoundToPort;
}

void UDPServer::nextPort(){
    port_++;
}

int UDPServer::getPort(){
    return port_;
}

void UDPServer::setupServer() {
    // Launch the server thread.
    std::thread t([this](){
        UDPServerFunc();
    });
    t.detach();
}

int UDPServer::UDPServerFunc() {

    // Creating socket file descriptor
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    // Filling server information
    struct sockaddr_in servaddr, cliaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    memset(&cliaddr, 0, sizeof(cliaddr));
    servaddr.sin_family = AF_INET; // IPv

    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(port_);

    // Bind the socket with the server address
    if (::bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    _isBoundToPort = true;
    while (true)  {
        // Read the next message from the socket.
        char message[MAXBUFFER_SIZE];
        socklen_t len = sizeof(struct sockaddr);
        ssize_t n = recvfrom(sockfd, (char *)&message, MAXBUFFER_SIZE, MSG_DONTWAIT,
                     (struct sockaddr *)&cliaddr, (socklen_t*)&len);
        if (n > 0) {
            message[n] = '\0';
            // Parse incoming data and push the result on the queue.
            // Parsed messages are represented as a std::array<int, 4>.

            if(!isFoundIP){
                _ipAddresses.push(parseIp(message));
            }else{
                queue_.push(parseMessage(message));
            }
        } else {
            // Wait a fraction of a millisecond for the next message.
            usleep(100);
        }
    }

    return 0;
}