1、主進(jìn)程建立TCP監(jiān)聽(tīng)服務(wù),并且初始化一個(gè)變量 talkChan := make(map[int]chan string)
2、當(dāng)主進(jìn)程ACCEPT連接請(qǐng)求后,利用go 啟動(dòng)一個(gè)協(xié)程A去維持和客戶(hù)端的連接,把taokChan帶入到協(xié)程里
4、協(xié)程A在收到客戶(hù)端發(fā)送的用戶(hù)信息后,建立一個(gè)此用戶(hù)對(duì)應(yīng)的管道 talkChan[uid] = make(chan string)
5、協(xié)程A再啟動(dòng)一個(gè)協(xié)程A1去專(zhuān)門(mén)用來(lái)讀取客戶(hù)端發(fā)送的消息,并且用來(lái)判斷是發(fā)送給誰(shuí)的消息,然后把消息放到對(duì)應(yīng)的chan里。
6、協(xié)程A再啟動(dòng)一個(gè)協(xié)程A2用來(lái)讀取此用戶(hù)對(duì)應(yīng)的管道,如果里面有信息,則取出來(lái)發(fā)送到客戶(hù)端。
package main
import (
"fmt"
"log"
"net"
"strconv"
)
func handleConnection(conn net.Conn, talkChan map[int]chan string) {
//fmt.Printf("%p\n", talkChan) //用以檢查是否是傳過(guò)來(lái)的指針
/*
定義當(dāng)前用戶(hù)的uid
*/
var curUid int
var err error
/*
定義關(guān)閉通道
*/
var closed = make(chan bool)
defer func() {
fmt.Println("defer do : conn closed")
conn.Close()
fmt.Printf("delete userid [%v] from talkChan", curUid)
delete(talkChan, curUid)
}()
/**
* 提示用戶(hù)設(shè)置自己的uid, 如果沒(méi)設(shè)置,則不朝下執(zhí)行
*/
for {
//提示客戶(hù)端設(shè)置用戶(hù)id
_, err = conn.Write([]byte("請(qǐng)?jiān)O(shè)置用戶(hù)uid"))
if err != nil {
return
}
data := make([]byte, 1024)
c, err := conn.Read(data)
if err != nil {
//closed - true //這樣會(huì)阻塞 | 后面取closed的for循環(huán),沒(méi)有執(zhí)行到。
return
}
sUid := string(data[0:c])
//轉(zhuǎn)成int類(lèi)型
uid, _ := strconv.Atoi(sUid)
if uid 1 {
continue
}
curUid = uid
talkChan[uid] = make(chan string)
//fmt.Println(conn, "have set uid ", uid, "can talk")
_, err = conn.Write([]byte("have set uid "+sUid+" can talk"))
if err != nil {
return
}
break
}
fmt.Println("err 3")
//當(dāng)前所有的連接
fmt.Println(talkChan)
//讀取客戶(hù)端傳過(guò)來(lái)的數(shù)據(jù)
go func() {
for {
//不停的讀客戶(hù)端傳過(guò)來(lái)的數(shù)據(jù)
data := make([]byte, 1024)
c, err := conn.Read(data)
if err != nil {
fmt.Println("have no client write", err)
closed - true //這里可以使用 | 因?yàn)槭怯糜玫膅o 新開(kāi)的線(xiàn)程去處理的。 | 即便chan阻塞,后面的也會(huì)執(zhí)行去讀 closed 這個(gè)chan
}
clientString := string(data[0:c])
//將客戶(hù)端過(guò)來(lái)的數(shù)據(jù),寫(xiě)到相應(yīng)的chan里
if curUid == 3 {
talkChan[4] - clientString
} else {
talkChan[3] - clientString
}
}
}()
/*
從chan 里讀出給這個(gè)客戶(hù)端的數(shù)據(jù) 然后寫(xiě)到該客戶(hù)端里
*/
go func() {
for {
talkString := -talkChan[curUid]
_, err = conn.Write([]byte(talkString))
if err != nil {
closed - true
}
}
}()
/*
檢查是否已經(jīng)關(guān)閉連接 如果關(guān)閉則推出該線(xiàn)程 去執(zhí)行defer語(yǔ)句
*/
for {
if -closed {
return
}
}
}
func main() {
/**
建立監(jiān)聽(tīng)鏈接
*/
ln, err := net.Listen("tcp", "127.0.0.1:6010")
if err != nil {
panic(err)
}
//創(chuàng)建一個(gè)管道
//talkChan := map[f]
talkChan := make(map[int]chan string)
fmt.Printf("%p\n", talkChan)
/*
監(jiān)聽(tīng)是否有客戶(hù)端過(guò)來(lái)的連接請(qǐng)求
*/
for {
fmt.Println("wait connect...")
conn, err := ln.Accept()
if err != nil {
log.Fatal("get client connection error: ", err)
}
go handleConnection(conn, talkChan)
}
}
package main
import (
"fmt"
"math/rand"
"net"
)
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:6010")
if err != nil {
panic(err)
}
fmt.Fprintf(conn, "hello server\n")
defer conn.Close()
go writeFromServer(conn)
for {
var talkContent string
fmt.Scanln(talkContent)
if len(talkContent) > 0 {
_, err = conn.Write([]byte(talkContent))
if err != nil {
fmt.Println("write to server error")
return
}
}
}
}
func connect() {
conn, err := net.Dial("tcp", "127.0.0.1:6010")
if err != nil {
panic(err)
}
fmt.Fprintf(conn, "hello server\n")
defer conn.Close()
go writeFromServer(conn)
for {
var talkContent string
fmt.Scanln(talkContent)
if len(talkContent) > 0 {
_, err = conn.Write([]byte(talkContent))
if err != nil {
fmt.Println("write to server error")
return
}
}
}
}
func writeFromServer(conn net.Conn) {
defer conn.Close()
for {
data := make([]byte, 1024)
c, err := conn.Read(data)
if err != nil {
fmt.Println("rand", rand.Intn(10), "have no server write", err)
return
}
fmt.Println(string(data[0:c]) + "\n ")
}
}