// Data structures used
class MSG_GENERIC
{
public:
MSG_GENERIC()
{
}
DWORD type;
};
class MSG_STRING : public MSG_GENERIC
{
public:
MSG_STRING()
{
textsize = 0;
text = NULL;
type = WCS_MESSAGE;
}
DWORD textsize;
char *text;
};
...
//
// Here''s the function that sends a message of type MSG_STRING. EditBox is an edit box control. ChatBox is a read-only edit control
//
MSG_STRING str;
DWORD dwSize = 0;
char *szSendBuff = NULL;
int l = SendMessage(EditBox, WM_GETTEXTLENGTH, 0,0);
str.text = new char[l+1];
SendMessage(EditBox, WM_GETTEXT, l+1, (LPARAM)str.text);
str.textsize = l+1;
dwSize = sizeof(str)+str.textsize;
szSendBuff = new char[dwSize+sizeof(dwSize)];
ZeroMemory(szSendBuff, dwSize+sizeof(dwSize));
memcpy(szSendBuff, (char*)&dwSize, sizeof(dwSize));
memcpy(szSendBuff+sizeof(dwSize), (char*)&str, dwSize);
// this code prints the text to the chat area as a test. It doesn''t send anything. This works fine.
char *data = szSendBuff+sizeof(dwSize);
MSG_GENERIC *msg = (MSG_GENERIC*)data;
switch( msg->type )
{
case WCS_MESSAGE:
{
MSG_STRING *temp;
temp = (MSG_STRING*)data;
SendMessage(ChatBox, WM_SETTEXT, 0, (LPARAM)temp->text);
} break;
}
//
// This next line sends the structure. I would comment out the above if I were to use this line.
// This does not give an error, but watch what happens in the recv function...
int e = send(outgoing.s, szSendBuff, dwSize+sizeof(dwSize), 0);
// cleanup
delete[] str.text;
delete[] szSendBuff;
...
// In my FD_READ message...my recv funciton. s is a valid socket with data to be recved
DWORD dwSize = 0;
int r = 0;
char *szRecvBuf = NULL;
// get size of data to be recieved
if((r = recv(s, (char*)&dwSize, sizeof(dwSize), 0)) == SOCKET_ERROR)
return(1);
// create buffer to hold data
szRecvBuf = new char[dwSize];
ZeroMemory(szRecvBuf, dwSize);
// get data
recv(s, szRecvBuf, dwSize, 0);
// pass data to handling function
HandleMessage(szRecvBuf);
delete[] szRecvBuf;
return(0);
//
// My HandleMessage funciton...here come the error. data is the buffer from the FD_READ message
MSG_GENERIC *msg = (MSG_GENERIC*)data;
// switch on the type. this works fine
switch( msg->type )
{
case WCS_MESSAGE:
{
// it finds it to be a MESSAGE
MSG_STRING *temp;
temp = (MSG_STRING*)data; // cast to correct type
// now, temp->text should contain the text....but it does not
// when I look in the Chat area, I either see ''i'' or some other kind of garbage when I set the text of it equal to temp->text
SendMessage(ChatBox, WM_SETTEXT, 0, (LPARAM)temp->text);
} break;
}
return(0);
I assume that I am sending the address of the text rather than the actual text. So, how do I accomplish this? What do I have to do to be able to send variable length strings? Thanks in advance, and sorry for the long post.
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X
R- tv+ b++ DI+(+++) D- G e* h!"
Decode my geekcode!
Geekcode.com
Visit our web site: Asylum Entertainment