Golang Write a TCP chat test

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

The go code is simple enough to make a simple chat

Server.go

Package main import ("FMT"; " NET ") const (IP =" "Port = 3333) Func main () {listen,err: = net. LISTENTCP ("TCP", &net. Tcpaddr{net. PARSEIP (IP), port, ""}) if err! = Nil {fmt. PRINTLN ("Listener Port failed:", err.) Error ()) return} FMT. PRINTLN ("Initializing connection, waiting for client connection ...") server (Listen)}func server (Listen *net. TcpListener) {for {conn, err: = Listen. ACCEPTTCP () if err! = Nil {fmt. PRINTLN ("Accept Client Connection exception:", err.) Error ()) continue} FMT. PRINTLN ("Client connection from:", Conn. Remoteaddr (). String ()) defer Conn. Close () go func () {data: = make ([]byte, +) for {i, err: = conn. Read (data) fmt. PRINTLN ("Client sends data:", String (Data[0:i]))) If err! = Nil {fmt. PRINTLN ("Read client data error:", err.) Error ()) break} conn. Write ([]byte{' f ', ' I ', ' n ', ' I ', ' s ', ' H '}}}}}}

Client.go

 PackageMainImport (    "FMT"    "NET")Const(Addr="127.0.0.1:3333") Func Main () {conn, err:= Net. Dial ("TCP", addr)ifErr! =Nil {fmt. Println ("Connection Server failed:", Err. Error ())return} fmt. Println ("Connected server") Defer Conn. Close () client (conn)}FUNC client (conn net. Conn) {SMS:= Make ([]byte, -)     for{fmt. Print ("Please enter a message to send:") _, Err:= Fmt. Scan (&SMS)ifErr! =Nil {fmt. Println ("Data Entry exception:", Err. Error ())} conn. Write (SMS) BUF:= Make ([]byte, -) C, err:=Conn. Read (BUF)ifErr! =Nil {fmt. Println ("Read Server data exception:", Err. Error ())} FMT. Println (String (buf[0: c])}}

$./server

Connection is initialized, waiting for client to connect ...

Client connections from: 127.0.0.1:50517

Client sent data: Nada

Client sent data: XXX

Client connections from: 127.0.0.1:50522

Client sent data: Asdada

The client sends the data:

Read client data error: EOF


Client connections from: 127.0.0.1:50529

Client sent data: Asdfasdfads

Client-sent data: FAs

Client sent data: D

Client sent data: FA

Client sent data: DSFA




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.