客户端
问题
你想使用网络上提供的服务。
解决方案
创建一个基本的TCP客户机。
在 Node.js 中
net = require net
domain = localhost
port = 9001
connection = net.createConnection port, domain
connection.on connect, () ->
console.log "Opened connection to #{domain}:#{port}."
connection.on data, (data) ->
console.log "Received: #{data}"
connection.end()
使用示例
可访问Basic Server :
$ coffee basic-client.coffee
Opened connection to localhost:9001
Received: Hello, World!
讨论
最重要的工作发生在connection.on data处理过程中,客户端接收到来自服务器的响应并最有可能安排对它的应答。
另请参阅Basic Server,Bi-Directional Client和Bi-Directional Server 。
练习
- 根据命令行参数或配置文件为选定的目标域和端口添加支持。
作者:andy,如若转载,请注明出处:https://www.web176.com/coffeescript/10625.html