This commit is contained in:
Mark McGranaghan 2012-09-16 18:31:45 -07:00
parent c1d067e350
commit 4928f24e79
3 changed files with 67 additions and 0 deletions

18
xx-signals.go Normal file
View File

@ -0,0 +1,18 @@
package main
import ("fmt"; "os"; "os/signal"; "syscall")
func main() {
c := make(chan os.Signal, 1)
d := make(chan bool, 1)
signal.Notify(c, syscall.SIGINT)
go func(){
sig := <- c
fmt.Println()
fmt.Println(sig)
d <- true
}()
fmt.Println("Awaiting signal")
<- d
}

49
xx-tcp-server.go Normal file
View File

@ -0,0 +1,49 @@
package main
package main
import (
"encoding/gob"
"fmt"
"net"
)
func server() {
// listen on a port
ln, err := net.Listen("tcp", ":9999")
if err != nil {
fmt.Println(err)
return
}
for {
// accept a connection
c, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
// handle the connection
go handleServerConnection(c)
}
}
func handleServerConnection(c net.Conn) {
// receive the message
var msg string
err := gob.NewDecoder(c).Decode(&msg)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Received", msg)
}
c.Close()
}
func main() {
go server()
go client()
var input string
fmt.Scanln(&input)
}