recieving data with sockets

Started by
11 comments, last by BeerNutts 11 years, 6 months ago

sizeof(Player) should be sizeof(struct Player)


There is no difference between those two expressions, unless you're using plain C and do not have a typedef for struct Player to Player. And, if you don't have that, then the code wouldn't even compile.
enum Bool { True, False, FileNotFound };
Advertisement

[quote name='KnolanCross' timestamp='1350784084' post='4992326']
sizeof(Player) should be sizeof(struct Player)


There is no difference between those two expressions, unless you're using plain C and do not have a typedef for struct Player to Player. And, if you don't have that, then the code wouldn't even compile.
[/quote]

Good to know, as I said, I am a C programmer, no CPP.


[quote name='KnolanCross' timestamp='1350784084' post='4992326']
Endianness problems got to do with how each machine deals with the byte order, for instance, if you have an int (4 bytes) with the value 1 it can be:
00 00 00 01
or
00 00 01 00

Depending on the machine.


from wikipedia:
"Well-known processor architectures that use the little-endian format include x86 (including x86-64), 6502 (including 65802, 65C816), Z80 (including Z180, eZ80 etc.), MCS-48, 8051, DEC Alpha, Altera Nios, Atmel AVR, SuperH, VAX, and, largely, PDP-11."

So we can say all commercial computers (x86 and x64) use little endian so there is not much of a problem here right?
[/quote]

Personally I never had problem with endianess, so I can't tell any popular processor having a different endianess.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

I posted something similar in another thread, but I would take a slightly different approach than using some random number of players (100) when you can easily use a configurable amount:


struct TPacketData {
uint16_t MsgType;
uint16_t MsgLength;
uint8_t MsgPayload[0];
}

enum {
PLAYER_PACKET = 0,
CHAT_PACKET
// define other types here
}

struct TPlayerData {
uint32_t id;
uint32_t x;
uint32_t y;
}

struct TChatData {
uint32_t PlayerId;
uint32_t ChatLength;
char ChatData[0];
}

void SendData(uint16_t msgType, uint16 msgLength, uint8_t *msgData)
{
uint32_t totalLength = sizeof(TPacketData) + msgLength;
TPacketData *packet = (TPacketData*)malloc(totalLength);
packet->MsgType = msgType;
packet->MsgLength = msgLength;
memcpy(packet->MsgData, msgData, msgLength);
send(ServerSocket, packet, totalLength);
}

// To Send 10 Player's Data do this
TPlayerData playerData[10]; // or use malloc to allocate how many players you need

// Fill in each of the player's data
playerData[0].id = Players[0].id;
...
playerData[9].x = Players[9].x;

// Now send off
SendData(PLAYER_PACKET, sizeof(TPlayerData)*10, (uint8_t *)playerData);


// To read it out, do this:
void RecvData()
{
TPacketData packetTemp;
TPacketData *packet;

// Read the packet header
recv(clientSocket, &packetTemp, sizeof(TPacketData));

// Create the actual packet
packet = (TPacketData*)malloc(sizeof(TPacketData) + packetTemp.MsgLength);
memcpy(packet, &packetTemp, sizeof(TPacketData);

// Read the rest of the data into the msg data
recv(clientSocket, packet->MsgData, packet->MsgLength);

// Check the type
if (packet->MsgType == PLAYER_PACKET) {
// parse each player
uint32_t playerLength = sizeof(TPlayerData);
uint32_t numPlayers = packet->MsgLength/playerLength;
for (uint32_t i = 0; i < numPlayers; i++) {
TPlayerData player;
memcpy(&player, packet->MsgData + playerLength*i, playerLength);

UpdatePlayer(player);
}
}


Casting and other things my be funny, but that's just something I threw together.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement