diff --git a/day09/goroutine/README.md b/day09/goroutine/README.md new file mode 100644 index 0000000..77f3cb6 --- /dev/null +++ b/day09/goroutine/README.md @@ -0,0 +1 @@ +# goroutine \ No newline at end of file diff --git a/day09/goroutine/arch.drawio b/day09/goroutine/arch.drawio new file mode 100644 index 0000000..99eedd9 --- /dev/null +++ b/day09/goroutine/arch.drawio @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day09/tcp/README.md b/day09/tcp/README.md new file mode 100644 index 0000000..ce1364a --- /dev/null +++ b/day09/tcp/README.md @@ -0,0 +1,2 @@ +# TCP编程 + diff --git a/day09/tcp/client/README.md b/day09/tcp/client/README.md new file mode 100644 index 0000000..17d584d --- /dev/null +++ b/day09/tcp/client/README.md @@ -0,0 +1,2 @@ +# TCP Client + diff --git a/day09/tcp/client/main.go b/day09/tcp/client/main.go new file mode 100644 index 0000000..aacd8d9 --- /dev/null +++ b/day09/tcp/client/main.go @@ -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") +} diff --git a/day09/tcp/flow.drawio b/day09/tcp/flow.drawio new file mode 100644 index 0000000..df6c1ca --- /dev/null +++ b/day09/tcp/flow.drawio @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day09/tcp/server/README.md b/day09/tcp/server/README.md new file mode 100644 index 0000000..826880c --- /dev/null +++ b/day09/tcp/server/README.md @@ -0,0 +1 @@ +# TCP Server \ No newline at end of file diff --git a/day09/tcp/server/main.go b/day09/tcp/server/main.go new file mode 100644 index 0000000..b2c7657 --- /dev/null +++ b/day09/tcp/server/main.go @@ -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") + } +} \ No newline at end of file