我的 node.js UDP 服务器的代码是
var PORT = 515;
var HOST = '127.0.0.1';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port +' - ' + message);
});
server.bind(PORT, HOST);
我的 C++ 客户端的代码是:
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
return FALSE;
}
if ( LOBYTE( wsaData.wVersion) != 1 ||
HIBYTE( wsaData.wVersion) != 1 )
{
WSACleanup();
return FALSE;
}
// get the server address
srvAddress.sin_addr.S_un.S_addr = inet_addr(GetProfileUdpHost());
srvAddress.sin_family = AF_INET;
srvAddress.sin_port = htons(GetProfileUdpPort());
int UdpSend(CString& content, SOCKADDR* address)
{
SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, 0);
int result = sendto(sockClient,
content,
content.GetLength()+1,
0,
address, sizeof(SOCKADDR));
closesocket(sockClient);
return result;
}
当用户发送“中文”之类的字符时,nodejs UDP服务器收到“”。它得到无法识别的代码。我的 nodejs 代码的编码是 UTF-8 并且更改为 ANSI 是同样的问题。