为什么不是'我的helloworld node.js tcp服务器没有连接
Why isn't my hello world node.js tcp server getting any connections?
我正在尝试测试Node.js,我使用的代码是:
// Load the net, and sys modules to create a tcp server.
var net = require('net');
var sys = require('sys');
// Setup a tcp server
var server = net.createServer(function (socket) {
// Every time someone connects, tell them hello and then close the connection.
socket.addListener("connect", function () {
//sys.puts("Connection from " + socket.remoteAddress);
console.log("Person connected.");
var myPacket = [1,2,3,4,5];
sys.puts(myPacket);
socket.end("Hello World'n");
});
});
// Fire up the server bound to port 7000 on localhost
server.listen(7000, "localhost");
// Put a friendly message on the terminal
console.log("TCP server listening on port 7000 at localhost.");
将字节数组发送到本地主机端口7000上显示的任何连接。虽然没有任何连接,但我尝试过firefox(localhost:7000和127.0.0.1:7000),我尝试过PuTTy,甚至编写了自己的Java TCP客户端来连接到本地主机,但没有任何工作,所以我确信代码是错误的。
有人能告诉我为什么我的代码不允许连接吗?
您似乎过于复杂化了连接部分。带有套接字的回调已经是连接事件,因此不需要单独侦听它。此外,如果要发送二进制文件,请使用Buffer类。这是您更改的代码。连接时请记住将您的模式设置为putty中的telnet。我还将end()更改为write(),这样它就不会自动关闭连接。
// Load the net, and sys modules to create a tcp server.
var net = require('net');
var sys = require('sys');
// Setup a tcp server
var server = net.createServer(function (socket) {
//sys.puts("Connection from " + socket.remoteAddress);
console.log("Person connected.");
var myPacket = new Buffer([65,66,67,68]);
socket.write(myPacket);
socket.write("Hello World'n");
});
// Fire up the server bound to port 7000 on localhost
server.listen(7000, "localhost");
// Put a friendly message on the terminal
console.log("TCP server listening on port 7000 at localhost.");
相关文章:
- Node.js TCP Connections
- node.js 需要太多的 TCP 套接字
- 从html页面通过node.js tcp套接字发送数据
- Node.js:tcp连接中的zlib压缩
- Node.js 无法处理多个 TCP 消息
- 如何使用 Sails.js 创建 TCP 服务器
- Node.js 中的 TCP 套接字和文件操作
- 简单的TCP聊天节点.js
- 无法在TCP服务器和节点.js之间发送消息
- node js 从 tcp socket net.createServer 读取特定消息
- 在Node.JS中管理多个TCP客户端
- Node js,module.js:340错误:找不到模块'tcp'
- Node.js TCP服务器,用于评估和打印数据
- 当用户发送get请求时,我怎么能等到数据在Node.js中从TCP服务器接收到
- Node.js JavaScript如何在异步事件处理程序中访问oject数据;socket.on”;在TCP服务器中
- 我可以用TLS加密Node.js TCP流量,使其无法读取吗?
- Node.js找不到模块'tcp'
- node.js服务器泄漏TCP连接
- 在node.js中使用TCP发送大文件,但接收损坏的文件
- Node.js中的同步TCP读取