国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

深入理解 Golang: 網(wǎng)絡(luò)編程

這篇具有很好參考價值的文章主要介紹了深入理解 Golang: 網(wǎng)絡(luò)編程。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Go 中的 Epoll

關(guān)于計算機網(wǎng)絡(luò)分層與 TCP 通信過程過程此處不再贅述。

  • 考慮到 TCP 通信過程中各種復(fù)雜操作,包括三次握手,四次揮手等,多數(shù)操作系統(tǒng)都提供了 Socket 作為 TCP 網(wǎng)絡(luò)連接的抽象。
  • Linux -> Internet domain socket -> SOCK_STREAM
  • Linux 中 Socket 以 “文件描述符” FD 作為標(biāo)識

在進行 Socket 通信時,服務(wù)端同時操作多個 Socket,此時便需要 IO 模型操作方案。:

  • 阻塞 IO。傳統(tǒng) C/C++ 方案,同步讀寫 Socket(一個線程一個 Socket),線程陷入內(nèi)核態(tài),當(dāng)讀寫成功后,切換回用戶態(tài)繼續(xù)執(zhí)行。
  • 非阻塞 IO。應(yīng)用會不斷自旋輪詢,直到 Socket 可以讀寫,如果暫時無法收發(fā)數(shù)據(jù),會返回錯誤。
  • Epoll 多路復(fù)用。提供了事件列表,不需要查詢各個 Socket。其注冊多個 Socket 事件,調(diào)用 epoll ,當(dāng)有事件發(fā)生則返回。

Epoll 是 Linux 下的 event poll,Windows 中為 IOCP, Mac 中為 kqueue。

在 Go 中,內(nèi)部采用結(jié)合阻塞模型和多路復(fù)用的方法。在這里就不再是線程操作 Socket,而是 Goroutine 協(xié)程。每個協(xié)程關(guān)心一個 Socket 連接:

  • 在底層使用操作系統(tǒng)的多路復(fù)用 IO。
  • 在協(xié)程層次使用阻塞模型。
  • 阻塞協(xié)程時,休眠協(xié)程。

我們知道,Go 是一個跨平臺的語言,不同平臺/操作系統(tǒng)下提供的 Epoll 實現(xiàn)不同,所以 Go 在 Epoll/IOCP/kqueue 上再獨立了一層 epoll 抽象層,用于屏蔽各個系統(tǒng)的差異性,抽象各系統(tǒng)對多路復(fù)用器的實現(xiàn)。
深入理解 Golang: 網(wǎng)絡(luò)編程

Go Network Poller 多路復(fù)用器的抽象,以 Linux 為例:

  • Go Network Poller 對于多路復(fù)用器的抽象和適配
  • epoll_create() -> netpollinit()
  • epoll_ctl() -> netpollopen()
  • epoll_wait() -> netpoll()
// Integrated network poller (platform-independent part).
// A particular implementation (epoll/kqueue/port/AIX/Windows)
// must define the following functions:
//
// func netpollinit()
//     Initialize the poller. Only called once.
//
// func netpollopen(fd uintptr, pd *pollDesc) int32
//     Arm edge-triggered notifications for fd. The pd argument is to pass
//     back to netpollready when fd is ready. Return an errno value.
//
// func netpollclose(fd uintptr) int32
//     Disable notifications for fd. Return an errno value.
//
// func netpoll(delta int64) gList
//     Poll the network. If delta < 0, block indefinitely. If delta == 0,
//     poll without blocking. If delta > 0, block for up to delta nanoseconds.
//     Return a list of goroutines built by calling netpollready.
//
// func netpollBreak()
//     Wake up the network poller, assumed to be blocked in netpoll.
//
// func netpollIsPollDescriptor(fd uintptr) bool
//     Reports whether fd is a file descriptor used by the poller.

上訴所有方法的實現(xiàn)都在 %GOROOT/src/runtime/netpoll_epoll.go%

netpollinit() 新建多路復(fù)用器

  1. 新建 Epoll,不同系統(tǒng)對應(yīng)不同的實現(xiàn)方式。
  2. 新建一個 Pipe 管道用于中斷 Epoll。
  3. 將 “管道有數(shù)據(jù)到達” 事件注冊到 Epoll 中。
