How TCP Client(golang) knows when data end if TCP Server(nodejs) send data without separator “n”












-1














My problem:



I'm trying to read the data sended by TCP server(nodejs)
but I can't, the server send the data without breakline "rn" or "n"



I'm new in Golang but I have been trying a lot of things to get all the data sended by server.



Code from Server.js, this a simple example



var net = require('net');

var server = net.createServer(function(socket) {
console.log("New Client")

socket.on('data', function(data){
console.log("data",data,data.toString())
socket.write("qweqweqkjwebqkjwhbekqjwbekjqbwkejhqwkjehqkwjehkqjwehkqjwhekjqhwekjhqwe")
})
socket.on('error', function(error){
console.error("Error:",error)
})
});

server.listen(4001, '127.0.0.1');


My code from golang



package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"time"
)

func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:4001")

for {
fmt.Println("Send Text")
fmt.Fprintf(conn, "Hello")

// message, _ := bufio.NewReader(conn).ReadString('n')
// message, _ := bufio.NewReader(conn).ReadString('r')
message, _, _ := bufio.NewReader(conn).ReadLine() // how i know when data end if server doesn't send separator like "n" "r"
fmt.Println("Message from server: " + message)

time.Sleep(time.Second * 3)
}
}


Output from Client Golang:



Send Text


And that is all, the client(golang) is waiting for new line



Questions:



1.- There is a standard size of buffer in net(nodejs) ?

2.- How I can read the data sended by Server in golang without breakline? (there is no problem client and server in nodejs)

3.- I need to read byte by byte? and find x00 from buffer sended by Server(nodejs) ? (if this is the case how?)

4.- Server and Client in nodejs works with separator, but when they send data to the other one, in the other side separator is deleted?



I have teste this examples, but no one break the cicle for



reader := bufio.NewReader(conn)
// for {
// time.Sleep(time.Second * 3)
// // buff := make(byte, 4)
// test, _ := reader.ReadByte()
// fmt.Printf("%qn", test)
// fmt.Printf("%xn", test)
// }

// buf := make(byte, 1)
// for {
// n, err := reader.Read(buf)
// fmt.Println(n, err, buf[:n])
// if err == io.EOF {
// break
// }
// }

// buf := make(byte, 4)
// if _, err := io.ReadFull(reader, buf); err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(buf))

buf, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))









share|improve this question




















  • 1




    then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
    – danicheeta
    Nov 20 at 7:04












  • @danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
    – DarckBlezzer
    Nov 20 at 16:27










  • it must append n under the hood. I havent checked that out but its logical to me
    – danicheeta
    Nov 20 at 16:38
















-1














My problem:



I'm trying to read the data sended by TCP server(nodejs)
but I can't, the server send the data without breakline "rn" or "n"



I'm new in Golang but I have been trying a lot of things to get all the data sended by server.



Code from Server.js, this a simple example



var net = require('net');

var server = net.createServer(function(socket) {
console.log("New Client")

socket.on('data', function(data){
console.log("data",data,data.toString())
socket.write("qweqweqkjwebqkjwhbekqjwbekjqbwkejhqwkjehqkwjehkqjwehkqjwhekjqhwekjhqwe")
})
socket.on('error', function(error){
console.error("Error:",error)
})
});

server.listen(4001, '127.0.0.1');


My code from golang



package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"time"
)

func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:4001")

for {
fmt.Println("Send Text")
fmt.Fprintf(conn, "Hello")

// message, _ := bufio.NewReader(conn).ReadString('n')
// message, _ := bufio.NewReader(conn).ReadString('r')
message, _, _ := bufio.NewReader(conn).ReadLine() // how i know when data end if server doesn't send separator like "n" "r"
fmt.Println("Message from server: " + message)

time.Sleep(time.Second * 3)
}
}


Output from Client Golang:



Send Text


And that is all, the client(golang) is waiting for new line



Questions:



1.- There is a standard size of buffer in net(nodejs) ?

2.- How I can read the data sended by Server in golang without breakline? (there is no problem client and server in nodejs)

3.- I need to read byte by byte? and find x00 from buffer sended by Server(nodejs) ? (if this is the case how?)

4.- Server and Client in nodejs works with separator, but when they send data to the other one, in the other side separator is deleted?



I have teste this examples, but no one break the cicle for



reader := bufio.NewReader(conn)
// for {
// time.Sleep(time.Second * 3)
// // buff := make(byte, 4)
// test, _ := reader.ReadByte()
// fmt.Printf("%qn", test)
// fmt.Printf("%xn", test)
// }

// buf := make(byte, 1)
// for {
// n, err := reader.Read(buf)
// fmt.Println(n, err, buf[:n])
// if err == io.EOF {
// break
// }
// }

// buf := make(byte, 4)
// if _, err := io.ReadFull(reader, buf); err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(buf))

buf, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))









share|improve this question




















  • 1




    then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
    – danicheeta
    Nov 20 at 7:04












  • @danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
    – DarckBlezzer
    Nov 20 at 16:27










  • it must append n under the hood. I havent checked that out but its logical to me
    – danicheeta
    Nov 20 at 16:38














-1












-1








-1







My problem:



I'm trying to read the data sended by TCP server(nodejs)
but I can't, the server send the data without breakline "rn" or "n"



I'm new in Golang but I have been trying a lot of things to get all the data sended by server.



Code from Server.js, this a simple example



var net = require('net');

var server = net.createServer(function(socket) {
console.log("New Client")

socket.on('data', function(data){
console.log("data",data,data.toString())
socket.write("qweqweqkjwebqkjwhbekqjwbekjqbwkejhqwkjehqkwjehkqjwehkqjwhekjqhwekjhqwe")
})
socket.on('error', function(error){
console.error("Error:",error)
})
});

server.listen(4001, '127.0.0.1');


My code from golang



package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"time"
)

func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:4001")

for {
fmt.Println("Send Text")
fmt.Fprintf(conn, "Hello")

// message, _ := bufio.NewReader(conn).ReadString('n')
// message, _ := bufio.NewReader(conn).ReadString('r')
message, _, _ := bufio.NewReader(conn).ReadLine() // how i know when data end if server doesn't send separator like "n" "r"
fmt.Println("Message from server: " + message)

time.Sleep(time.Second * 3)
}
}


Output from Client Golang:



Send Text


And that is all, the client(golang) is waiting for new line



Questions:



1.- There is a standard size of buffer in net(nodejs) ?

2.- How I can read the data sended by Server in golang without breakline? (there is no problem client and server in nodejs)

3.- I need to read byte by byte? and find x00 from buffer sended by Server(nodejs) ? (if this is the case how?)

4.- Server and Client in nodejs works with separator, but when they send data to the other one, in the other side separator is deleted?



I have teste this examples, but no one break the cicle for



reader := bufio.NewReader(conn)
// for {
// time.Sleep(time.Second * 3)
// // buff := make(byte, 4)
// test, _ := reader.ReadByte()
// fmt.Printf("%qn", test)
// fmt.Printf("%xn", test)
// }

// buf := make(byte, 1)
// for {
// n, err := reader.Read(buf)
// fmt.Println(n, err, buf[:n])
// if err == io.EOF {
// break
// }
// }

// buf := make(byte, 4)
// if _, err := io.ReadFull(reader, buf); err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(buf))

buf, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))









share|improve this question















My problem:



I'm trying to read the data sended by TCP server(nodejs)
but I can't, the server send the data without breakline "rn" or "n"



I'm new in Golang but I have been trying a lot of things to get all the data sended by server.



Code from Server.js, this a simple example



var net = require('net');

var server = net.createServer(function(socket) {
console.log("New Client")

socket.on('data', function(data){
console.log("data",data,data.toString())
socket.write("qweqweqkjwebqkjwhbekqjwbekjqbwkejhqwkjehqkwjehkqjwehkqjwhekjqhwekjhqwe")
})
socket.on('error', function(error){
console.error("Error:",error)
})
});

server.listen(4001, '127.0.0.1');


My code from golang



package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"time"
)

func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:4001")

for {
fmt.Println("Send Text")
fmt.Fprintf(conn, "Hello")

// message, _ := bufio.NewReader(conn).ReadString('n')
// message, _ := bufio.NewReader(conn).ReadString('r')
message, _, _ := bufio.NewReader(conn).ReadLine() // how i know when data end if server doesn't send separator like "n" "r"
fmt.Println("Message from server: " + message)

time.Sleep(time.Second * 3)
}
}


Output from Client Golang:



Send Text


And that is all, the client(golang) is waiting for new line



Questions:



1.- There is a standard size of buffer in net(nodejs) ?

2.- How I can read the data sended by Server in golang without breakline? (there is no problem client and server in nodejs)

