Go语言(服务器开发):客户端向服务器发送数据并接收返回数据

来源:互联网 发布:大医医学数据库 编辑:程序博客网 时间:2024/06/05 08:30

客户端向服务器发送数据并接收返回数据。

示例代码:

package mysocketimport ("fmt""io""net")func MySocketBase() {var (host   = "www.apache.org"port   = "80"remote = host + ":" + portmsg    = "GET/ \n"data   = make([]uint8, 4096)count  = 0)// create the socketconn, err := net.Dial("tcp", remote)// send our message. an HTTP GET request in this caseio.WriteString(conn, msg)//conn.Write([]byte(msg))// read the response from the webserverfor {count, err = conn.Read(data)fmt.Printf(string(data[:count]))if err != nil {break}}conn.Close()}


0 0
原创粉丝点击