doskmc7870 2018-11-20 06:41
浏览 183

如果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))
  • 写回答

1条回答 默认 最新

  • droos02800 2018-11-20 07:09
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算