我有这个 Go tcp 套接字
package main
import (
"fmt"
"io"
"log"
"net"
"bytes"
)
func main() {
l, err := net.Listen("tcp", ":1200")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go func(c net.Conn) {
var buf bytes.Buffer
io.Copy(&buf, c)
fmt.Println("total size:", buf.Len())
s := buf.String()
println(s)
c.Close()
}(conn)
}
}
接受一条消息,将其转换为字符串并显示它,但如果客户端未关闭连接,我将无法按预期看到服务器上显示的消息
如何在不需要关闭客户端连接(由客户端)的情况下从客户端发送多条消息?
这里是 NodeJs 中的客户端
var net = require('net');
let packet = {
Target: { Host: "", Port: "9000" },
Name: { Family: "Newmarch", Personal: "Jan" },
Email: [
{ Kind: "home", Address: "jan@newmarch.name"},
{ Kind: "work", Address: "j.newmarch@boxhill.edu.au"}
]
}
var client = new net.Socket();
client.connect(1200, '0.0.0.0', function() {
console.log('Connected');
client.write( (new Buffer(JSON.stringify(packet), 'UTF-8')) );
client.end();
//client.write('\n');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
米琪卡哇伊
潇潇雨雨
相关分类