成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Go 語言高級網絡編程

開發 后端
Go(Golang)中的網絡編程具有易用性、強大性和樂趣。本指南深入探討了網絡編程的復雜性,涵蓋了協議、TCP/UDP 套接字、并發等方面的內容,并附有詳細的注釋。

一、簡介

Go(Golang)中的網絡編程具有易用性、強大性和樂趣。本指南深入探討了網絡編程的復雜性,涵蓋了協議、TCP/UDP 套接字、并發等方面的內容,并附有詳細的注釋。

二、關鍵概念

1. 網絡協議

  • TCP(傳輸控制協議):確保可靠的數據傳輸。
  • UDP(用戶數據報協議):更快,但不保證數據傳遞。

2. 套接字

  • TCP 套接字:用于面向連接的通信。
  • UDP 套接字:用于無連接通信。

3. 并發

  • Goroutines(協程):允許在代碼中實現并行處理。
  • Channels(通道):用于協程之間的通信。

三、示例

示例 1:TCP 服務器和客戶端

TCP 服務器和客戶端示例演示了TCP通信的基礎。

服務器:

package main

import (
 "net"
 "fmt"
)

func main() {
 // Listen on TCP port 8080 on all available unicast and
 // any unicast IP addresses.
 listen, err := net.Listen("tcp", ":8080")
 if err != nil {
  fmt.Println(err)
  return
 }
 defer listen.Close()

 // Infinite loop to handle incoming connections
 for {
  conn, err := listen.Accept()
  if err != nil {
   fmt.Println(err)
   continue
  }
  // Launch a new goroutine to handle the connection
  go handleConnection(conn)
 }
}

func handleConnection(conn net.Conn) {
 defer conn.Close()
 buffer := make([]byte, 1024)
 // Read the incoming connection into the buffer.
 _, err := conn.Read(buffer)
 if err != nil {
  fmt.Println(err)
  return
 }
 // Send a response back to the client.
 conn.Write([]byte("Received: " + string(buffer)))
}

客戶端:

package main

import (
 "net"
 "fmt"
)

func main() {
 // Connect to the server at localhost on port 8080.
 conn, err := net.Dial("tcp", "localhost:8080")
 if err != nil {
  fmt.Println(err)
  return
 }
 defer conn.Close()

 // Send a message to the server.
 conn.Write([]byte("Hello, server!"))
 buffer := make([]byte, 1024)
 // Read the response from the server.
 conn.Read(buffer)
 fmt.Println(string(buffer))
}

服務器在端口8080上等待連接,讀取傳入的消息并發送響應。客戶端連接到服務器,發送消息并打印服務器的響應。

示例 2:UDP 服務器和客戶端

與TCP不同,UDP是無連接的。以下是UDP服務器和客戶端的實現。

服務器:

package main

import (
 "net"
 "fmt"
)

func main() {
 // Listen for incoming UDP packets on port 8080.
 conn, err := net.ListenPacket("udp", ":8080")
 if err != nil {
  fmt.Println(err)
  return
 }
 defer conn.Close()

 buffer := make([]byte, 1024)
 // Read the incoming packet data into the buffer.
 n, addr, err := conn.ReadFrom(buffer)
 if err != nil {
  fmt.Println(err)
  return
 }
 fmt.Println("Received: ", string(buffer[:n]))
 // Write a response to the client's address.
 conn.WriteTo([]byte("Message received!"), addr)
}

客戶端:

package main

import (
 "net"
 "fmt"
)

func main() {
 // Resolve the server's address.
 addr, err := net.ResolveUDPAddr("udp", "localhost:8080")
 if err != nil {
  fmt.Println(err)
  return
 }

 // Dial a connection to the resolved address.
 conn, err := net.DialUDP("udp", nil, addr)
 if err != nil {
  fmt.Println(err)
  return
 }
 defer conn.Close()

 // Write a message to the server.
 conn.Write([]byte("Hello, server!"))
 buffer := make([]byte, 1024)
 // Read the response from the server.
 conn.Read(buffer)
 fmt.Println(string(buffer))
}

