That is basically how its done. The only thing I would say is that you should send it all in 1 packet. Simplifies things if a packet gets lost or takes longer or arrives out of order. This is not as big of an issue as it used to be, but UDP does not guarantee order like TCP. Also its just more effective to send 1 packet.
I dont know what language you use, but normally I create a enum (number) like JC said. So my code looks a bit like this.
int PLAYER_MOVE = 1;
int PLAYER_CONNECT = 2;
PlayerMoveData
{
int x;
int y;
}
ConnectData
{
}
Packet
{
type = PLAYER_MOVE;
PlayerMoveData data;
}
So when I get the packet, I read the first value and then decide what to do with the rest of the information based on what type of packet it is. I am not sure this will translate to the language your using. But if you can try to only read the first bit of the packet to find out the type, then do something with the rest.I do not know java so I cant say how to do it. But here is some mockup code in a c++ ish language. The hardest part of the above is probably reading a partial packet before you know the rest of the information. It is fairly trivial in c++ if that's what you are using.
If you have a packet like:
void* packetData;
you could do:
const int* identifier = packetData;
if(*identifier == PLAYER_MOVE)
{
PlayerMoveData* moveData= static_cast<char*>(packetData) + sizeof(int);
}