update tpc
This commit is contained in:
parent
749fc1e1ed
commit
bad6325f62
1
day09/goroutine/README.md
Normal file
1
day09/goroutine/README.md
Normal file
@ -0,0 +1 @@
|
||||
# goroutine
|
||||
85
day09/goroutine/arch.drawio
Normal file
85
day09/goroutine/arch.drawio
Normal file
@ -0,0 +1,85 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="ntcMOG5Spn2z-IJ_tN7C" name="第 1 页">
|
||||
<mxGraphModel dx="1134" dy="632" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="110" y="230" width="320" height="320" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="同步编程" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="230" y="180" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="4" value="connection1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="170" y="250" width="210" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="5" value="connection2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="170" y="310" width="210" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="<span style="color: rgb(0, 0, 0);">connection3</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="170" y="370" width="210" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="510" y="230" width="360" height="320" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="并行编程" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="650" y="180" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" value="<span style="color: rgb(0, 0, 0);">connection1</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="540" y="340" width="90" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="<span style="color: rgb(0, 0, 0);">connection2</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="650" y="340" width="90" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="<span style="color: rgb(0, 0, 0);">connection3</span>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="750" y="340" width="90" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="15" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="110" y="700" width="120" height="250" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="16" value="并行执行流<div>connection1</div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="130" y="660" width="80" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="320" y="695" width="120" height="250" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="并行执行流<div>connection2</div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="340" y="655" width="80" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="20" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="520" y="700" width="120" height="250" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="21" value="并行执行流<div>connection3</div>" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="540" y="660" width="80" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="22" value="step 1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="120" y="730" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="23" value="step 2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="120" y="780" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="24" value="step 3" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="120" y="830" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="25" value="step 1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="725" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="26" value="step 2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="775" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="27" value="step 3" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="825" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="28" value="step 1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="530" y="730" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="29" value="step 2" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="530" y="780" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="30" value="step 3" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="530" y="830" width="100" height="40" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
2
day09/tcp/README.md
Normal file
2
day09/tcp/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# TCP编程
|
||||
|
||||
2
day09/tcp/client/README.md
Normal file
2
day09/tcp/client/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# TCP Client
|
||||
|
||||
39
day09/tcp/client/main.go
Normal file
39
day09/tcp/client/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. 连接服务器
|
||||
conn, err := net.Dial("tcp", "localhost:8080")
|
||||
if err != nil {
|
||||
fmt.Println("Error connecting:", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
fmt.Println("Connected to server")
|
||||
|
||||
// 2. 发送数据
|
||||
_, err = conn.Write([]byte("Hello, Server! I am a client."))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing data:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Written 22 bytes:", "Hello, Server! I am a client.")
|
||||
|
||||
// 3. 接收数据
|
||||
buf := make([]byte, 1024)
|
||||
n, err := conn.Read(buf)
|
||||
if err == io.EOF {
|
||||
fmt.Println("Server closed connection")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Error reading data:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Read", len(buf), "bytes:", "Hello, Server! I have received your message: "+string(buf[:n])+"\n")
|
||||
}
|
||||
69
day09/tcp/flow.drawio
Normal file
69
day09/tcp/flow.drawio
Normal file
@ -0,0 +1,69 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="3PNk-fw2sB_go403P8wj" name="第 1 页">
|
||||
<mxGraphModel dx="1134" dy="632" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="3" value="progress<div><br></div>" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="160" y="110" width="350" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="4" value="net.Listen" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="275" y="160" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="5" target="4" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="13" value="connection1" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="9" vertex="1" connectable="0">
|
||||
<mxGeometry x="-0.7157" y="1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="5" value="端1" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="80" y="410" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="6" target="4" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" value="connection2" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="10" vertex="1" connectable="0">
|
||||
<mxGeometry x="-0.3364" y="-3" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="端2" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="275" y="410" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="7" target="4" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="15" value="connection3" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="11" vertex="1" connectable="0">
|
||||
<mxGeometry x="-0.6603" y="1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="7" value="端3" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="420" y="410" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" parent="1" source="8" target="4" edge="1">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="16" value="connection4" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="12" vertex="1" connectable="0">
|
||||
<mxGeometry x="-0.7798" y="2" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="端4" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="600" y="410" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="{}" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="595" y="190" width="30" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="18" value="{}" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="645" y="190" width="30" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="{}" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" parent="1" vertex="1">
|
||||
<mxGeometry x="695" y="190" width="30" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
1
day09/tcp/server/README.md
Normal file
1
day09/tcp/server/README.md
Normal file
@ -0,0 +1 @@
|
||||
# TCP Server
|
||||
72
day09/tcp/server/main.go
Normal file
72
day09/tcp/server/main.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"io"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. 监听端口
|
||||
listener, err := net.Listen("tcp", ":8080")
|
||||
if err != nil {
|
||||
fmt.Println("Error listening:", err)
|
||||
return
|
||||
}
|
||||
defer listener.Close()
|
||||
fmt.Println("Server is listening on port 8080")
|
||||
|
||||
// 2. 接受连接
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Error accepting connection:", err)
|
||||
continue
|
||||
}
|
||||
// 3. 处理连接
|
||||
// 使用 goroutine 并发处理连接
|
||||
// 这样就可以同时处理多个连接,提高并发能力 (非阻塞)
|
||||
go handleConnection(conn)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理连接
|
||||
// 1. 读取数据
|
||||
// 2. 处理数据
|
||||
// 3. 写入数据
|
||||
func handleConnection(conn net.Conn) {
|
||||
defer func() {
|
||||
conn.Close()
|
||||
fmt.Println("Connection closed by client")
|
||||
}()
|
||||
|
||||
fmt.Println("New connection accepted")
|
||||
|
||||
// 4. 循环读取数据
|
||||
for {
|
||||
// 4.1 读取数据
|
||||
buf := make([]byte, 1024)
|
||||
n, err := conn.Read(buf)
|
||||
// 如果客户端关闭连接,则退出循环
|
||||
if err == io.EOF {
|
||||
fmt.Println("Client closed connection")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Error reading data:", err)
|
||||
break
|
||||
}
|
||||
|
||||
// 4.3 处理数据
|
||||
fmt.Println("Read", n, "bytes:", string(buf[:n]))
|
||||
|
||||
|
||||
// 4.2 写入数据
|
||||
_, err = conn.Write([]byte("Hello, Client! I have received your message: " + string(buf[:n]) + "\n"))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing data:", err)
|
||||
break
|
||||
}
|
||||
fmt.Println("Written", n, "bytes:", "Hello, Client! I have received your message: " + string(buf[:n]) + "\n")
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user