【发布时间】:2017-04-25 03:13:02
【问题描述】:
我想向我的 nodejs TCP 服务器发送一个字符串。
这是我的客户端 (C++) 的代码:
#include <nds.h>
#include <dswifi9.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
std::string hostname = "hostname";
int port = 61733;
int sock;
int main(){
Connect(); //function to connect with a WiFi network
ConnectToServer();
unsigned long lastSendTime = 0;
unsigned long sendInterval = 200; //send a message every 200 milliseconds
unsigned int nMsgSent = 0;
while (1){
unsigned long now = milliseconds(); //function to get the time in milliseconds
if ((now - lastSendTime) > sendInterval){
std::string request = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
request += request;
request += std::to_string(nMsgSent);
request += "\n";
int sendResult = ::send(sock, request.c_str(), request.length(), 0);
iprintf("req(%d): %d\n", nMsgSent, sendResult);
nMsgSent++;
lastSendTime = now;
}
}
return 0;
}
这是我的服务器代码(JS):
var net = require('net');
var tcpServer = net.createServer();
var tcpPort = 61733;
tcpServer.on('connection', onConnection);
function onConnection(conn){
var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;
console.log('New client connection from: %s', remoteAddress);
conn.on('data', onData);
conn.once('close', onClose);
conn.on('error', onError);
function onData(data){
console.log('connection data from %s: %s', remoteAddress, data);
}
function onClose(){
console.log('connection from %s closed', remoteAddress);
}
function onError(err){
console.log('Connection %s error: %s', remoteAddress, err.message);
}
}
tcpServer.listen(tcpPort, function(){
console.log('Server listening on: %j', tcpServer.address());
});
问题来了:
客户端只发送了大约 180 条消息,然后send 挂起。服务器仅接收约 120 条消息。当我增加请求的长度时,客户端发送的请求更少,当我减少长度时,客户端发送的请求更多。我还发现,当我减少 sendInterval 时,我可以在send 挂起之前发送更多请求。我不知道我做错了什么。当我用 nodejs 客户端执行类似的代码时,一切正常,所以问题出在 C++ 客户端。
编辑:
我真的不认为这是必要的,但这里是connectToServer()的代码:
void ConnectToServer()
{
sock = socket(AF_INET, SOCK_STREAM, 0);
if (socket >= 0)
{
printf("Created Socket!\n");
}
else
{
printf("Couldn't create socket");
}
struct hostent * host = gethostbyname(hostname.c_str());
if (host != NULL)
{
printf("Found IP Address!\n");
}
else
{
printf("IP not found");
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = *((unsigned long *)(host->h_addr_list[0]));
int connectResult = ::connect(sock, (struct sockaddr *)&server, sizeof(server));
if (connectResult >= 0)
{
printf("Connected to server!\n");
}
else
{
printf("Not connected to server. Err: %d\n", connectResult);
}
}
来自 C++ 客户端的输出(发送间隔:2000 毫秒,msgLength:126-127 个字符):
req(1): 126
req(2: 126
req(3): 126
...keeps going on for a while...
req(9): 126
req(10): 126
req(11): 127
req(12): 127
...keeps going on for a while...
req(74): 64
send() hangs after this
nodejs服务器的输出(只接收9条消息):
connection data from ip::port: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678900
...keeps going on for a while...
connection data from ip::port: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678909
测试结果看发送间隔、消息长度和发送消息数量之间是否存在联系:
发送间隔 |消息长度 |消息 -------------------------------------- 200 | 127 | 26 200 | 65 | 33 -------------------------------------- 400 | 65 | 59 400 | 127 | 53 -------------------------------------- 2000 | 65 | 47 2000 | 127 | 21(我没有看到这些值之间的联系)
【问题讨论】:
-
不要垃圾标签!这不是 C。C 不是 C++ 不是 C!
-
@Olaf 我知道,但是套接字库是 C 代码,对吧?
-
@user7353781 套接字库的语言与您的问题无关。
-
如果客户端发送消息的速度快于服务器读取它们的速度,客户端最终会阻塞,直到服务器赶上。您确定服务器正在读取消息吗?
-
几乎所有你在某个级别使用的库都是如此。你认为应该为每个问题添加C标签吗?