0

What is getting written to the socket when I write an ENUM reference (below)? I have something captured in whireshark but it does not resemble the ENUM name "JOIN" .. it is not the same length either. The server somehow understands that this code sent the JOIN enum.

#include <sstream>
#include <iostream>

... WriteToSocket( SOCKET hSocket, char *buf, int iCount)
  send(hSocket, buf, iCount, 0);


enum { JOIN, ...};
m_Command = JOIN;
WriteToSocket (hSocket, (char *)&m_Command, sizeof(m_Command));

I hope I included enough info and include statements ...

Gabe
  • 84,912
  • 12
  • 139
  • 238
jcalfee314
  • 4,642
  • 8
  • 43
  • 75
  • 3
    Writing an enum to a socket gives me a feeling of impending doom. – Corbin Mar 30 '12 at 20:39
  • It's writing the byte representation of whatever `m_Command` is in *native endian*. Most likely, it's writing a 4-byte integer. But if you want your code to be robust across different compilers and architectures, use fixed-size integers with network endianness (cf. [`htonl(3)`](http://linux.die.net/man/3/htonl)). Or better yet, use a serialization/marshaling library such as [protobuf](http://code.google.com/p/protobuf/). – Adam Rosenfield Mar 30 '12 at 20:43
  • All the more reason to support my approach to re-write the client... I can't change the server though. Unfortunately, I don't yet have the luxury of compiling and adding print statements either. – jcalfee314 Mar 30 '12 at 20:57
  • If you can't change the server, do you have a spec of the protocol it's using? Do you have read access to its source code? – Adam Rosenfield Mar 30 '12 at 20:59
  • All I have is the client code and wireshark (captures the packets and lets me see everything). Should be enough... I'm converting it to Java though so it is going to take me more setup time to run the C or C examples. – jcalfee314 Mar 30 '12 at 21:02
  • How do you know what data to send if you don't know what the server protocol is? – Adam Rosenfield Mar 30 '12 at 21:28
  • It depends on what m_Command is and what the assignment operator of the type of m_Command does, you didn't specify. – pizza Mar 30 '12 at 22:07

2 Answers2

0

An integral value is begin written. The size of the varaible can vary. In your case the size might be anything between either a 1-byte zero, up to an int-sized zero.

To see exactly what is being written, add this code:

std::cout << "Size: " << sizeof(m_Command) << " Value: " << (int)m_Command << "\n";
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • The compiler has a lot of flexibility in choosing the storage size and representation of enums, as long as they behave correctly when converted to integral types. – Ben Voigt Mar 30 '12 at 20:40
  • Outputs: Size: 4 Value: 0 The "Value" increments by one as I advance through the enum list. – jcalfee314 Mar 30 '12 at 21:15
0

If I understood correctly, you assume "JOIN" is transferred over network as a string. This is not correct. JOIN is an alias for an integer value (0 if it is the first member of enumeration). That integer value is transferred over the network, the server knows that; that particular integer value is an alias for "JOIN". Thats why your server works as expected.
Also you cannot see "JOIN" as a string in your wireshark logs, but the integer value which is alias of the "JOIN".

xaero99
  • 327
  • 1
  • 5