1

My node.js UDP server's code is

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);

and my C++ client's code is:

    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;
}

When user sends characters like "中文",the nodejs UDP server receives " ".It gets unrecognizable code. My nodejs code's encoding is UTF-8 and changing to ANSI is the same problem.

Gank
  • 4,507
  • 4
  • 49
  • 45
  • What do you see if you do `console.log(message)`? It should be a Buffer. – loganfsmyth Apr 25 '13 at 14:54
  • 1
    I assume that you are using unicode in your client application. You should convert `content` from CString to a UTF-8 string. And it should work wonderfully. A little help on that: [Converting CString](http://stackoverflow.com/questions/859304/convert-cstring-to-const-char) – spotirca Apr 25 '13 at 14:55
  • @loganfsmyth I saw space word and coped it out,it is "???" – Gank Apr 25 '13 at 15:55
  • @spotirca I think you are right,I'll try it.Is it be able to let nodejs receive message by ANSI? – Gank Apr 25 '13 at 15:58
  • @Gank well, you can't convert unicode characters to ansi. UTF-8 is the way to go. – spotirca Apr 25 '13 at 16:22
  • @spotirca My C++ program is working with other C++ programs well.My nodejs server is using for test only.So I am trying to make nodejs support ANSI messages. – Gank Apr 26 '13 at 10:41

0 Answers0