pocket86
pocket86

Reputation: 33

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.

I'm using the SDL_net framework for this project.

The issue I'm running into is the UDPpacket struct uses Uint8 * as the data type for the data. 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).

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;
}

Upvotes: 3

Views: 2481

Answers (1)

Christopher Oicles
Christopher Oicles

Reputation: 3107

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. So, you will want to do something similar to this wherever you update the packet with C string data:

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

Upvotes: 2

Related Questions