更新udp
This commit is contained in:
parent
bad6325f62
commit
d2a4c831a4
@ -79,6 +79,24 @@
|
||||
<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>
|
||||
<mxCell id="32" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="85" y="1050" width="590" height="190" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="33" value="提供程序执行能力的执行器" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="340" y="1010" width="170" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="34" value="os process<div>go run main.go</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="140" y="1115" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="35" value="os thread<div><br></div><div>thread.Run()</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="310" y="1115" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="36" value="协程序(用户空间 实现的任务调度执行)<div>go func()</div>" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="470" y="1115" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="37" value="Goroutine 用户态线程" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="680" y="1130" width="130" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
<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">
|
||||
<mxGraphModel dx="894" dy="483" 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 id="3" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
<mxGeometry x="80" y="30" width="630" height="140" 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"/>
|
||||
@ -26,7 +26,7 @@
|
||||
</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"/>
|
||||
<mxPoint x="-3" y="13" as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="端2" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
|
||||
@ -41,7 +41,7 @@
|
||||
</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"/>
|
||||
<mxGeometry x="440" 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"/>
|
||||
@ -63,6 +63,18 @@
|
||||
<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>
|
||||
<mxCell id="21" value="goroutine1&nbsp;<br>(connection1)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="110" y="50" width="120" height="50" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="22" value="goroutine2<br>(connection2)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="260" y="50" width="120" height="50" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="23" value="goroutine3<br>(connection3)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="430" y="50" width="120" height="50" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="24" value="goroutine4<br>(connection4)" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="575" y="50" width="120" height="50" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
|
||||
2
day09/upd/README.md
Normal file
2
day09/upd/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# UDP编程
|
||||
|
||||
2
day09/upd/client/README.md
Normal file
2
day09/upd/client/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# 客户端
|
||||
|
||||
39
day09/upd/client/main.go
Normal file
39
day09/upd/client/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. 连接服务器
|
||||
conn, err := net.Dial("udp", "localhost:9999")
|
||||
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, 1500)
|
||||
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")
|
||||
}
|
||||
2
day09/upd/server/README.md
Normal file
2
day09/upd/server/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# 服务端
|
||||
|
||||
40
day09/upd/server/main.go
Normal file
40
day09/upd/server/main.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 监听 UDP 9999,可接收任意客户端数据报
|
||||
pc, err := net.ListenPacket("udp", ":9999")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer pc.Close()
|
||||
fmt.Println("Server is listening on port 9999")
|
||||
|
||||
// 循环读取数据
|
||||
for {
|
||||
// 准备一个buf来读取包的内容
|
||||
buf := make([]byte, 1024)
|
||||
n, addr, err := pc.ReadFrom(buf)
|
||||
// 如果读取失败,则继续下一次循环
|
||||
if err != nil {
|
||||
fmt.Println("Error reading data:", err)
|
||||
continue
|
||||
}
|
||||
// 业务处理: 打印读取到的数据
|
||||
fmt.Println("Read", n, "bytes from", addr)
|
||||
fmt.Println("Data:", string(buf[:n])+"\n")
|
||||
|
||||
// 写入数据(回复客户端, 回一个包)
|
||||
_, err = pc.WriteTo([]byte("Hello, Client! I have received your message: "+string(buf[:n])+"\n"), addr)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing data:", err)
|
||||
continue
|
||||
}
|
||||
fmt.Println("Written", len(buf), "bytes to", addr)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user