Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GoでSocket通信プログラム(クライアント側)
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s message", os.Args[0])
os.Exit(1)
}
message := os.Args[1]
serverIP := "192.xxx.xxx.xxx" //サーバ側のIP
serverPort := "55555" //サーバ側のポート番号
myIP := "192.xxx.xxx.xxx" //クライアント側のIP
myPort := 55556 //クライアント側のポート番号
tcpAddr, err := net.ResolveTCPAddr("tcp", serverIP+":"+serverPort)
checkError(err)
myAddr := new(net.TCPAddr)
myAddr.IP = net.ParseIP(myIP)
myAddr.Port = myPort
conn, err := net.DialTCP("tcp", myAddr, tcpAddr)
checkError(err)
defer conn.Close()
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
conn.Write([]byte(message))
readBuf := make([]byte, 1024)
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
readlen, err := conn.Read(readBuf)
checkError(err)
fmt.Println("server: " + string(readBuf[:readlen]))
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: error: %s", err.Error())
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.