服務器從任何客戶端讀取消息并發送響應。客戶端發送消息并等待響應。

示例 3:并發 TCP 服務器

并發允許同時處理多個客戶端。

package main

import (
 "net"
 "fmt"
)

func main() {
 // Listen on TCP port 8080.
 listener, err := net.Listen("tcp", ":8080")
 if err != nil {
  fmt.Println(err)
  return
 }
 defer listener.Close()

 for {
  // Accept a connection.
  conn, err := listener.Accept()
  if err != nil {
   fmt.Println(err)
   continue
  }
  // Handle the connection in a new goroutine.
  go handleConnection(conn)
 }
}

func handleConnection(conn net.Conn) {
 defer conn.Close()
 buffer := make([]byte, 1024)
 // Read the incoming connection.
 conn.Read(buffer)
 fmt.Println("Received:", string(buffer))
 // Respond to the client.
 conn.Write([]byte("Message received!"))
}

通過為每個連接使用新的 goroutine,多個客戶端可以同時連接。

示例 4:帶有 Gorilla Mux 的 HTTP 服務器

Gorilla Mux 庫簡化了 HTTP 請求路由。

package main

import (
 "fmt"
 "github.com/gorilla/mux"
 "net/http"
)

func main() {
 // Create a new router.
 r := mux.NewRouter()
 // Register a handler function for the root path.
 r.HandleFunc("/", homeHandler)
 http.ListenAndServe(":8080", r)
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
 // Respond with a welcome message.
 fmt.Fprint(w, "Welcome to Home!")
}

這段代碼設置了一個 HTTP 服務器,并為根路徑定義了一個處理函數。

示例 5:HTTPS 服務器

實現 HTTPS 服務器可以確保安全通信。

package main

import (
 "net/http"
 "log"
)

func main() {
 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  // Respond with a message.
  w.Write([]byte("Hello, this is an HTTPS server!"))
 })
 // Use the cert.pem and key.pem files to secure the server.
 log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
}

服務器使用 TLS(傳輸層安全性)來加密通信。

示例 6:自定義 TCP 協議

可以使用自定義的 TCP 協議進行專門的通信。

package main

import (
 "net"
 "strings"
)

func main() {
 // Listen on TCP port 8080.
 listener, err := net.Listen("tcp", ":8080")
 if err != nil {
  panic(err)
 }
 defer listener.Close()

 for {
  // Accept a connection.
  conn, err := listener.Accept()
  if err != nil {
   panic(err)
  }
  // Handle the connection in a new goroutine.
  go handleConnection(conn)
 }
}

func handleConnection(conn net.Conn) {
 defer conn.Close()
 buffer := make([]byte, 1024)
 // Read the incoming connection.
 conn.Read(buffer)
 // Process custom protocol command.
 cmd := strings.TrimSpace(string(buffer))
 if cmd == "TIME" {
  conn.Write([]byte("The current time is: " + time.Now().String()))
 } else {
  conn.Write([]byte("Unknown command"))
 }
}

這段代碼實現了一個簡單的自定義協議,當客戶端發送命令“TIME”時,它會回復當前時間。

示例 7:使用 Gorilla WebSocket 進行 WebSockets

WebSockets 提供了通過單一連接的實時全雙工通信。

package main

import (
 "github.com/gorilla/websocket"
 "net/http"
)

var upgrader = websocket.Upgrader{
 ReadBufferSize:  1024,
 WriteBufferSize: 1024,
}

func handler(w http.ResponseWriter, r *http.Request) {
 conn, err := upgrader.Upgrade(w, r, nil)
 if err != nil {
  http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
  return
 }
 defer conn.Close()

 for {
  messageType, p, err := conn.ReadMessage()
  if err != nil {
   return
  }
  // Echo the message back to the client.
  conn.WriteMessage(messageType, p)
 }
}

func main() {
 http.HandleFunc("/", handler)
 http.ListenAndServe(":8080", nil)
}

