-
Notifications
You must be signed in to change notification settings - Fork 29
/
socket-tcp-server1.go
63 lines (54 loc) · 1.42 KB
/
socket-tcp-server1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//++++++++++++++++++++++++++++++++++++++++
// 《Go Web编程实战派从入门到精通》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goWebActualCombat
// 仓库地址:https://github.com/shirdonl/goWebActualCombat
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
_ "io"
"log"
"net"
)
func Server() {
// Listen函数创建的服务端
//tcp : 网络协议
//127.0.0.1:8088 / :8088 本机IP和端口
l, err := net.Listen("tcp", "127.0.0.1:8088")
if err != nil {
log.Fatal(err)
}
defer l.Close() //延时关闭listen
//循环等待客户端访问
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
fmt.Printf("访问客户端信息: con=%v 客户端ip=%v\n", conn, conn.RemoteAddr().String())
go handleConnection(conn)
}
}
//服务端处理从客户端接受的数据
func handleConnection(c net.Conn) {
defer c.Close() //关闭conn
for {
//1. 等待客户端通过conn发送信息
//2. 如果客户端没有wrtie[发送],那么协程就阻塞在这里
fmt.Printf("服务器在等待客户端%s 发送信息\n", c.RemoteAddr().String())
buf := make([]byte, 1024)
n, err := c.Read(buf)
if err != nil {
log.Fatal(err)
break
}
//3. 显示客户端发送的内容到服务器的终端
fmt.Print(string(buf[:n]))
}
}
func main() {
Server()
}