You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
605 B
45 lines
605 B
6 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"net"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
message = "Ping"
|
||
|
StopCharacter = "\r\n\r\n"
|
||
|
)
|
||
|
|
||
|
func SocketClient(ip string, port int) {
|
||
|
addr := strings.Join([]string{ip, strconv.Itoa(port)}, ":")
|
||
|
conn, err := net.Dial("tcp", addr)
|
||
|
|
||
|
defer conn.Close()
|
||
|
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
conn.Write([]byte(message))
|
||
|
conn.Write([]byte(StopCharacter))
|
||
|
log.Printf("Send: %s", message)
|
||
|
|
||
|
buff := make([]byte, 1024)
|
||
|
n, _ := conn.Read(buff)
|
||
|
log.Printf("Receive: %s", buff[:n])
|
||
|
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
|
||
|
var (
|
||
|
ip = "127.0.0.1"
|
||
|
port = 3333
|
||
|
)
|
||
|
|
||
|
SocketClient(ip, port)
|
||
|
|
||
|
}
|