A tutorial to implement automatic re-interconnection of TCP

Source: Internet
Author: User
The following small series for everyone to bring a Golang TCP automatic re-connect implementation method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

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*/package mainimport ("FMT" "NET" "OS" "Strings" "Time") Func CheckError (err error) {if err! = Nil {fmt. PRINTLN (ERR) OS. Exit (1)}}func handleclient (conn net. Conn) {Conn. Setreaddeadline (time. Now (). ADD (3 * time. Minute) Request: = Make ([]byte,1024) defer Conn. Close () for {recv_len,err: = conn. Read (Request) if err! = Nil {fmt. Println (ERR) Break} if Recv_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,1024)}}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 () if err! = nil {Continue} go Handleclient (cc)}}

Solution Solutions

/*TCP Client with Reconnect*/package mainimport (  "NET"  "FMT"  "Bufio" "Time  ") func dotask (conn net. Conn) {for  {    FMT. fprintf (conn, "Test msg\n")    msg,err: = Bufio. Newreader (conn). ReadString (' \ n ')    if err! = 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)    if err! = Nil {      FMT. Println (") fail")    }else{      FMT. Println (") OK")      defer Conn. Close ()      dotask (conn)    } time    . Sleep (3 * time. Second)  }}


Operating effect:


[Root@local t1]#/tcpclient1connect (127.0.0.1:12345) okrecv msg:2017-06-12 21:10:32.110977137 +0800 CSTrecv msg:2017 -06-12 21:10:33.111868746 +0800 cstrecv data errorconnect (127.0.0.1:12345) failconnect (127.0.0.1:12345) Failconnect ( 127.0.0.1:12345) okrecv msg:2017-06-12 21:10:43.117203432 +0800 cstrecv msg:2017-06-12 21:10:44.11853427 +0800 CST

Discuss

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

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

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.