Golang net.IPConn.Close(), Read() and ReadFrom() functions example
package net
Golang net.IPConn.Close(), Read() and ReadFrom() functions usage example.
func handleConnection(c net.Conn) {
log.Printf("Client(TLS) %v connected via secure channel.", c.RemoteAddr())
// stuff to do... like read data from client, process it, write back to client
// see what you can do with (c net.Conn) at
// http://golang.org/pkg/net/#Conn
buffer := make([]byte, 4096)
for {
n, err := c.Read(buffer)
if err != nil || n == 0 {
c.Close()
break
}
// n, addr, err := c.ReadFrom(buffer)
// fmt.Println(addr.String())
// if err != nil || n == 0 {
// c.Close()
// break
// }
n, err = c.Write(buffer[0:n])
if err != nil {
c.Close()
break
}
}
log.Printf("Connection from %v closed.", c.RemoteAddr())
}
See also : https://www.socketloop.com/tutorials/golang-secure-tls-connection-between-server-and-client
References :
https://www.socketloop.com/tutorials/golang-secure-tls-connection-between-server-and-client
http://golang.org/pkg/net/#IPConn.Read
Advertisement
Something interesting
Tutorials
+15.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+26.1k Mac/Linux and Golang : Fix bind: address already in use error
+30.5k Get client IP Address in Go
+20k Golang : How to run your code only once with sync.Once object
+9.3k Golang : Generate EAN barcode
+16.5k Golang : File path independent of Operating System
+14.5k Golang : Overwrite previous output with count down timer
+10.2k Golang : Random Rune generator
+6.9k Mac/Linux/Windows : Get CPU information from command line
+8.8k Android Studio : Image button and button example
+21.1k Golang : Sort and reverse sort a slice of strings
+8.8k Golang : Heap sort example