[SDL] Preparing a UDP packet with SDL_net
James Buchwald
jamestheprogrammer at gmail.com
Wed Jun 20 09:45:18 PDT 2007
I'm trying to write a client/server MMO using SDL_net for the networking on
both sides. However, I've run into a problem when using the SDLNet_WriteXX
family of functions. I'm trying to write a 16-bit packet type ID to a UDP
packet, but it always segfaults.
This is the code that I create my packets with:
// initialize packet to given size
packet = SDLNet_AllocPacket(buffer_size + 1);
if (!packet) {
cout << "[error] failed to create packet" << endl;
return;
}
// zero out the packet
memset(packet->data, 0, buffer_size + 1);
And this is the code I use for writing data to the packet:
void AO_Packet::write16(Uint16 src) {
if (read_only) return;
if (position + sizeof(src) > packet->maxlen) {
string error = "write16: not enough room in packet";
cout << "[debug] " << error << endl;
return;
}
SDLNet_Write16(src, packet->data[position]);
position += sizeof(src);
}
Whenever my code runs, however, I get a segfault the first time
SDLNet_Write16 is called. The packet was successfully created, though, and
position is set to 0. Now I can't figure out what I'm doing wrong.
--
"If you see somebody who you don't know getting into a crop duster that
doesn't belong to you, report them." -- President George W. Bush
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070620/e6e19150/attachment.htm>
More information about the SDL
mailing list