Golang Implement a chat room

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently took a look at the go language, tried to write a chat room, practice practiced hand just, but for me to do PHP, the go language is very enlightening to me.

Customer Service side

Package Mainimport ("FMT" "NET" "OS")//define channel var ch chan int = make (chan int)//define nickname Var nickname Stringfunc Reader ( Conn *net. Tcpconn) {buff: = make ([]byte, +) for {j, err: = conn. Read (Buff) if err! = Nil {ch <-1 break} fmt. Println (String (Buff[0:j]))}}func main () {tcpaddr, _: = Net. RESOLVETCPADDR ("TCP", "127.0.0.1:9999") conn, err: = Net. DIALTCP ("TCP", nil, tcpaddr) if err! = Nil {fmt. Println ("Server is not starting") OS. Exit (0)}//Why can't I put it before the IF? Err is not nil words is painc (PAINC and defer discrimination!!! ) Defer Conn. Close () Go Reader (conn) fmt. Println ("Please enter nickname") fmt. Scanln (&nickname) fmt. Println ("Your nickname is:", nickname) for {var msg string fmt. Scanln (&msg) B: = []byte ("<" + nickname + ">" + "said:" + MSG) Conn. Write (b)//select is a non-blocking select {case <-ch:fmt.            PRINTLN ("Server Error! Please reconnect!") Os. Exit (1) dEfault://Do not add default, then <-ch will block for, the next input will not be the method}} 

server-side

Package Mainimport ("FMT" "net") var connmap map[string]*net. Tcpconnfunc Checkerr (err error) int {if err! = Nil {if Err. Error () = = "EOF" {//user exits FMT. PRINTLN ("user launched") return 0} fmt. PRINTLN ("Error") Return-1} return 1}func say (Tcpconn *net.        Tcpconn) {for {//read a client sent over data: = Make ([]byte, +) Total, err: = Tcpconn.read (data) Fmt.         Println (String (data[:total), err) flag: = Checkerr (err) if flag = = 0 {//exit entire loop break }//Broadcast form, send data to each client for _, Conn: = Range Connmap {if Conn. Remoteaddr (). String () = = Tcpconn.remoteaddr (). String () {///does not send a message to the data input client continue} conn. Write (Data[:total])}}}func Main () {tcpaddr, _: = Net. RESOLVETCPADDR ("TCP", "127.0.0.1:9999") TcpListener, _: = Net. LISTENTCP ("TCP", TCPADDR)/* map is defined and make? (Which data types defineAfter you finish, make?) Http://stackoverflow.com/questions/27267900/runtime-error-assignment-to-entry-in-nil-map */Connmap = make (map[ String]*net. Tcpconn) for {tcpconn, _: = Tcplistener.accepttcp () defer tcpconn.close () Connmap[tcpconn.remotea DDR (). String ()] = Tcpconn FMT. Println ("Connected Customer terminal information:", TCPCONN.REMOTEADDR (). String ()) go Say (tcpconn)}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.