func netpollinit() {
    var errno uintptr

    // 1. 新建 Epoll,不同系統(tǒng)對應(yīng)不同的實現(xiàn)方式
    epfd, errno = syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
    if errno != 0 {
        println("runtime: epollcreate failed with", errno)
        throw("runtime: netpollinit failed")
    }

    // 用來中斷 Epoll 的管道
    r, w, errpipe := nonblockingPipe()
    if errpipe != 0 {
        println("runtime: pipe failed with", -errpipe)
        throw("runtime: pipe failed")
    }

    // 3. 注冊事件
    ev := syscall.EpollEvent{
        Events: syscall.EPOLLIN,
    }
    *(**uintptr)(unsafe.Pointer(&ev.Data)) = &netpollBreakRd
    errno = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, r, &ev)
    if errno != 0 {
        println("runtime: epollctl failed with", errno)
        throw("runtime: epollctl failed")
    }
    netpollBreakRd = uintptr(r)
    netpollBreakWr = uintptr(w)
}

netpollopen() 插入事件

  1. 傳入一個 Socket 的 FD 和 pollDesc 指針。
  2. pollDesc 指針是 Socket 相關(guān)詳細信息。
  3. pollDesc 指針中記錄了哪個協(xié)程在休眠等待此 Socket。
  4. 將 Socket 的可讀/可寫/斷開事件注冊到 Epoll 中。
func netpollopen(fd uintptr, pd *pollDesc) uintptr {
    var ev syscall.EpollEvent

    // 事件類型
    ev.Events = syscall.EPOLLIN | syscall.EPOLLOUT | syscall.EPOLLRDHUP | syscall.EPOLLET
    *(**pollDesc)(unsafe.Pointer(&ev.Data)) = pd
    return syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, int32(fd), &ev)
}

netpoll() 查詢事件

  1. 調(diào)用 EpollWait() 方法,查詢有哪些事件發(fā)生
  2. 根據(jù) Socket 相關(guān)的 pollDesc 信息,返回哪些協(xié)程可以喚醒。
func netpoll(delay int64) gList {
    // 1. 查詢哪些事件發(fā)生
    n, errno := syscall.EpollWait(epfd, events[:], int32(len(events)), waitms)
    // ...
    if errno != 0 {
        if errno != _EINTR {
            println("runtime: epollwait on fd", epfd, "failed with", errno)
            throw("runtime: netpoll failed")
        }
        // If a timed sleep was interrupted, just return to
        // recalculate how long we should sleep now.
        if waitms > 0 {
            return gList{}
        }
        goto retry
    }
    // 2. 根據(jù) Socket 相關(guān)的 pollDesc 信息,返回哪些協(xié)程可以喚醒。
    var toRun gList
    for i := int32(0); i < n; i++ {
        ev := events[i]
        if ev.Events == 0 {
            continue
        }

        if *(**uintptr)(unsafe.Pointer(&ev.Data)) == &netpollBreakRd {
            if ev.Events != syscall.EPOLLIN {
                println("runtime: netpoll: break fd ready for", ev.Events)
                throw("runtime: netpoll: break fd ready for something unexpected")
            }
            if delay != 0 {
                var tmp [16]byte
                read(int32(netpollBreakRd), noescape(unsafe.Pointer(&tmp[0])), int32(len(tmp)))
                netpollWakeSig.Store(0)
            }
            continue
        }

        var mode int32
        if ev.Events&(syscall.EPOLLIN|syscall.EPOLLRDHUP|syscall.EPOLLHUP|syscall.EPOLLERR) != 0 {
            mode += 'r'
        }
        if ev.Events&(syscall.EPOLLOUT|syscall.EPOLLHUP|syscall.EPOLLERR) != 0 {
            mode += 'w'
        }
        if mode != 0 {
            pd := *(**pollDesc)(unsafe.Pointer(&ev.Data))
            pd.setEventErr(ev.Events == syscall.EPOLLERR)
            netpollready(&toRun, pd, mode)
        }
    }

    // 協(xié)程列表
    return toRun
}

Go Network Poller

Network Poller 初始化

  • 初始化一個 Network Poller。
  • 調(diào)用 netpollinit() 新建多路復(fù)用器。
