This really helped me.
http://www.ecst.csuchico.edu/~beej/guide/net/html/
This really helped me.
http://www.ecst.csuchico.edu/~beej/guide/net/html/
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
Have read that from cover to cover, though in the examples that he gives, he only uses char data types. I, on the other hand am trying to send a struct data type.
The prototypes listed above do not list any of the variables that they accept as of type char.
They are listed as const void * and void * respectively.
Thanks for your input anyway.
WebmasterMattD
WebmasterMattD.NET
>>. 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)?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.
WebmasterMattD
WebmasterMattD.NET
Your confusion over this:
>send( socket, &msg, sizeof( msg ), 0 )
>they pass it as follows
>send( socket, msg, sizeof( msg ), 0 )
is easily explained. A char array defined like this:
>char mydata[100];
will be passed like so:
>>send( socket, mydata, sizeof( msg ), 0 )
because mydata is a pointer to the first element of the array. That's how arrays work.
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.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.
Later,
WebmasterMattD
WebmasterMattD.NET
If you want, you could send the strings indivually over the wire, by passing the c_str() to the send().
stringA.c_str() for example will get you a char* to the string.
I not sure how this will fit your needs though.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Thanks for your input about strings, and all.
While, I have had success sending string using the c_str() command, it is a lot easier to send something such as a structure.
I will rethink the way that I am going to solve the problem, and hopefully get it working better than before.
Thanks,
WebmasterMattD
WebmasterMattD.NET
There's a reason everything you read sends text strings...
different machines represent binary differently in memory, the size of data types and big-endian v. little-endian, etc...
Sending binary structures across a socket could lead to a corruption of your data...
Why would he trust a cry baby?Originally posted by TrollKingBlows
There's a reason everything you read sends text strings...
different machines represent binary differently in memory, the size of data types and big-endian v. little-endian, etc...
Sending binary structures across a socket could lead to a corruption of your data...