WebSocket 服務器會將消息回傳給客戶端。

示例 8:連接超時

可以使用 context 包來管理連接超時。

package main

import (
 "context"
 "fmt"
 "net"
 "time"
)

func main() {
 // Create a context with a timeout of 2 seconds
 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
 defer cancel()

 // Dialer using the context
 dialer := net.Dialer{}
 conn, err := dialer.DialContext(ctx, "tcp", "localhost:8080")
 if err != nil {
  panic(err)
 }

 buffer := make([]byte, 1024)
 _, err = conn.Read(buffer)
 if err == nil {
  fmt.Println("Received:", string(buffer))
 } else {
  fmt.Println("Connection error:", err)
 }
}

這段代碼為從連接讀取數據設置了兩秒的截止時間。

示例 9:使用 golang.org/x/time/rate 進行速率限制

速率限制控制請求的速率。

package main

import (
 "golang.org/x/time/rate"
 "net/http"
 "time"
)

// Define a rate limiter allowing two requests per second with a burst capacity of five.
var limiter = rate.NewLimiter(2, 5)

func handler(w http.ResponseWriter, r *http.Request) {
 // Check if request is allowed by the rate limiter.
 if !limiter.Allow() {
  http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
  return
 }
 w.Write([]byte("Welcome!"))
}

func main() {
 http.HandleFunc("/", handler)
 http.ListenAndServe(":8080", nil)
}

此示例使用速率限制器,將請求速率限制為每秒兩個請求,突發容量為五個。

責任編輯:趙寧寧 來源: 技術的游戲
相關推薦

2023-05-24 09:31:51

CGo

2019-02-11 08:32:22

編程語言Go

2023-02-10 09:40:36

Go語言并發

2013-05-28 09:43:38

GoGo語言并發模式

2012-11-20 10:20:57

Go

2024-03-01 20:16:03

GoRust語言

2009-12-10 10:33:09

Go語言

2023-11-01 08:08:50

Go語言傳遞請求

2023-09-21 22:02:22

Go語言高級特性

2015-08-21 10:38:16

編程語言GoC語言

2020-12-30 09:04:32

Go語言TCPUDP

2012-03-15 14:25:22

Go

2024-01-08 07:02:48

數據設計模式

2022-09-19 00:29:01

編程語言Go 語言功能

2022-08-17 17:57:37

GoGo語言

2017-12-27 14:52:21

JSGo編程語言

2019-09-16 16:21:38

Go語言編程語言Python

2024-03-26 11:54:35

編程抽象代碼

2023-12-15 14:38:00

GoRust編程語言

2025-02-05 08:13:48

Go語言范式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 激情六月丁香 | 日韩精品一区二区三区视频播放 | 国产日韩一区二区三免费高清 | 久久国 | 亚洲成人中文字幕 | 亚洲一区二区不卡在线观看 | 亚洲国产精品99久久久久久久久 | 精品国模一区二区三区欧美 | 九色在线观看 | 亚洲自拍一区在线观看 | 日本黄色一级片视频 | japan21xxxxhd美女 日本欧美国产在线 | www.se91| xx性欧美肥妇精品久久久久久 | 久久成人免费 | 久久99精品久久久水蜜桃 | 一区二区三区四区免费视频 | 欧美 日韩 在线播放 | 日本精品一区二区 | 日韩高清国产一区在线 | www成人免费 | www国产亚洲精品久久网站 | 国产乡下妇女做爰 | 一区二区三区欧美 | 伊人在线| 国产欧美日韩综合精品一区二区 | 日韩一区在线播放 | 久久一区二区免费视频 | 在线免费视频一区 | 九九综合 | 免费欧美 | 日韩视频一区在线观看 | 乱码av午夜噜噜噜噜动漫 | 日韩av黄色 | 亚洲欧美视频一区 | 91视频免费观看 | 国产午夜精品久久久 | 玖玖综合网 | 亚洲一区免费在线 | 91电影| av在线免费播放 |