亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從列表中刪除客戶端會殺死其他連接

從列表中刪除客戶端會殺死其他連接

Go
森林海 2021-06-29 13:16:07
為什么要殺死來自其他客戶端的簡單“ClientList.Remove(entry)”所有連接?我有一個非常簡單的 Go TCP Server,可以進行連接處理和登錄處理。之后,如果創建一個客戶端并使用 TCP 客戶端啟動一個 GO 程序。newClient := &Client{"", "", login.LoginToken, conn} go ClientReader(newClient) ClientList.PushBack(*newClient)Go 例程讀取所有傳入的數據。當連接超時或網絡更改(客戶端獲得新 IP)時,它會從客戶端列表中刪除客戶端。但是當它從列表中刪除客戶端時......所有其他客戶端連接都死了?在循環中它找到正確的客戶端并將其刪除??纯磖emoveloop:常規:func ClientReader(client *Client) {    buffer := make([]byte, 2048)    for {        bytesRead, error := client.Conn.Read(buffer)    if error != nil {        Log(error)        break    }    var m Message    err := json.Unmarshal([]byte(buffer[0:bytesRead]), &m)    if err != nil {        Log(err)    } else {        switch m.Cmd {        case "Message":        case "Ping":            Log("Ping from: ", client.Name, " on ", client.Conn.RemoteAddr())            client.Conn.SetDeadline(time.Now().Add(25 * time.Second))            pong := []byte(`{"PONG":"..."}` + "\r\n")            client.Conn.Write(pong)            Log("PONG: " + time.Now().Format(time.RFC850))            Log("User Online: " + strconv.Itoa(ClientList.Len()))            Log("Goroutines: " + strconv.Itoa(runtime.NumGoroutine()))        default:            Log("Not supported Command: ", m.Cmd)            clienterror := []byte(`{"Err":"Command not supported"}` + "\r\n")            client.Conn.Write(clienterror)        }        for i := 0; i < 2048; i++ {            buffer[i] = 0x00        }    }}RemoveLoop:    for entry := ClientList.Front(); entry != nil; entry = entry.Next() {        listclient := entry.Value.(Client)        if client.Conn.RemoteAddr() == listclient.Conn.RemoteAddr() {        ClientList.Remove(entry)        Log("## SEARCH: ", client.Name, client.Conn.RemoteAddr())        Log("## FOUND: ", listclient.Name,listclient.Conn.RemoteAddr())    Log("## REMOVED: ", entry.Value)    break RemoveLoop    }}Log("Exit Client Reader Routine ", client.Name, " on ", client.Conn.RemoteAddr())}
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號