|
|
@ -1,19 +1,21 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"bufio" |
|
|
|
|
|
|
|
"flag" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"strconv" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
|
message = "init http://localhost:8080/configuration.txt" |
|
|
|
message = "init http://localhost:8080/configuration.txt" |
|
|
|
|
|
|
|
defaultPort = 6000 |
|
|
|
StopCharacter = "\r\n\r\n" |
|
|
|
StopCharacter = "\r\n\r\n" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func SocketClient(ip string, port int) { |
|
|
|
func SocketClient(addr string) { |
|
|
|
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":") |
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", addr) |
|
|
|
conn, err := net.Dial("tcp", addr) |
|
|
|
|
|
|
|
|
|
|
|
defer conn.Close() |
|
|
|
defer conn.Close() |
|
|
@ -28,18 +30,39 @@ func SocketClient(ip string, port int) { |
|
|
|
|
|
|
|
|
|
|
|
buff := make([]byte, 1024) |
|
|
|
buff := make([]byte, 1024) |
|
|
|
n, _ := conn.Read(buff) |
|
|
|
n, _ := conn.Read(buff) |
|
|
|
log.Printf("Receive from %v: %s", port, buff[:n]) |
|
|
|
log.Printf("Receive from %s: %s", addr, buff[:n]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
|
|
|
|
configFile := flag.String("config_file", "config.txt", "file containing all ip addresses") |
|
|
|
|
|
|
|
flag.Parse() |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
configs := readConfigFile(*configFile) |
|
|
|
ip = "127.0.0.1" |
|
|
|
|
|
|
|
portList = []int{3333, 4444} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, port := range portList { |
|
|
|
for _, config := range configs { |
|
|
|
SocketClient(ip, port) |
|
|
|
ip := config[0] |
|
|
|
|
|
|
|
port := config[1] |
|
|
|
|
|
|
|
addr := strings.Join([]string{ip, port}, ":") |
|
|
|
|
|
|
|
SocketClient(addr) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func readConfigFile(configFile string) [][]string { |
|
|
|
|
|
|
|
file, _ := os.Open(configFile) |
|
|
|
|
|
|
|
fscanner := bufio.NewScanner(file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result := [][]string{} |
|
|
|
|
|
|
|
for fscanner.Scan() { |
|
|
|
|
|
|
|
p := strings.Split(fscanner.Text(), " ") |
|
|
|
|
|
|
|
result = append(result, p) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func Map(vs []string, f func(string) string) []string { |
|
|
|
|
|
|
|
vsm := make([]string, len(vs)) |
|
|
|
|
|
|
|
for i, v := range vs { |
|
|
|
|
|
|
|
vsm[i] = f(v) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return vsm |
|
|
|
} |
|
|
|
} |
|
|
|