Better closing of connections

Improved the handling about how to close the connections.
This commit is contained in:
Thorsten Sommer 2015-01-03 23:09:41 +01:00
parent ffd85697ed
commit 687ecb2d99

View File

@ -9,6 +9,7 @@ import (
func forward(localConn net.Conn, config *ssh.ClientConfig) { func forward(localConn net.Conn, config *ssh.ClientConfig) {
defer localConn.Close()
currentRetriesServer := 0 currentRetriesServer := 0
currentRetriesRemote := 0 currentRetriesRemote := 0
var sshClientConnection *ssh.Client = nil var sshClientConnection *ssh.Client = nil
@ -39,6 +40,7 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) {
// Success: // Success:
log.Println(`Connected to the SSH server ` + serverAddrString) log.Println(`Connected to the SSH server ` + serverAddrString)
sshClientConnection = sshClientConn sshClientConnection = sshClientConn
defer sshClientConnection.Close()
break break
} }
} }
@ -67,6 +69,7 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) {
// Fine, the connections are up and ready :-) // Fine, the connections are up and ready :-)
log.Printf("The remote end-point %s is connected.\n", remoteAddrString) log.Printf("The remote end-point %s is connected.\n", remoteAddrString)
defer sshConn.Close()
// To be able to close down both transfer threads, we create a channel: // To be able to close down both transfer threads, we create a channel:
quit := make(chan bool) quit := make(chan bool)
@ -88,10 +91,6 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) {
// Now, close all the channels and therefore, force the other / second thread to go down: // Now, close all the channels and therefore, force the other / second thread to go down:
log.Println(`Close now all connections.`) log.Println(`Close now all connections.`)
sshConn.Close()
localConn.Close()
sshClientConnection.Close()
return return
} }
} }