Created
March 14, 2025 15:25
-
-
Save tiesmaster/6b20fafccee5b2ee5890138c9c48b4b8 to your computer and use it in GitHub Desktop.
check-tcp-socket-liveness-in-golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"time" | |
) | |
func main() { | |
// Connect to the server | |
// conn, err := net.Dial("tcp", "localhost:8080") | |
conn, err := net.Dial("tcp", "1.2.3.4:80") | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
for { | |
// Send some data to the server | |
_, err = conn.Write([]byte("GET / HTTP/1.1\nHost: example.com\nConnection: keep-alive\n\n")) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
var buff [1024]byte | |
_, err = conn.Read(buff[:]) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} else { | |
fmt.Println("Socket is still open...") | |
} | |
time.Sleep(2 * time.Second) | |
} | |
// var buff [1024]byte | |
// i, err2 := conn.Read(buff[:]) | |
// if err2 != nil { | |
// fmt.Println(err2) | |
// return | |
// } else { | |
// fmt.Println(string(buff[:i])) | |
// } | |
// Close the connection | |
conn.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment