Socket Programmins

This is a discussion on Socket Programmins within the Linux Programming forums, part of the Platform Specific Boards category; This really helped me.
http://www.ecst.csuchico.edu/~beej/guide/net/html/...

>>. I am trying to send a struct data type, though it does not want to be sent as it should.
Can you be more specific in what's actually wrong?
What debugging have you done?
Does it send the correct number of bytes?
What do you receive if you just use a normal buffer and hex dump it?
What's the definition of the struct? Does it contain pointers (which might be a problem)?

Okay, what is actually wrong, I am not sure about.
First of all, in all of the socket programming examples and tutes that I have look through and read they use chars. What they also do is that they do not pass the char array to send as
send( socket, &msg, sizeof( msg ), 0 )
they pass it as follows
send( socket, msg, sizeof( msg ), 0 )

This really confuses me. When I try to pass any other data type to the send function in this manner, the compiler will generate the error
' Can not convert from type const void * to MsgPacket '.

This error dissapears when i pass the memmory address using the & operator with the structure and send as follows
send( socket, &MsgPacket, sizeof( MsgPacket ), 0 )

The structure definition does not contain any pointers what so ever and is as follows:
struct MsgPacket
{
string username;
string message;
};

The reason that it is so simple for the moment is so that I can get the passing of the structure right before I get too carried away.

As far as the number of bytes, That is being sent correctly.

Hex dumping? Well I am not sure on how to get that achieved.

Thanks for your feedback, I hope that this futher information will help in the solving of this problem.

Now if you have a single struct, then yes, you need to pass it's address, so you must use &mystruct.

>As far as the number of bytes, That is being sent correctly.
To clarify, does send() return the correct number of bytes?

>Hex dumping? Well I am not sure on how to get that achieved.
After recv()'ing data, write out a message that says %d bytes received (rc from recv), then loop through your received buffer 1 to nbytes times using printf("%02x ", buf[i]); or something similar. It's just a way of seeing the raw data your app received.

>string username;
I'm not sure of the underlying structure of a C++ string. This may be a problem, you might need to convert it somehow.

Thanks for your reply Hammer, it was of greate help.
It appears that because String are created dynamicaly, then it is not as easy to determin their size ( in memory ) and requires a bit more fiddling around. Anyway when I created a different structure, this time with two integers contained inside, it was sent without a problem.

If anyone with a bit more experience in sending C++ strings over a TCP ( stream ) socket connection, then please let me know how it is done.