美文网首页
利用tcp服务器和客户端,创建一个聊天室

利用tcp服务器和客户端,创建一个聊天室

作者: 吃猫的鱼0 | 来源:发表于2018-01-08 15:31 被阅读0次

服务器

package main

import (
    "fmt"
    "net"
    "os"
)

var clients []net.Conn

func main() {
    var (
        host   = "localhost"
        port   = "8000"
        remote = host + ":" + port
        data   = make([]byte, 1024)
    )
    fmt.Println("Initiating server...")

    lis, err := net.Listen("tcp", remote)
    defer lis.Close()

    if err != nil {
        fmt.Printf("Error when listen: %s, Err: %s\n", remote, err)
        os.Exit(-1)
    }

    for {
        var res string
        conn, err := lis.Accept()
        if err != nil {
            fmt.Println("Error accepting client: ", err.Error())
            os.Exit(0)
        }
        clients = append(clients, conn)

        go func(con net.Conn) {
            fmt.Println("New connection: ", con.RemoteAddr())

            // Get client's name
            length, err := con.Read(data)
            if err != nil {
                fmt.Printf("Client %v quit.\n", con.RemoteAddr())
                con.Close()
                disconnect(con, con.RemoteAddr().String())
                return
            }
            name := string(data[:length])
            comeStr := name + " entered the room."
            notify(con, comeStr)

            // Begin recieve message from client
            for {
                length, err := con.Read(data)
                if err != nil {
                    fmt.Printf("Client %s quit.\n", name)
                    con.Close()
                    disconnect(con, name)
                    return
                }
                res = string(data[:length])
                sprdMsg := name + " said: " + res
                fmt.Println(sprdMsg)
                res = "You said:" + res
                con.Write([]byte(res))
                notify(con, sprdMsg)
            }
        }(conn)
    }
}


func notify(conn net.Conn, msg string) {
    for _, con := range clients {
        if con.RemoteAddr() != conn.RemoteAddr() {
            con.Write([]byte(msg))
        }
    }
}


func disconnect(conn net.Conn, name string) {
    for index, con := range clients {
        if con.RemoteAddr() == conn.RemoteAddr() {
            disMsg := name + " has left the room."
            fmt.Println(disMsg)
            clients = append(clients[:index], clients[index+1:]...)
            notify(conn, disMsg)
        }
    }
}

客户端

package main

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

var writeStr, readStr = make([]byte, 1024), make([]byte, 1024)

func main() {
    var (
        host   = "localhost"
        port   = "8000"
        remote = host + ":" + port
        reader = bufio.NewReader(os.Stdin)
    )

    con, err := net.Dial("tcp", remote)
    defer con.Close()

    if err != nil {
        fmt.Println("Server not found.")
        os.Exit(-1)
    }
    fmt.Println("Connection OK.")
    fmt.Printf("Enter your name: ")
    fmt.Scanf("%s", &writeStr)
    in, err := con.Write([]byte(writeStr))
    if err != nil {
        fmt.Printf("Error when send to server: %d\n", in)
        os.Exit(0)
    }
    fmt.Println("Now begin to talk!")
    go read(con)

    for {
        writeStr, _, _ = reader.ReadLine()
        if string(writeStr) == "quit" {
            fmt.Println("Communication terminated.")
            os.Exit(1)
        }

        in, err := con.Write([]byte(writeStr))
        if err != nil {
            fmt.Printf("Error when send to server: %d\n", in)
            os.Exit(0)
        }

    }
}

func read(conn net.Conn) {
    for {
        length, err := conn.Read(readStr)
        if err != nil {
            fmt.Printf("Error when read from server. Error:%s\n", err)
            os.Exit(0)
        }
        fmt.Println(string(readStr[:length]))
    }
}

资料参考:http://blog.csdn.net/wangshubo1989/article/details/70668916

相关文章

网友评论

      本文标题:利用tcp服务器和客户端,创建一个聊天室

      本文链接:https://www.haomeiwen.com/subject/elsbnxtx.html