W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
你想在網(wǎng)絡(luò)上提供一個(gè)服務(wù)器。
創(chuàng)建一個(gè)基本的TCP服務(wù)器。
net = require 'net'
domain = 'localhost'
port = 9001
server = net.createServer (socket) ->
console.log "Received connection from #{socket.remoteAddress}"
socket.write "Hello, World!\n"
socket.end()
console.log "Listening to #{domain}:#{port}"
server.listen port, domain
可訪問(wèn)Basic Client:
$ coffee basic-server.coffee
Listening to localhost:9001
Received connection from 127.0.0.1
Received connection from 127.0.0.1
[...]
函數(shù)將為每個(gè)客戶端新連接的新插口傳遞給@net.createServer@ ?;镜姆?wù)器與訪客只進(jìn)行簡(jiǎn)單地交互,但是復(fù)雜的服務(wù)器會(huì)將插口連上一個(gè)專(zhuān)用的處理程序,然后返回等待下一個(gè)用戶的任務(wù)。
另請(qǐng)參閱Basic Client,Bi-Directional Server和Bi-Directional Client。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: