String sent with sockets
I get this code from another soft built by VC++ 6.
So then, ive tried to transate it to VC++8.
Server Side:
In the Socket.cpp:
void Socket::OnReceive(int nErrorCode)
{
long dataLength;
Receive(&dataLength, 4);
if (dataLength > 0&& dataLength < 100000) //Just to make sure the client doesn't crash the server)
{
byte* message = new byte[dataLength+1];
int nRec = Receive(message,dataLength);
message[nRec] = '\0';
::SendMessage(hParent,WM_RECEIVE_MESSAGE,(WPARAM)message,(LPARAM)nSocket);
}
Dialog:
char* pMessage = (char*)receiveMessage;
CString message = (CString)pMessage;
//int *pSocket = (int*)nSocket;
//int numSocket = (int)pSocket;
MessageBox(L"Data: "+message);
now my probleme is that i dont get the string sent correctly.
i get some symbol instead.
here is the send code in the client side if you need:
long userl = m_user.GetLength();
UpdateData(TRUE);
SocketA.Send(&userl, 4);
SocketA.Send(&m_user,userl);
Maybe i type casted wrong ?
"m_user" is a edit box in the dialog(CString member)
Thank you from advance
[1266 byte] By [
yoni1993] at [2007-11-20 11:34:54]

# 3 Re: String sent with sockets
If it really is a UNICODE problem and nothing else, then this will fix it:
LPCSTR pMessage = (LPCSTR)receiveMessage;
CString message = pMessage;
Note that it's LPCSTR (not LPCTSTR) and that pMessage isn't cast to a CString.
# 4 Re: String sent with sockets
You have the old style look because you remove Unicode. To get the "new" look either way, look at your stdafx.h file... you'll see at the bottom, something like this:#ifdef _UNICODE
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#endif
You'll notice that this is wrapped in an #ifdef _UNICODE. Remove that #ifdef (and the matching #endif) and the new style will appear without Unicode.
Hope that helps.
krmed at 2007-11-10 22:28:04 >
