Created
March 7, 2013 00:49
-
-
Save whyrusleeping/5104600 to your computer and use it in GitHub Desktop.
Sample code for tonights tech club talk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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