more
This commit is contained in:
parent
c1d067e350
commit
4928f24e79
18
xx-signals.go
Normal file
18
xx-signals.go
Normal 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
49
xx-tcp-server.go
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user