// %GOROOT%src/runtime/netpoll.go
func poll_runtime_pollServerInit() {
    netpollGenericInit()
}

func netpollGenericInit() {
    // 每個 Go 應(yīng)用只初始化一次
    if netpollInited.Load() == 0 {
        lockInit(&netpollInitLock, lockRankNetpollInit)
        lock(&netpollInitLock)
        if netpollInited.Load() == 0 {
            // 新建多路復(fù)用器
            netpollinit()
            netpollInited.Store(1)
        }
        unlock(&netpollInitLock)
    }
}

關(guān)于 pollDesc,是 runtime 包對 Socket 的詳細描述:

type pollDesc struct {
    _    sys.NotInHeap
    link *pollDesc // in pollcache, protected by pollcache.lock
    fd   uintptr   // constant for pollDesc usage lifetime
    atomicInfo atomic.Uint32 // atomic pollInfo


    rg atomic.Uintptr // pdReady, pdWait, G waiting for read or pdNil
    wg atomic.Uintptr // pdReady, pdWait, G waiting for write or pdNil

    lock    mutex // protects the following fields
    closing bool
    user    uint32    // user settable cookie
    rseq    uintptr   // protects from stale read timers
    rt      timer     // read deadline timer (set if rt.f != nil)
    rd      int64     // read deadline (a nanotime in the future, -1 when expired)
    wseq    uintptr   // protects from stale write timers
    wt      timer     // write deadline timer
    wd      int64     // write deadline (a nanotime in the future, -1 when expired)
    self    *pollDesc // storage for indirect interface. See (*pollDesc).makeArg.
}

Network Poller 新增監(jiān)聽 Socket

  • 在 pollCache 鏈表中分配一個 pollDesc。
  • 初始化 pollDesc,rg,wg 都為 0。
  • 調(diào)用 netpollopen() 插入事件
func poll_runtime_pollOpen(fd uintptr) (*pollDesc, int) {
    // 分配 pollDesc
    pd := pollcache.alloc()
    lock(&pd.lock)
    wg := pd.wg.Load()
    if wg != pdNil && wg != pdReady {
        throw("runtime: blocked write on free polldesc")
    }
    rg := pd.rg.Load()
    if rg != pdNil && rg != pdReady {
        throw("runtime: blocked read on free polldesc")
    }

    // 初始化 pollDesc
    pd.fd = fd
    pd.closing = false
    pd.setEventErr(false)
    pd.rseq++
    pd.rg.Store(pdNil)
    pd.rd = 0
    pd.wseq++
    pd.wg.Store(pdNil)
    pd.wd = 0
    pd.self = pd
    pd.publishInfo()
    unlock(&pd.lock)

    // 插入事件
    errno := netpollopen(fd, pd)
    if errno != 0 {
        pollcache.free(pd)
        return nil, int(errno)
    }
    return pd, 0
}

Network Poller 收發(fā)數(shù)據(jù)

  • Socket 已經(jīng)可讀寫時
    • runtime 的 g0 協(xié)程循環(huán)調(diào)用 netpoll() 方法。
    • 發(fā)現(xiàn) Socket 可讀寫時,給對應(yīng)的 rg,wg 置為 pdReady(1)。
    • 協(xié)程調(diào)用 poll_runtime_pollWait()。
    • 判斷 rgwg 已置為 pdReady(1),返回 0。
func poll_runtime_pollWait(pd *pollDesc, mode int) int {
    // ...
    // 判斷 `rg` 或 `wg` 已置為 pdReady(1),返回 0。
    for !netpollblock(pd, int32(mode), false) {
        errcode = netpollcheckerr(pd, int32(mode))
        if errcode != pollNoError {
            return errcode
        }
    }
    return pollNoError
}
  • Socket 暫時無法讀寫時
    • runtime 的 g0 協(xié)程循環(huán)調(diào)用 netpoll() 方法。
    • 協(xié)程調(diào)用 poll_runtime_pollWait()。
    • 發(fā)現(xiàn)對應(yīng)的 rgwg 為 0。
    • 給對應(yīng)的 rgwg 置為協(xié)程地址。
    • 休眠等待。
    • 當(dāng)發(fā)現(xiàn) Socket可讀寫時,查看對應(yīng)的 rgwg。
    • 若為協(xié)程地址,則返回該地址。
    • 調(diào)度器開始調(diào)度該協(xié)程。
      深入理解 Golang: 網(wǎng)絡(luò)編程

