I have this Go tcp socket
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)
}
}
accept a message, transform it to string and display it, but if the connection is not closed by the client I am not able to see the message showed on the server as expected
How is possible to send multiple message from the client without the need to close the client connection (by the client) ?
Here the client in NodeJs
var net = require('net');
let packet = {
Target: { Host: "", Port: "9000" },
Name: { Family: "Newmarch", Personal: "Jan" },
Email: [
{ Kind: "home", Address: "[email protected]"},
{ Kind: "work", Address: "[email protected]"}
]
}
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');
});
Thanks valeriano cossu