GolangNote

Golang笔记

golang 简单网络服务编程入门

Permalink

做一个简单的server,每到来一个连接,就启用一个新的 goroutine 来进行处理

Go: 网络服务编程入门
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
func main() {
    ln, err := net.Listen("tcp", ":8080")
    checkError(err)
    for {
        conn, err := ln.Accept()
        if err != nil {
            continue
        }
        go run(conn)

    }
}

func run(conn net.Conn) {
    buffer := make([]byte, 1024)
    defer conn.Close()

    for {
        n, err := conn.Read(buffer)
        if err != nil {
            return
        }
        fmt.Println("read:", string(buffer[:n]))
    }
}

本文网址: https://golangnote.com/topic/153.html 转摘请注明来源

Related articles

Golang quicktemplate 模版快速入门

Golang 有很多的模版引擎,自带的 `html/template` 也很好,大多数情况都能满足需求,只是有些逻辑、条件判断不好在模版里实现, `quicktemplate` 是个很好的选择。...

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

Golang 把cookie 字符串解析为cookie 结构

在做爬虫时有时候会遇到需要带已登录的 cookie 请求,这个时候最简单的方法是在浏览器登录后,在开发者面板找到cookie 字符串,然后拷贝粘贴。这就面临一个问题需要把cookie 字符串解析成Go 语言 cookie 结构体。...

Golang WebAssembly 了解一下

Go 1.11 起开始支持 WebAssembly ,也就是说以后可以使用任何语言作为“前端语言”来进行 Web 开发。...

Go 编程格言谚语

这些 go 格言谚语大多出自 Rob Pike 振奋人心的演讲视频,有很大参考价值。...

Write a Comment to "golang 简单网络服务编程入门"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms