【发布时间】:2019-02-13 17:45:50
【问题描述】:
我正在尝试在不使用任何库的情况下实现自己的 P2P 网络。我想构建一个 TCP 客户端来发送和接收来自其他节点的消息。所以所有节点都应该能够通过 tcp/ip 发送和接收消息。
我目前的问题是当我启动两个客户端时:一个节点能够接收和发送消息,但另一个节点只是发送消息而无法接收消息。
我想我需要以某种方式实现一个通道,但我对 Go 真的很陌生,不知道如何实现它。有什么建议么?
代码如下:
Main.go:
package main
func main() {
address := "127.0.0.1:8081" // IP of the other node; hardcoded for now
go startServer()
startClient(address)
}
helpler.go:
package main
import (
"bufio"
"fmt"
"net"
"os"
)
func startClient(address string) {
//connect to this socket
connClient, _ := net.Dial("tcp", address)
for {
//read in input from stdin
reader := bufio.NewReader(os.Stdin)
fmt.Print("Text to send: ")
text, _ := reader.ReadString('\n')
//send to socket
fmt.Fprint(connClient, text+"\n")
//listen for reply
//message, _ := bufio.NewReader(connClient).ReadString('\n')
//fmt.Print("Message from server: " + message)
}
}
func startServer() {
fmt.Println("Starting...")
//listen on all interfaces
ln, _ := net.Listen("tcp", ":8081")
//accept connection on port
connServer, _ := ln.Accept()
//run loop forever
for {
//will listen for message to process ending in newline(\n)
message, _ := bufio.NewReader(connServer).ReadString('\n')
//fmt.Print("Message Received:" + string(message))
//sample process for string received
//newmessage := strings.ToUpper(message)
connServer.Write([]byte(message + "\n"))
}
}
【问题讨论】:
-
一个问题是 StartServer 中的循环创建并丢弃 bufio.Reader 以及该读取器缓冲的任何数据。每个连接创建一次 bufio.Readers。
-
谢谢,但它是在循环中创建双向通道,以便在节点之间发送无休止的消息。不仅仅是发送“ping”
-
程序丢弃缓冲数据。那可能不是你想要的。此外,ReadString 返回的值包括跟踪 \n。另一个 \n 在写入时添加。如果您不打算在每条消息中都发送一个空行,则不要添加额外的 \n。
-
不要忽略错误。您不能让两台服务器在同一个主机:端口上侦听。
-
你的意思是发送消息一个端口和接收消息一个端口?