Socket 通信

net 包中的 Socket 會被定義為一個 netFD 結(jié)構(gòu)體:

type netFD struct {
    // 最終指向的 runtime 中的 Socket 結(jié)構(gòu)體
    pfd poll.FD

    family      int
    sotype      int
    isConnected bool // handshake completed or use of association with peer
    net         string
    laddr       Addr
    raddr       Addr
}

深入理解 Golang: 網(wǎng)絡(luò)編程

Server 端

以 TCP 協(xié)議為例:

net 的 net.Listen() 操作:

  1. 新建 Socket,并執(zhí)行 bind 操作
  2. 新建一個 FD(net 包對 Socket 的詳情描述)。
  3. 返回一個 TCPListener 對象
  4. TCPListenerFD 信息加入監(jiān)聽。
func main() {
    ln, err := net.Listen("tcp", ":8888")
    if err != nil {
        panic(err)
    }
}

TCPListener 本質(zhì)是一個 LISTEN 狀態(tài)的 Scoket。

TCPListener.Accept() 操作:

  1. 直接調(diào)用 Socket 的 accept()。
  2. 如果失敗,休眠等待新的連接。
  3. 將新的 Socket 包裝成 TCPConn 變量返回。
  4. TCPConnFD 信息加入監(jiān)聽。
func main() {
    ln, err := net.Listen("tcp", ":8888")
    if err != nil {
        panic(err)
    }

    conn, err := ln.Accept()
    if err != nil {
        panic(err)
    }
    defer conn.Close()
}

TCPConn 本質(zhì)是一個 ESTANBLISHED 狀態(tài)的 Scoket。

TCPConn 收發(fā)數(shù)據(jù)

func main() {
    // 1. 監(jiān)聽端口
    ln, err1 := net.Listen("tcp", ":8888")
    if err1 != nil {
        panic(err1)
    }

    // 2. 建立連接
    conn, err2 := ln.Accept()
    if err2 != nil {
        panic(err2)
    }
    defer conn.Close()

    var recv [1024]byte
    // 使用 bufio 標(biāo)準(zhǔn)庫提供的緩沖區(qū)功能
 send := bufio.NewReader(conn)

    for {

        // 3. 獲取數(shù)據(jù)
        _, err3 := conn.Read(recv[:])
        if err3 != nil {
            break
        }

        fmt.Printf("n: %v\n", string(recv[:]))

        // 4. 發(fā)送數(shù)據(jù)
        msg, err := send.ReadString('\n')
        if strings.ToUpper(msg) == "Q" {
            return
        }
        if err != nil {
            return
        }
        _, err4 := conn.Write([]byte(msg))
        if err4 != nil {
            break
        }
    }
}

Client 端

func main() {
    // 與服務(wù)端建立連接
    conn, err := net.Dial("tcp", ":8888")
    if err != nil {
        panic(err)
    }
    
    var recv [1024]byte

    send := bufio.NewReader(os.Stdin)
    for {
        s, _ := send.ReadString('\n')
        if strings.ToUpper(s) == "Q" {
            return  
        }

        // 發(fā)送數(shù)據(jù)
        _, err = conn.Write([]byte(s))
        if err != nil {
            panic(err)
        }

        // 接收數(shù)據(jù) 
        _, err := conn.Read(recv[:])
        if err != nil {
            break
        }
        fmt.Printf(":%v\n", string(recv[:]))
    }
}

goroutine-per-connection style code

一個協(xié)程服務(wù)一個新的連接文章來源地址http://www.zghlxwxcb.cn/news/detail-515679.html

package main

