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
+8.2k Golang : Metaprogramming example of wrapping a function
+16.6k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+13.4k Golang : Verify token from Google Authenticator App
+5.8k Golang : Launching your executable inside a console under Linux
+11.6k Golang : Simple file scaning and remove virus example
+14.4k Golang : Recombine chunked files example
+18.4k Golang : Read binary file into memory
+6.6k Golang : Totalize or add-up an array or slice example
+3.7k Golang : Switch Redis database redis.NewClient
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+7.5k Golang : Process json data with Jason package