Sign in to follow this  
nivlekio

Unity Whats better programming practice?

Recommended Posts

nivlekio    156
Hello Ive been reading some networking tutorials online some most don't seem to be consistent on sending bit of winsock can't explain by word alone so ill give an example to make thnigs clear...

struct UnitInfo
{
stPacketHeader stHeader;
int iUnitID;
int iUnitType;
int iUnitHealth;
int iUnitFireRate;

int UnitXPos;
int UnitYPos;
}

struct UserInfo
{
stPacketHeader stHeader;
int UserID;
char szUserName[50];
char szUserIP[16];

}


struct UnitInfo g_stiSPacketUnitInfo;
struct UserInfo g_stiSPacketUserInfo;

char szSendPacketBuffer[2000];
int iSizeOfSentData = 0;


some where in code after i fill in the structs with info then memcpy the struct in to the szSendPacketBuffer...... Is it better to send it with the size of the struct prototype,
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(UserInfo),0);

or send it with the sizeof the struct instance
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(g_stiSPacketUserInitInfo),0);

or the size of the buffer
iSizeOfSentData = ClientSocObject.iSend(szSendBuffer,sizeof(szSendBuffer),0);

no suprise i get diffrent results for iSizeOfSentData but i was wondering which is better programming practice and safer? I think logically its better to send with size of the struct instance but i am a bit worried that be might bad and cause some data/memory error or problems?

Share this post


Link to post
Share on other sites
Anon Mike    1098
This isn't really a networking question...

I prefer sizeof(variable) because if you use sizeof(Type) you can accidentally specify the wrong Type, or the Type could change and you forget to change the sizeof call. sizeof(szSendBuffer) is just plain wrong unless you really want to send uninitialized stack garbage for no good reason.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this