Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created March 7, 2013 00:49
Show Gist options
  • Save whyrusleeping/5104600 to your computer and use it in GitHub Desktop.
Save whyrusleeping/5104600 to your computer and use it in GitHub Desktop.
Sample code for tonights tech club talk
package main
import (
"net"
)
func main() {
addr,_ := net.ResolveTCPAddr("tcp","jero.my:10234")
con, err := net.DialTCP("tcp",nil,addr)
if err != nil {
panic(err)
}
con.Write([]byte("Hello World!"))
con.Close()
}
package main
import (
"fmt"
"net"
)
func handleCon(c net.Conn) {
b := make([]byte, 1) //Read one byte at a time
str := ""
for {
_, err := c.Read(b)
if err != nil {
break
}
str = str + string(b)
}
fmt.Println(str)
c.Close()
}
func main() {
listener, err := net.Listen("tcp",":10234")
if err != nil {
panic(err)
}
for {
con, _ := listener.Accept()
go handleCon(con)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment