[SDL] Yet another SDL2-based game

Leszek Godlewski lg at inequation.org
Thu Oct 17 22:47:29 PDT 2013


In fact, I am using the C language, not the C++ language.
So, I have not access to reinterpret_cast.

Yes, I specify a size for the packet :
---
// Create the packet
UDPpacket *packet = NULL;
packet = SDLNet_AllocPacket(1024);
---

But this size of 1024 is not the same than the one in "packet->len":
---
packet->len = 6;
---

Anyway, below is a source code that is working for a string.
Now, I would like to send a structure instead of this string.
For example, we can take this structure:
data.h :
typedef struct data
{
	int integer;
	char str[6];
} data_t;

Finally, I found the way how to cast correctly:
---
data_t data;
strcpy(data.str, "Hello\0");
data.integer = 123;
packet->data = (Uint8*)&data;
packet->len = sizeof(data);
---

The data is correctly sent and received between the server and the client.

Problem solved.

server.c:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <SDL/SDL.h> /* for SDL_Init */
#include <SDL/SDL_net.h> /* for SDLNet_Init */
#include <unistd.h> /* for sleep */
int main()
{
	// Initialize
	if(SDL_Init(0) == -1)
	{
		printf("SDL_Init: %s\n", SDL_GetError());
		exit(EXIT_FAILURE);
	}
	if(SDLNet_Init() == -1) {
		printf("SDLNet_Init: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	// Create the socket
	UDPsocket udpsock = NULL;
	udpsock = SDLNet_UDP_Open(6666);
	if(udpsock == NULL)
	{
		printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	// Create the packet
	UDPpacket *packet = NULL;
	packet = SDLNet_AllocPacket(1024);
	if(packet == NULL)
	{
		printf("SDLNet_AllocPacket: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	// Receive the packet
	int numrecv;
	while(1)
	{
		numrecv = SDLNet_UDP_Recv(udpsock, packet);
		printf("numrecv = %d (packet->data = \"%s\")\n", numrecv, (char *) packet->data);
		sleep(1);
	}
	SDLNet_FreePacket(packet);
	SDLNet_Quit();
	SDL_Quit();
	exit(EXIT_SUCCESS);
}

client.c:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <SDL/SDL.h> /* for SDL_Init */
#include <SDL/SDL_net.h> /* for SDLNet_Init */
#include <unistd.h> /* for sleep */
int main()
{
	char data[6] = "Hello\0";
	// Initialize
	if(SDL_Init(0) == -1)
	{
		printf("SDL_Init: %s\n", SDL_GetError());
		exit(EXIT_FAILURE);
	}
	if(SDLNet_Init() == -1) {
		printf("SDLNet_Init: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	// Create the socket
	UDPsocket udpsock;
	udpsock = SDLNet_UDP_Open(0);
	if(!udpsock)
	{
		printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError());
		exit(1);
	}
	// Resolve host
	IPaddress ipaddress;
	if(SDLNet_ResolveHost(&ipaddress, "localhost", 6666) == -1)
	{
		printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	// Create the packet
	UDPpacket *packet = NULL;
	packet = SDLNet_AllocPacket(1024);
	if(packet == NULL)
	{
		printf("SDLNet_AllocPacket: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	packet->data = data;
	packet->len = 6;
	packet->address = ipaddress;
	// Send the packet
	int numsent;
	while(1)
	{
		numsent = SDLNet_UDP_Send(udpsock, packet->channel, packet);
		printf("numsent = %d (packet->data = \"%s\")\n", numsent, (char *)packet->data);
		sleep(1);
	}
	SDLNet_FreePacket(packet);
	SDLNet_Quit();
	SDL_Quit();
	exit(EXIT_SUCCESS);
}


On Sun, 6 Oct 2013 17:04:41 +0200
Jonas Kulla <nyocurio at gmail.com> wrote:

> 2013/10/6 YuGiOhJCJ Mailing-List <yugiohjcj-mailinglist at laposte.net>
> 
> > Hello,
> >
> > I am using sdl_net-1.2.8.
> > I have a problem with the "UDPpacket" structure [1] because the "data"
> > member is of type "Uint8*" whereas the data I want to send over network is
> > a structure... So I am unable to cast.
> >
> > Why it is not a "void*" type for the data member ?
> >
> > Thank you.
> > Best regards.
> >
> 
> Hi,
> 
> I presume you're using C++? If everything else fails you can always
> reinterpret_cast anything to anything
> (I think). Anyway, I'm not sure you're just supposed to point the packet's
> data pointer to your structure.
> When you create the UDP packet, you specify a size, right? I think the data
> member points to an already
> allocated chunk of memory, so you should just memcpy your struct into it
> (assuming you allocated the
> packet with the size of your struct).
> 
> Jonas



More information about the SDL mailing list