本文實(shí)例講述了Go語(yǔ)言服務(wù)器開(kāi)發(fā)之客戶(hù)端向服務(wù)器發(fā)送數(shù)據(jù)并接收返回?cái)?shù)據(jù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
package mysocket
import (
"fmt"
"io"
"net"
)
func MySocketBase() {
var (
host = "www.apache.org"
port = "80"
remote = host + ":" + port
msg = "GET/ \n"
data = make([]uint8, 4096)
count = 0
)
// create the socket
conn, err := net.Dial("tcp", remote)
// send our message. an HTTP GET request in this case
io.WriteString(conn, msg)
//conn.Write([]byte(msg))
// read the response from the webserver
for {
count, err = conn.Read(data)
fmt.Printf(string(data[:count]))
if err != nil {
break
}
}
conn.Close()
}
希望本文所述對(duì)大家的Go語(yǔ)言程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- golang簡(jiǎn)單tls協(xié)議用法完整示例
- go語(yǔ)言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法
- GO語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)的方法
- Go語(yǔ)言服務(wù)器開(kāi)發(fā)之簡(jiǎn)易TCP客戶(hù)端與服務(wù)端實(shí)現(xiàn)方法
- Go語(yǔ)言基于Socket編寫(xiě)服務(wù)器端與客戶(hù)端通信的實(shí)例
- go語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的http客戶(hù)端抓取遠(yuǎn)程url的方法
- Golang實(shí)現(xiàn)的聊天程序服務(wù)端和客戶(hù)端代碼分享
- golang實(shí)現(xiàn)簡(jiǎn)單的udp協(xié)議服務(wù)端與客戶(hù)端示例