parent
2df44e2085
commit
12e19bf2ee
@ -1,35 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Initialize the random seed
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Define flags
|
||||
index := flag.Int("i", 0, "Index number")
|
||||
config := flag.String("c", "", "Configuration path")
|
||||
|
||||
// Parse the flags
|
||||
flag.Parse()
|
||||
|
||||
// Print the values of the flags
|
||||
fmt.Printf("args: -i %d -c %s\n", *index, *config)
|
||||
|
||||
// Generate a random number (0 or 1) and subtract 1 to get 0 or -1
|
||||
randomValue := rand.Intn(2) - 1
|
||||
|
||||
if randomValue == 0 {
|
||||
fmt.Println("Sleeping for 500 seconds...")
|
||||
time.Sleep(500 * time.Second)
|
||||
} else if randomValue == -1 {
|
||||
fmt.Fprintln(os.Stderr, "An error occurred. Exiting with status -1.")
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define flags
|
||||
index := flag.Int("i", 0, "Index number")
|
||||
config := flag.String("c", "", "Configuration path")
|
||||
|
||||
// Parse the flags
|
||||
flag.Parse()
|
||||
|
||||
// Print the values of the flags
|
||||
fmt.Printf("args: -i %d -c %s\n", *index, *config)
|
||||
|
||||
// Initialize the random seed
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// Randomly select two ports between 10000 and 20000
|
||||
port1 := rand.Intn(10001) + 10000
|
||||
port2 := rand.Intn(10001) + 10000
|
||||
// Ensure port2 is different from port1
|
||||
for port2 == port1 {
|
||||
port2 = rand.Intn(10001) + 10000
|
||||
}
|
||||
|
||||
fmt.Printf("Randomly selected TCP ports to listen on: %d, %d\n", port1, port2)
|
||||
|
||||
// Listen on the selected ports
|
||||
go listenOnPort(port1)
|
||||
go listenOnPort(port2)
|
||||
|
||||
// Wait for a termination signal to gracefully shutdown
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
|
||||
fmt.Println("Shutting down...")
|
||||
}
|
||||
|
||||
func listenOnPort(port int) {
|
||||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
||||
if err != nil {
|
||||
log.Fatalf("Error listening on port %d: %v", port, err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
fmt.Printf("Listening on port %d...\n", port)
|
||||
|
||||
// Normally, you would accept connections with l.Accept() in a loop here.
|
||||
// For this example, just sleep indefinitely to simulate a service.
|
||||
select {}
|
||||
|
||||
}
|
Loading…
Reference in new issue