3.- I need to read byte by byte? and find x00 from buffer sended by Server(nodejs) ? (if this is the case how?)

4.- Server and Client in nodejs works with separator, but when they send data to the other one, in the other side separator is deleted?



I have teste this examples, but no one break the cicle for



reader := bufio.NewReader(conn)
// for {
// time.Sleep(time.Second * 3)
// // buff := make(byte, 4)
// test, _ := reader.ReadByte()
// fmt.Printf("%qn", test)
// fmt.Printf("%xn", test)
// }

// buf := make(byte, 1)
// for {
// n, err := reader.Read(buf)
// fmt.Println(n, err, buf[:n])
// if err == io.EOF {
// break
// }
// }

// buf := make(byte, 4)
// if _, err := io.ReadFull(reader, buf); err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(buf))

buf, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))






node.js go tcp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 16:57

























asked Nov 20 at 6:41









DarckBlezzer

1,8711927




1,8711927








  • 1




    then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
    – danicheeta
    Nov 20 at 7:04












  • @danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
    – DarckBlezzer
    Nov 20 at 16:27










  • it must append n under the hood. I havent checked that out but its logical to me
    – danicheeta
    Nov 20 at 16:38














  • 1




    then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
    – danicheeta
    Nov 20 at 7:04












  • @danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
    – DarckBlezzer
    Nov 20 at 16:27










  • it must append n under the hood. I havent checked that out but its logical to me
    – danicheeta
    Nov 20 at 16:38








1




1




then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
– danicheeta
Nov 20 at 7:04






then how does your golang server understands the command is finished ?! its like asking a way to issue a command in terminal without pressing Enter. I think what you want is a websocket like protocol
– danicheeta
Nov 20 at 7:04














@danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
– DarckBlezzer
Nov 20 at 16:27




@danicheeta then how Client and Server TCP in nodejs works?, If I execute socket.write(data) without separator("n") in the server(js), data go to on socket.on('data', function) in client side, and my questions is how can I emulate this with golang?
– DarckBlezzer
Nov 20 at 16:27












it must append n under the hood. I havent checked that out but its logical to me
– danicheeta
Nov 20 at 16:38




it must append n under the hood. I havent checked that out but its logical to me
– danicheeta
Nov 20 at 16:38












1 Answer
1






active

oldest

votes


















0














It's about TCP data transfer, usually in this case you should define a protocol for your data to define how much byte will be send and usually a separator for end of packet. for example



[n-Bytes for data lenght][data][separator bytes]


In fact, ReadLine also uses this method but only with separator






share|improve this answer





















  • but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
    – DarckBlezzer
    Nov 20 at 16:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387559%2fhow-tcp-clientgolang-knows-when-data-end-if-tcp-servernodejs-send-data-witho%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It's about TCP data transfer, usually in this case you should define a protocol for your data to define how much byte will be send and usually a separator for end of packet. for example



[n-Bytes for data lenght][data][separator bytes]


In fact, ReadLine also uses this method but only with separator






share|improve this answer





















  • but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
    – DarckBlezzer
    Nov 20 at 16:19
















0














It's about TCP data transfer, usually in this case you should define a protocol for your data to define how much byte will be send and usually a separator for end of packet. for example



[n-Bytes for data lenght][data][separator bytes]


In fact, ReadLine also uses this method but only with separator






share|improve this answer





















  • but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
    – DarckBlezzer
    Nov 20 at 16:19














0












0








0






It's about TCP data transfer, usually in this case you should define a protocol for your data to define how much byte will be send and usually a separator for end of packet. for example



[n-Bytes for data lenght][data][separator bytes]


In fact, ReadLine also uses this method but only with separator






share|improve this answer












It's about TCP data transfer, usually in this case you should define a protocol for your data to define how much byte will be send and usually a separator for end of packet. for example



[n-Bytes for data lenght][data][separator bytes]


In fact, ReadLine also uses this method but only with separator







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 7:09









Milad Aghamohammadi

801515




801515












  • but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
    – DarckBlezzer
    Nov 20 at 16:19


















  • but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
    – DarckBlezzer
    Nov 20 at 16:19
















but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
– DarckBlezzer
Nov 20 at 16:19




but how a server and client TCP in nodejs know when data ends if there isn't a "separator byte" in the data that I send?
– DarckBlezzer
Nov 20 at 16:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387559%2fhow-tcp-clientgolang-knows-when-data-end-if-tcp-servernodejs-send-data-witho%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out