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
+7.5k Golang : How to handle file size larger than available memory panic issue
+7.9k Golang : Grayscale Image
+11k Golang : Create Temporary File
+18.9k Golang : Read input from console line
+17.7k Golang : Read data from config file and assign to variables
+6.9k Nginx : Password protect a directory/folder
+19.4k Golang : How to count the number of repeated characters in a string?
+21.8k Golang : Convert string slice to struct and access with reflect example
+6.3k Unix/Linux : Use netstat to find out IP addresses served by your website server
+15.2k Golang : Get timezone offset from date or timestamp
+8.4k Golang : Convert word to its plural form example
+25.2k Golang : Storing cookies in http.CookieJar example