I'm trying to communicate between a c++ client and a node.js server. I can send data to the server, but I'm unable to send any messages back to the client. My simple understanding of socket.emit is that it should send a message that's retrievable by calling recv() on the FD_READ message in a basic windows message loop. I might be missing something there.
Here's the relevant client code:
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
MessageBox(hwnd, TEXT("3"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
long hostname = inet_addr("192.168.1.70");
target.sin_family = AF_INET;
target.sin_port = htons(server_port_num);
target.sin_addr.s_addr = hostname;
if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
MessageBox(hwnd, TEXT("999"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
WSAAsyncSelect(s, hwnd, CUST_MSG, FD_READ | FD_CONNECT | FD_CLOSE);
I send data, which is received and output on the server console:
int nret = send(s, buffer, strlen(buffer), 0);
Then I attempt to send some data back in the same server event that receives the data:
this.socket.on('data', function (buffer) {
try {
logger.warn(buffer + "\n");
socket.emit('message', 'psst, wanna buy some macaroni?');
//that.messageHandler(that, buffer);
} catch (err) {
logger.warn('Protocol ' + err + ' ' + that.socket.remoteAddress);
that.dispose();
}
});
I have a message loop, but it doesn't detect a message sent from socket.emit():
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
if (numBytes > 0) {
wchar_t buffer[256];
wsprintfW(buffer, L"%d", numBytes);
MessageBox(hwnd, buffer, TEXT(""), MB_OK | MB_ICONEXCLAMATION);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//MESSAGE LOOP FUNCTION:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) //handle the messages
{
case CUST_MSG: //Is a message being sent?
{
switch (lParam) //If so, which one is it?
{
case FD_ACCEPT:
MessageBox(hwnd, TEXT("accept"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
accept(wParam, NULL, NULL);
break;
case FD_CONNECT:
MessageBox(hwnd, TEXT("connect"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
break;
case FD_READ:
char buffer[80];
int byte_num;
memset(buffer, 0, sizeof(buffer));
byte_num = recv(wParam, buffer, sizeof(buffer) - 1, 0);
MessageBox(hwnd, TEXT("READING"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
numBytes += byte_num;
break;
case FD_CLOSE:
//MessageBox(hwnd, TEXT("close"), TEXT(""), MB_OK | MB_ICONEXCLAMATION);
break;
}
}
The same loop correctly detects messages when I change the program a bit and send data from the client to the client. I've tried messing around bind(), listen(), recv(), but I can't ever get an FD_READ message using node. I've tried with the client and server running on the same computer, as well as different computers on the same local network.
To write data to a socket in Node.js, use socket.write()
.
See https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.