Jump to content
  • Advertisement
Sign in to follow this  
MirekMalysz

Sending data

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a question. in vb when u send data over winsock, and say you are sending the x co-ordinate and y-coordinate of and object, the client would receive the data and split up the x and y coordinate with a delimeter. how would you approach that in c++?

Share this post


Link to post
Share on other sites
Advertisement
In c++ if you want to send a delimiter you must add it to the end of the buffer to send. this character is ussually '\0' in c++.

Share this post


Link to post
Share on other sites
why don t you create a buffer where you pack all the data you want to send into in a formated way

or are you doing text base transmission?

Share this post


Link to post
Share on other sites
Typically, you send binary data, and you send it as messages that you can parse out based on the data.

Either each message has a known size, and you know what to do from the message code, or the message has a length field prepended before the actual data.

A simple example might be:


enum MessageCode {
DoNothing,
SaySomething,
MoveToPoint,
};

struct MessageHeader {
short messageCode;
short messageSize;
};

struct MessageSaySomething {
char what[256];
};

struct MessageMoveToPoint {
float x;
float y;
};

struct ActualMessage {
MessageHeader header;
union {
MessageSaySomething saySomething;
MessageMoveToPoint moveToPoint;
};
};

int sendSaySomething( SOCKET s, std::string const & text ) {
ActualMessage msg;
if( text.size() >= sizeof( msg.saySomething.what ) ) {
throw BadDataException();
}
memcpy( msg.saySomething.what, text.c_str(), text.size() );
msg.header.messageCode = MessageSaySomething;
msg.header.messageSize = sizeof( MessageHeader ) + text.size();
return ::send( s, (char const *)&msg, msg.header.messageSize, 0 );
}

int sendMoveToPoint( SOCKET s, float x, float y ) {
ActualMessage msg;
msg.header.messageCode = MessageMoveToPoint;
msg.header.messageSize = sizeof( MessageHeader ) + sizeof( msg.moveToPoint );
msg.moveToPoint.x = x;
msg.moveToPoint.y = y;
return ::send( s, (char const *)&msg, msg.header.messageSize, 0 );
}

// Really, you'll be using asynchronous receives and do some
// input buffering to avoid blocking the thread, but this is
// for illustration only.
int receiveMessage( SOCKET s, ActualMessage * outMessage ) {
int r = ::recv( s, (char *)outMessage, sizeof( outMessage->header ), 0 );
if( r != sizeof( outMessage->header ) ) {
throw ProtocolDesyncError();
}
if( outMessage->header.messageSize > sizeof( *outMessage ) - sizeof( outMessage->header ) ) {
throw BadNetworkDataError();
}
r = ::recv( s, (char *)&(&outMessage->header)[1], outMessage->header.messageSize, 0 );
return outMessage->header.messageCode;
}


You only need one receiveMessage() function, but you need one sendMessage function per message kind you want to send. Typically, in a larger system, you'll use some data-driven description of the message layouts to avoid having to write error-prone custom marshalling code for each message kind. (Code generation is also sometimes used)

Share this post


Link to post
Share on other sites
So you are merely casting a struct to an array of chars? Does this have any higher overheads than directly copying memory locations (that store ints, longs, etc...) to an array of chars and back again?

...Stip

Share this post


Link to post
Share on other sites
Casting between pointer types is a zero-cost operation; it's only there for the compiler semantic analysis. It doesn't actually generate any extra code, because at the assembly level, all pointers are just (long) integers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!