Golang network programming practice examples in the standard library

WBOY
Release: 2024-01-20 10:30:07
Original
1027 people have browsed it

Golang network programming practice examples in the standard library

Network programming practice in Golang standard library

Introduction:
Network programming is an essential part of modern software development, it can be used to implement customer Communication between client and server. Golang is a development framework and programming language with powerful network programming capabilities. This article will introduce some important network programming-related packages in the Golang standard library, as well as specific code examples.

Network programming related packages:
The Golang standard library provides some packages for network programming, the most important of which is the net package. The net package contains many practical functions and methods that can be used to implement various network programming tasks, such as creating clients and servers, sending and receiving data, etc. In addition to the net package, Golang also provides some other network programming-related packages, such as the http package for implementing HTTP protocol-related functions, and the rpc package for Implement remote procedure calls, etc.

Code example one: Create a simple server
The following example demonstrates how to use the net package in the Golang standard library to create a simple server that receives clients The request is sent and then returned to the client unchanged.

package main

import (
    "fmt"
    "net"
)

func handleConnection(conn net.Conn) {
    defer conn.Close()

    buffer := make([]byte, 1024)

    // 读取客户端发来的数据
    n, err := conn.Read(buffer)
    if err != nil {
        fmt.Println("Error reading:", err.Error())
        return
    }

    // 将收到的数据返回给客户端
    _, err = conn.Write(buffer[:n])
    if err != nil {
        fmt.Println("Error writing:", err.Error())
        return
    }
}

func main() {
    // 创建一个监听器,监听在本地的8888端口
    listener, err := net.Listen("tcp", "localhost:8888")
    if err != nil {
        fmt.Println("Error listening:", err.Error())
        return
    }
    defer listener.Close()

    fmt.Println("Server started, listening on localhost:8888")

    for {
        // 等待客户端连接
        conn, err := listener.Accept()
        if err != nil {
            fmt.Println("Error accepting:", err.Error())
            return
        }

        // 处理连接
        go handleConnection(conn)
    }
}
Copy after login

Code example two: Create a simple client
The following example demonstrates how to use the net package in the Golang standard library to create a simple client. The client sends a request to the server and receives the response data returned by the server.

package main

import (
    "fmt"
    "net"
)

func main() {
    // 连接服务器
    conn, err := net.Dial("tcp", "localhost:8888")
    if err != nil {
        fmt.Println("Error connecting:", err.Error())
        return
    }
    defer conn.Close()

    // 向服务器发送数据
    _, err = conn.Write([]byte("Hello, server!"))
    if err != nil {
        fmt.Println("Error sending:", err.Error())
        return
    }

    // 接收服务器返回的数据
    buffer := make([]byte, 1024)
    n, err := conn.Read(buffer)
    if err != nil {
        fmt.Println("Error reading:", err.Error())
        return
    }

    fmt.Println("Received from server:", string(buffer[:n]))
}
Copy after login

Conclusion:
The network programming-related packages in the Golang standard library provide powerful and easy-to-use tools that can be used to implement various network programming tasks. This article introduces the net package and some other important network programming related packages, and provides specific code examples. By learning and mastering these tools and techniques, we can more easily implement various types of web applications.

The above is the detailed content of Golang network programming practice examples in the standard library. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!