mirror of
https://github.com/openp2p-cn/openp2p.git
synced 2026-04-10 14:29:00 +08:00
3.18.4 virtual private network
This commit is contained in:
42
example/echo/echo-client.go
Normal file
42
example/echo/echo-client.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
op "openp2p/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
op.Run()
|
||||
for i := 0; i < 10; i++ {
|
||||
go echoClient("5800-debug")
|
||||
}
|
||||
echoClient("5800-debug")
|
||||
}
|
||||
|
||||
func echoClient(peerNode string) {
|
||||
sendDatalen := op.ReadBuffLen
|
||||
sendBuff := make([]byte, sendDatalen)
|
||||
for i := 0; i < len(sendBuff); i++ {
|
||||
sendBuff[i] = byte('A' + i/100)
|
||||
}
|
||||
// peerNode = "YOUR-PEER-NODE-NAME"
|
||||
if err := op.GNetwork.ConnectNode(peerNode); err != nil {
|
||||
fmt.Println("connect error:", err)
|
||||
return
|
||||
}
|
||||
for i := 0; ; i++ {
|
||||
sendBuff[1] = 'A' + byte(i%26)
|
||||
if err := op.GNetwork.WriteNode(op.NodeNameToID(peerNode), sendBuff[:sendDatalen]); err != nil {
|
||||
fmt.Println("write error:", err)
|
||||
break
|
||||
}
|
||||
nd := op.GNetwork.ReadNode(time.Second * 10)
|
||||
if nd == nil {
|
||||
fmt.Printf("waiting for node data\n")
|
||||
time.Sleep(time.Second * 10)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("read %d len=%d data=%s\n", nd.NodeID, len(nd.Data), nd.Data[:16]) // only print 16 bytes
|
||||
}
|
||||
}
|
||||
32
example/echo/echo-server.go
Normal file
32
example/echo/echo-server.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
op "openp2p/core"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
op.Run()
|
||||
echoServer()
|
||||
forever := make(chan bool)
|
||||
<-forever
|
||||
}
|
||||
|
||||
func echoServer() {
|
||||
// peerID := fmt.Sprintf("%d", core.NodeNameToID(peerNode))
|
||||
for {
|
||||
nd := op.GNetwork.ReadNode(time.Second * 10)
|
||||
if nd == nil {
|
||||
fmt.Printf("waiting for node data\n")
|
||||
// time.Sleep(time.Second * 10)
|
||||
continue
|
||||
}
|
||||
// fmt.Printf("read %s len=%d data=%s\n", nd.Node, len(nd.Data), nd.Data[:16])
|
||||
nd.Data[0] = 'R' // echo server mark as replied
|
||||
if err := op.GNetwork.WriteNode(nd.NodeID, nd.Data); err != nil {
|
||||
fmt.Println("write error:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user