如果TCP Server(nodejs)发送不带分隔符“ ”的数据,TCP Client(golang)如何知道何时结束数据

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 " " or " "

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('
')
        // message, _ := bufio.NewReader(conn).ReadString('')
        message, _, _ := bufio.NewReader(conn).ReadLine() // how i know when data end if server doesn't send separator like "
" ""
        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("%q
", test)
    //  fmt.Printf("%x
", 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))
doulanli6146
doulanli6146 它必须在幕后附加 。我还没有检查出来,但对我来说合乎逻辑
一年多之前 回复
dongyun9120
dongyun9120 那么nodejs中客户端和服务器TCP的工作原理是什么呢?如果我在server(js)中执行不带分隔符(“ ”)的socket.write(data),则数据将位于以下位置的socket.on('data',function)上客户端,我的问题是我该如何用golang来模仿?
一年多之前 回复
douxuanwei1980
douxuanwei1980 那么您的golang服务器如何理解命令已完成?就像在不按Enter的情况下询问在终端中发出命令的方法一样。我认为您想要的是类似协议的websocket
一年多之前 回复

1个回答



这与TCP数据传输有关,通常在这种情况下,您应该为数据定义协议以定义将发送多少字节,通常 数据包结尾的分隔符。 例如</ p>

  [数据长度的n字节] [data] [分隔符字节] 
</ code> </ pre>

ReadLine </ code>也使用此方法,但仅使用分隔符</ p>
</ div>

展开原文

原文

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

duanmei2805
duanmei2805 但是,如果我发送的数据中没有“分隔符字节”,nodejs中的服务器和客户端TCP如何知道数据何时结束?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问