Golang TCP Auto-re-connection

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

Operating system: CentOS 6.9_x64

Go language version: 1.8.3

Problem description

There is a TCP client program that needs to fetch data from the server on a regular basis, but it needs to be automatically re-connected for a variety of reasons (network instability, etc.).

Test Server Sample code:

/*TCP Server for test e-mail:mike_zhang@live.com*/Package main Import ("FMT"    "Net"    "OS"    "Strings"    " Time") func checkerror (err error) {ifErr! =Nil {fmt. PRINTLN (ERR) OS. Exit (1)}}} func handleclient (conn net. Conn) {Conn. Setreaddeadline (time. Now (). ADD (3*Time . Minute)) Request:= Make ([]byte,1024x768) Defer Conn. Close () for{recv_len,err:=Conn. Read (Request)ifErr! =Nil {fmt. PRINTLN (ERR) Break } ifRecv_len = =0 {  Break} recvdata:= Strings. Trimspace (string(Request[:recv_len]) fmt. Println ("Recv_len:", Recv_len) fmt. Println ("Recv_data:"+recvdata) Daytime:=Time . Now (). String () Conn. Write ([]byte(Daytime +"\ n") Request= Make ([]byte,1024x768)}}} Func main () {bindinfo:=": 12345"Tcpaddr,err:= Net. RESOLVETCPADDR ("TCP4", Bindinfo) checkerror (err) Listener,err:= Net. LISTENTCP ("TCP", TCPADDR) checkerror (err) for{cc,err:=Listener. Accept ()ifErr! =Nil {Continue} go Handleclient (cc)}}
View Code

Solution Solutions

/*TCP client with reconnect e-mail:mike_zhang@live.com*/Package main Import ("Net"    "FMT"    "Bufio"    " Time") func dotask (conn net. Conn) { for{fmt. fprintf (Conn,"Test msg\n") Msg,err:= Bufio. Newreader (conn). ReadString ('\ n') ifErr! =Nil {fmt. Println ("recv Data Error")  Break }Else{fmt. Println ("recv msg:", MSG)} Time. Sleep (1*Time . Second)}}} Func main () {hostinfo:="127.0.0.1:12345"     for{conn,err:= Net. Dial ("TCP", Hostinfo) fmt. Print ("Connect (", Hostinfo)ifErr! =Nil {fmt. Println (") Fail") }Else{fmt. Println (") OK") Defer Conn. Close () Dotask (conn)} time. Sleep (3*Time . Second)}}

Operating effect:

[Root@local t1]#./tcpClient1 Connect (127.0.0.1:12345) OK recv msg: .- .- A  +:Ten:32.110977137+0800CST recv msg: .- .- A  +:Ten:33.111868746+0800CST recv Data error Connect (127.0.0.1:12345) fail Connect (127.0.0.1:12345) fail Connect (127.0.0.1:12345) OK recv msg: .- .- A  +:Ten:43.117203432+0800CST recv msg: .- .- A  +:Ten:44.11853427+0800Cst

Discuss

Here is a simple example code that implements TCP auto-re-interconnection.

All right, that's it, I hope it helps you.

This article GitHub address:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170613_golang TCP Auto-re-connect. RST

Welcome to Supplement

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.