import (
    "bufio"
    "fmt"
    "net"
    "strings"
)

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

    var recv [1024]byte
    // 使用 bufio 標(biāo)準(zhǔn)庫提供的緩沖區(qū)功能
    send := bufio.NewReader(conn)

    for {

        // 3. 獲取數(shù)據(jù)
        _, err3 := conn.Read(recv[:])
        if err3 != nil {
            break
        }

        fmt.Printf("n: %v\n", string(recv[:]))

        // 4. 發(fā)送數(shù)據(jù)
        msg, err := send.ReadString('\n')
        if strings.ToUpper(msg) == "Q" {
            return
        }
        if err != nil {
            return
        }
        _, err4 := conn.Write([]byte(msg))
        if err4 != nil {
            break
        }
    }
}

func main() {
    // 1. 監(jiān)聽端口
    ln, err1 := net.Listen("tcp", ":8888")
    if err1 != nil {
        panic(err1)
    }

    for {
        // 2. 建立連接
        conn, err2 := ln.Accept()
        if err2 != nil {
            panic(err2)
        }

        go handleConnection(conn)
    }
}

到了這里,關(guān)于深入理解 Golang: 網(wǎng)絡(luò)編程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ UDP協(xié)議學(xué)習(xí)_及原理深入理解

    「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ UDP協(xié)議學(xué)習(xí)_及原理深入理解

    「前言」文章內(nèi)容大致是傳輸層協(xié)議,UDP協(xié)議講解。 「歸屬專欄」網(wǎng)絡(luò)編程 「主頁鏈接」個人主頁 「筆者」楓葉先生(fy) HTTP協(xié)議普通用戶認為是將請求和響應(yīng)直接發(fā)送到了網(wǎng)絡(luò)當(dāng)中。但實際應(yīng)用層需要先將數(shù)據(jù)交給傳輸層,由傳輸層對數(shù)據(jù)做進一步處理后再將數(shù)據(jù)繼續(xù)向下

    2024年02月17日
    瀏覽(25)
  • 「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ TCP協(xié)議學(xué)習(xí)_及原理深入理解(一)[萬字詳解]

    「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ TCP協(xié)議學(xué)習(xí)_及原理深入理解(一)[萬字詳解]

    「前言」文章內(nèi)容大致是傳輸層協(xié)議,TCP協(xié)議講解,續(xù)上篇UDP協(xié)議。 「歸屬專欄」網(wǎng)絡(luò)編程 「主頁鏈接」個人主頁 「筆者」楓葉先生(fy) TCP( Transmission Control Protoco l)是一種面向連接的、可靠的傳輸協(xié)議,TCP全稱為 \\\"傳輸控制協(xié)議”,TCP人如其名,要對數(shù)據(jù)的傳輸進行一個

    2024年02月16日
    瀏覽(28)
  • 網(wǎng)絡(luò)編程——深入理解TCP/IP協(xié)議——OSI模型和TCP/IP模型:構(gòu)建網(wǎng)絡(luò)通信的基石

    網(wǎng)絡(luò)編程——深入理解TCP/IP協(xié)議——OSI模型和TCP/IP模型:構(gòu)建網(wǎng)絡(luò)通信的基石

    TCP/IP協(xié)議,即 傳輸控制協(xié)議/互聯(lián)網(wǎng)協(xié)議 ,是一組用于在計算機網(wǎng)絡(luò)中實現(xiàn)通信的協(xié)議。它由兩個主要的協(xié)議組成:TCP(傳輸控制協(xié)議)和IP(互聯(lián)網(wǎng)協(xié)議)。TCP負責(zé)確保數(shù)據(jù)的可靠傳輸,而IP則負責(zé)路由數(shù)據(jù)包以在網(wǎng)絡(luò)中傳遞。TCP/IP協(xié)議簇還包含其他輔助協(xié)議,如UDP(用戶數(shù)

    2024年02月14日
    瀏覽(33)
  • 「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ TCP協(xié)議學(xué)習(xí)_及原理深入理解(二 - 完結(jié))[萬字詳解]

    「網(wǎng)絡(luò)編程」傳輸層協(xié)議_ TCP協(xié)議學(xué)習(xí)_及原理深入理解(二 - 完結(jié))[萬字詳解]

    「前言」文章內(nèi)容大致是傳輸層協(xié)議,TCP協(xié)議講解的第二篇,續(xù)上篇TCP。 「歸屬專欄」網(wǎng)絡(luò)編程 「主頁鏈接」個人主頁 「筆者」楓葉先生(fy) 首先明確,TCP是面向連接的,TCP通信之前需要先建立連接,就是因為 TCP的各種可靠性保證都是基于連接的,要保證傳輸數(shù)據(jù)的可靠性

    2024年02月15日
    瀏覽(28)
  • 【文末送書】計算機網(wǎng)絡(luò)編程 | epoll詳解

    【文末送書】計算機網(wǎng)絡(luò)編程 | epoll詳解

    歡迎關(guān)注博主 Mindtechnist 或加入【智能科技社區(qū)】一起學(xué)習(xí)和分享Linux、C、C++、Python、Matlab,機器人運動控制、多機器人協(xié)作,智能優(yōu)化算法,濾波估計、多傳感器信息融合,機器學(xué)習(xí),人工智能等相關(guān)領(lǐng)域的知識和技術(shù)。關(guān)注公粽號 《機器和智能》 回復(fù) “python項目

    2024年02月08日
    瀏覽(26)
  • 網(wǎng)絡(luò)編程詳解(select poll epoll reactor)

    網(wǎng)絡(luò)編程詳解(select poll epoll reactor)

    serverfd = socket( opt ):調(diào)用socket( )方法創(chuàng)建一個對應(yīng)的serverfd bind( serverfd, address ):調(diào)用bind( )方法將fd和指定的地址( ip + port )進行綁定 listen( serverfd ):調(diào)用listen( )方法監(jiān)聽前面綁定時指定的地址 clientfd = accept( serverfd ):進入無限循環(huán)等待接受客戶端連接請求 n = read( clientfd, buf

    2024年04月09日
    瀏覽(26)
  • 網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室)

    網(wǎng)絡(luò)編程 IO多路復(fù)用 [epoll版] (TCP網(wǎng)絡(luò)聊天室)

    //head.h? ? ? ? ? ? 頭文件 //TcpGrpSer.c? ? ?服務(wù)器端 //TcpGrpUsr.c? ? ?客戶端 通過IO多路復(fù)用實現(xiàn)服務(wù)器在單進程單線程下可以與多個客戶端交互 ?API epoll函數(shù) ?head.h TcpGrpSer.c TcpGrpUsr.c ?

    2024年02月11日
    瀏覽(25)
  • Linux網(wǎng)絡(luò)編程(epoll的ET模式和LT模式)

    本篇文章主要來講解epoll的ET模式和LT模式,epoll中有兩種模式可以選擇一種是ET模式(邊緣觸發(fā)模式),另一種是LT模式(水平觸發(fā)模式) 在水平觸發(fā)模式下,當(dāng)一個文件描述符上的I/O事件就緒時,epoll會立即通知應(yīng)用程序,然后應(yīng)用程序可以對就緒事件進行處理。即,只要文件描述

    2024年02月12日
    瀏覽(24)
  • TCP/IP網(wǎng)絡(luò)編程 第十七章:優(yōu)于select的epoll

    select復(fù)用方法其實由來已久,因此,利用該技術(shù)后,無論如何優(yōu)化程序性能也無法同時接入上百個客戶端(當(dāng)然,硬件性能不同,差別也很大)。這種select方式并不適合以Web服務(wù)器端開發(fā)為主流的現(xiàn)代開發(fā)環(huán)境,所以要學(xué)習(xí)Linux平臺下的epoll。 基于select的I/O復(fù)用技術(shù)速度慢的原

    2024年02月16日
    瀏覽(29)
  • Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    Linux網(wǎng)絡(luò)編程:多路I/O轉(zhuǎn)接服務(wù)器(select poll epoll)

    文章目錄: 一:select 1.基礎(chǔ)API? select函數(shù) 思路分析 select優(yōu)缺點 2.server.c 3.client.c 二:poll 1.基礎(chǔ)API? poll函數(shù)? poll優(yōu)缺點 read函數(shù)返回值 突破1024 文件描述符限制 2.server.c 3.client.c 三:epoll 1.基礎(chǔ)API epoll_create創(chuàng)建? ?epoll_ctl操作? epoll_wait阻塞 epoll實現(xiàn)多路IO轉(zhuǎn)接思路 epoll優(yōu)缺點

    2024年02月11日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包