Golang net.Pipe() function example
package net
Golang net.Pipe() function usage example
package main
import (
"net"
)
func Client(c net.Conn) { ... }
func Server(c net.Conn) { ... }
func main() {
c1, c2 := net.Pipe()
go Server(c1)
Client(c2)
msg := "Message to client"
_, err := c1.Write([]byte(msg))
if err != nil {
fmt.Printf("Unable to send message : %v\n", err)
break
} else {
fmt.Printf("Sent : %s", msg)
msg = ""
}
}
NOTE : You can use net.Listener to setup net.Conn as well. For example :
func AcceptConnections(listener net.Listener) {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
fmt.Printf("client connected\n")
....
}
Reference :
Advertisement
Something interesting
Tutorials
+8.1k Golang : Multiplexer with net/http and map
+5.8k Golang : Markov chains to predict probability of next state example
+21.1k Golang : Get password from console input without echo or masked
+7.7k Golang : Error reading timestamp with GORM or SQL driver
+21.5k Golang : How to read float value from standard input ?
+21.8k Golang : How to reverse slice or array elements order
+7.9k Golang : Ways to recover memory during run time.
+13.7k Golang : Check if an integer is negative or positive
+8.1k Golang : How To Use Panic and Recover
+18.7k Unmarshal/Load CSV record into struct in Go
+20.2k Golang : Compare floating-point numbers
+10.3k Golang : Embed secret text string into binary(executable) file