简体   繁体   中英

SDL_net doesn't receive UDP packets

I'm trying to communicate with my esp8266 and as you can see, I successfully receive a packet from it every 2 seconds:

wireshark screenshot

wireshark

but it seems like I don't receive any data in my SDL_net app (the if in the while at the end stays always false):

#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_net.h>

int main(int argc, char **argv)
{   
    /* initialize SDL */
    if(SDL_Init(0)==-1)
    {
        printf("SDL_Init: %s\n",SDL_GetError());
        exit(1);
    }

    /* initialize SDL_net */
    if(SDLNet_Init()==-1)
    {
        printf("SDLNet_Init: %s\n",SDLNet_GetError());
        exit(2);
    }
        UDPsocket udpsock;

        udpsock=SDLNet_UDP_Open(6666);
        if(!udpsock) {
            printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError());
            exit(2);
        }

        UDPpacket *packet  = SDLNet_AllocPacket(2048);
        int numrecv;

        while(1)
        {

            if(SDLNet_UDP_Recv(udpsock, packet)) {
                printf("%s",packet->data);
            }
        }

    /* shutdown SDL_net */
    SDLNet_Quit();

    /* shutdown SDL */
    SDL_Quit();

    return(0);
}

It worked when I've send it packets from localhost, so I tried to shut down my firewall, but it still didn't work. Thank you for all your help.

edit: So, I tried to write similar thing using boost::asio. Both programs (SDL_net and boost::asio) receive packets from localhost, but none of them can receive anything from a different device (I tried my sending program, that works over localhost, on a different PC, but still without success). So I guess there is something wrong with my Ubuntu. Any advice on what should I do with that?

Ok, I think I've finally found the problem, I was trying to allocate too much space for the packet. I don't know why it even matters but when I changed the number in SDLNet_AllocPacket(2048); to 32, everything started to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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