diff --git a/xx-exit.go b/xx-exits.go similarity index 100% rename from xx-exit.go rename to xx-exits.go diff --git a/xx-signals.go b/xx-signals.go new file mode 100644 index 0000000..dab0787 --- /dev/null +++ b/xx-signals.go @@ -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 +} diff --git a/xx-tcp-server.go b/xx-tcp-server.go new file mode 100644 index 0000000..530c57c --- /dev/null +++ b/xx-tcp-server.go @@ -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) +} \ No newline at end of file