There are several problem I see with your code.
1. Your variable, command_header, is not initialized completely: only the command is assigned, but you never fill its DataBuffer. Did you mean to read the data from SourceFile into that buffer? If so, you need to copy it from buffer, and make sure you're not exceeding the PAYLOAD_SIZE.
2. The definition of Command_Data seems wrong. It only contains one char the way it's declared in your code. It's not clear what this struct is supposed to be, and used for.
3. tmp_str is never used. Therefore it's hard to say whether that assignment makes sense, or how to improve it.
Generally speaking, the compiler will normally accept all C-style typecasts, but that doesn't mean they make sense, or won't crash your program at run-time. I would suggest an alternative, but for that I need to know the use of tmp_str in your program.
P.S.:
After much additional info, and making some educated guesses, I think this is what your code should look like:
enum Command {
SEND_FILE = 0,
FILE_SIZE = 10,
};
struct FileSizeMsg {
Command command;
int file_size;
};
struct FileSendMsg {
Command command;
char data[];
};
int send_text_file(CFile& Sourcefile) {
ULONGLONG dwLength = Sourcefile.GetLength();
char* buffer = new char[(int)dwLength + 1];
UINT nActual = Sourcefile.Read(buffer, dwLength);
buffer[dwLength] = 0;
size_t real_length = strlen(buffer);
FileSizeMsg size_msg;
size_msg.command = FILE_SIZE;
size_msg.file_size = (int)real_length;
int nfRet = SockConnection.Send((char*)&size_msg, sizeof(size_msg));
int full_length = (int)sizeof(Command) + real_length + 1;
char* full_message = new char[full_length];
FileSendMsg* send_msg = reinterpret_cast<FileSendMsg*>(full_message);
send_msg->command = SEND_FILE;
strncpy(send_msg->data, buffer, real_length + 1);
nfRet = SockConnection.Send(full_message, full_length);
delete [] buffer;
delete [] full_message;
return nfRet;
}
This assumes that the receiver of these messages expects a size info message separated from the actual data. (otherwise why introduce two different commands?).
Note how FileSendMsg.data is declared as a placeholder for a string of arbitrary length. It can not be used in a normal way, but if you separate the memory allocation from the use of this struct, it behaves like a dynamically sized object class.