added keyboard interrupt catch to server

This commit is contained in:
2024-06-17 18:46:14 -04:00
parent f5bf217243
commit 0bd27c5a4c
+21 -10
View File
@@ -1,11 +1,10 @@
// ============================================================================= // =============================================================================
// Auth: alex // Auth: alex
// File: server.go // File: server.go
// Revn: 06-14-2024 4.0 // Revn: 06-17-2024 4.1
// Func: host MOTD connection // Func: host MOTD connection
// //
// TODO: catch keyboard int. signal // TODO: remove ioutil import
// remove ioutil import
// add more /codes/ to handle // add more /codes/ to handle
// add flags, like specify port // add flags, like specify port
// write usage and errors to logfile // write usage and errors to logfile
@@ -43,6 +42,7 @@
// 06-13-2024: removed debug print statements // 06-13-2024: removed debug print statements
// commented // commented
//*06-14-2024: byte count-based PWFT rewrite //*06-14-2024: byte count-based PWFT rewrite
//*06-17-2024: catch keyboard interrupts with os/signal and syscall
// //
// ============================================================================= // =============================================================================
@@ -55,9 +55,11 @@ import (
"io/ioutil" // ReadFile "io/ioutil" // ReadFile
"net" // ResovleTCPAddr, ListenTCP, listener.Accept "net" // ResovleTCPAddr, ListenTCP, listener.Accept
// conn.Read,Write // conn.Read,Write
"os" // Exit "os" // Exit, Create, os.Signal
"strconv" // Itoa "os/signal" // Notify
"strconv" // Itoa, Atoi
"strings" // Split "strings" // Split
"syscall" // syscall.SIGINT,SIGTERM
) )
@@ -206,7 +208,7 @@ func handle( conn net.Conn ) {
} else { } else {
// get slice from start index to end, concat // get slice from start index to end, concat
// with response // with response
resp = resp + qfile[num:end] resp = resp + qfile[bytes:end]
} }
} }
} else { // file is smaller than the tx size } else { // file is smaller than the tx size
@@ -298,6 +300,11 @@ var tsize int // size of transmission buffer
// main, create port and wait for connection // main, create port and wait for connection
func main() { func main() {
// create buffered channel for catching keyboard interrupt signal
sigs := make( chan os.Signal, 1 )
// set interrupt and terminate signals to notify channel
signal.Notify( sigs, syscall.SIGINT, syscall.SIGTERM )
// flag for default transmission size // flag for default transmission size
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" ) flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
flag.Parse() flag.Parse()
@@ -313,15 +320,19 @@ func main() {
// create listener object from ip:port // create listener object from ip:port
listener, err := net.ListenTCP( "tcp", addr ) listener, err := net.ListenTCP( "tcp", addr )
go func() { // keyboard interrupt thread
// print kill message
fmt.Println( "press ctrl+C to stop..." )
<- sigs // blocking read of signal from channel
fmt.Println( "\nexiting" ) // exit message
os.Exit( 0 ) // exeunt
}()
for { for {
// wait, create connection when found // wait, create connection when found
conn, err := listener.Accept() conn, err := listener.Accept()
check( err ) // make sure connection works check( err ) // make sure connection works
go handle( conn ) // handle connection go handle( conn ) // handle connection
} }
os.Exit( 0 ) // exeunt
} }