mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
Create socket.go
This commit is contained in:
30
eBook/examples/chapter_15/socket.go
Normal file
30
eBook/examples/chapter_15/socket.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var (
|
||||||
|
host = "www.apache.org"
|
||||||
|
port = "80"
|
||||||
|
remote = host + ":" + port
|
||||||
|
msg string = "GET / \n"
|
||||||
|
data = make([]uint8, 4096)
|
||||||
|
read = true
|
||||||
|
count = 0
|
||||||
|
)
|
||||||
|
// 创建一个socket
|
||||||
|
con, err := net.Dial("tcp", remote)
|
||||||
|
// 发送我们的消息,一个http GET请求
|
||||||
|
io.WriteString(con, msg)
|
||||||
|
// 读取服务器的响应
|
||||||
|
for read {
|
||||||
|
count, err = con.Read(data)
|
||||||
|
read = (err == nil)
|
||||||
|
fmt.Printf(string(data[0:count]))
|
||||||
|
}
|
||||||
|
con.Close()
|
||||||
|
}
|
Reference in New Issue
Block a user