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

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