简体   繁体   English

SDL_net UDP分组数据

[英]SDL_net UDP Packet data

I'm new to network programming. 我是网络编程的新手。 I've done a little bit with TCP sockets, and I'm trying my had at a simple UDP client(s)/server. 我已经完成了一些TCP套接字,我正在尝试使用简单的UDP客户端/服务器。

I'm using the SDL_net framework for this project. 我正在为这个项目使用SDL_net框架。

The issue I'm running into is the UDPpacket struct uses Uint8 * as the data type for the data. 我遇到的问题是UDPpacket结构使用Uint8 *作为数据的数据类型。 I just want to be able to send straight up text, however. 但我只是希望能够直接发送文本。 I've read a bit on casting, but nothing seems to be compatible, and I'm struggling to find the correct solution to this. 我已经阅读了一些关于铸造的内容,但似乎没有什么兼容,我正在努力找到正确的解决方案。

The code to the client is shown below. 客户端的代码如下所示。 The error, as you would assume,is when I try and set p-data (towards the bottom of the code). 正如您所假设的那样,错误是当我尝试设置p数据时(朝向代码的底部)。

Any advice or corrections you have would be greatly appreciated! 任何建议或更正你将不胜感激!

#include <iostream>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2_net/SDL_net.h>

using namespace std;


int main(int argc, const char * argv[]) {

    UDPsocket sd;

    //create the socket
    if(!(sd = SDLNet_UDP_Open(0))) {
        printf("Could not create socket\n");
        SDLNet_Quit();
        SDL_Quit();
        exit(1);
    }

    //get my address
    IPaddress* myaddress = SDLNet_UDP_GetPeerAddress(sd, -1);
    if(!myaddress) {
        printf("Could not get own port\n");
        exit(2);
    }

    cout << "My Port: " << myaddress->port << endl;


    /******************************
    * These are used to recieve
    *******************************/

    UDPpacket * rcvP = SDLNet_AllocPacket(1024);
    if(!rcvP) {
        printf("Could not allocate receiving packet\n");
        exit(3);
    }

    UDPsocket rcvS;
    rcvS = SDLNet_UDP_Open(myaddress->port);
    if(!rcvS)
    {
        printf("Could not allocate receiving socket\n");
        exit(4);
    }

    //resolve the address of the server
    IPaddress srvHost;
    IPaddress * ip = & srvHost;
    SDLNet_ResolveHost(ip, "localhost", 8888);

    //set up the packet
    UDPpacket * p = SDLNet_AllocPacket(1024);
    if(!p) {
        printf("Could not allocate packet\n");
        SDLNet_Quit();
        SDL_Quit();
        exit(2);
    }

    //link the server address to our packet
    p->address.host = srvHost.host;
    p->address.port = srvHost.port;


    //main loop
    while(true){

        SDL_Event e;
        while(SDL_PollEvent(&e))
        {
            if(e.type == SDL_KEYDOWN)
            {
                char * data;
                switch(e.key.keysym.sym)
                {
                    case SDLK_LEFT:
                        p->data = "left";
                        p->len  = strlen("left") + 1;
                        SDLNet_UDP_Send(sd, -1, p);
                        break;
                    case SDLK_RIGHT:
                        p->data = "right";
                        p->len  = strlen("right") + 1;
                        SDLNet_UDP_Send(sd, -1, p);
                        break;
                    default:
                        break;
                }
            }
        }

    }

        return 0;
}

p->data points to a buffer which was already allocated by SDLNet_AllocPacket -- you need to copy your string data into it, rather than trying to replace where it points. p->data指向已由SDLNet_AllocPacket分配的SDLNet_AllocPacket - 您需要将字符串数据复制到其中,而不是尝试替换它指向的位置。 So, you will want to do something similar to this wherever you update the packet with C string data: 因此,无论您使用C字符串数据更新数据包,都需要执行类似的操作:

{
    static const char* data = "left";
    p->len = strlen(data) + 1;
    memcpy(p->data, data, p->len);
}

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

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