在windows上 用正常的socket
[code]send(s,"你好世界",4,0);[/code]
這樣workerman服務器收到的始終是亂碼.這是因為workerman里使用的都是 utf-8
因此發(fā)送之前,一定要。[code]string UnicodeToUTF8(const wstring& str)
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte(936,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL);
pElementText = new char[iTextLen + 1];
memset((void[i])pElementText, 0, sizeof(char) [/i] (iTextLen + 1));
::WideCharToMultiByte(CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL);
string strText;
strText = pElementText;
delete pElementText;
return strText;
}
wstring str = L"你好世界";
string utf8str = UnicodeToUTF8(str);
send(s,utf8str.cstr(),utf8str.length(),0);
[/code]上面的會被截斷一部分文字,故把字節(jié)buffer弄大了一點,就沒問題了。
修正如下,[code]string UnicodeToUTF8(const wstring& str)
{
char pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte(936,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL);
pElementText = new char[iTextLen2 + 1];
memset((void[i])pElementText, 0, sizeof(char) [/i] (iTextLen2 + 1));
::WideCharToMultiByte(CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen2,
NULL,
NULL);
string strText;
strText = pElementText;
delete pElementText;
return strText;
}[/code]
感謝@egirlasm的分享