Todo:golang UDP connection Simple test with caution deadline

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

Todo:golang UDP connection Simple test with caution deadline

UDP is the abbreviation of User Datagram protocol, the Chinese name is a Subscriber datagram protocol, is a connectionless transport layer protocol in the OSI (Open System interconnection, open Systems Interconnect) Reference Model, Provides transaction-oriented, simple unreliable messaging services, and IETF RFC 768 is a formal specification for UDP. The protocol number for UDP in the IP message is 17. In the network it is the same as the TCP protocol for processing packets, is a non-connected protocol. In the OSI model, the fourth layer, the transport layer, is in the upper layer of the IP protocol. UDP has the disadvantage of not providing packet grouping, assembling, or sorting packets, that is, when the message is sent, it is not possible to know whether or not it arrives safely and completely.


1.Golang UDP Service

Package Main

Import (

"FMT"

"NET"

)

Func sendresponse (Conn *net. Udpconn, addr *net. UDPADDR) {

_, ERR: = conn. WRITETOUDP ([]byte ("from Server:hello I got your Mesage"), addr)

If err! = Nil {

Fmt. Printf ("couldn ' t send response%v", err)

}

}

Func Main () {

P: = Make ([]byte, 2048)

Addr: = Net. udpaddr{

port:12345,

Ip:net. Parseip ("127.0.0.1"),

}

Ser, err: = Net. LISTENUDP ("UDP", &ADDR)

If err! = Nil {

Fmt. Printf ("Some error%vn", err)

Return

}

for {

_, remoteaddr, err: = Ser. READFROMUDP (P)

Fmt. Printf ("Read a message from%v%s n", REMOTEADDR, p)

If err! = Nil {

Fmt. Printf ("Some error%v", err)

Continue

}

Go Sendresponse (Ser, remoteaddr)

}

}

2.Golang UDP Client

Package Main

Import (

"Bufio"

"FMT"

"NET"

"OS"

"Time"

)

Func Main () {

Addr, Err: = Net. RESOLVEUDPADDR ("UDP", ": 12345")

If err! = Nil {

Fmt. PRINTLN ("net. Resolveudpaddr fail. ", err)

Os. Exit (1)

}

Socket, ERR: = Net. DIALUDP ("UDP", nil, addr)

If err! = Nil {

Fmt. PRINTLN ("net. DIALUDP fail. ", err)

Os. Exit (1)

}

T: = time. Now ()

Socket. Setdeadline (T.add (time. Duration (5 * time. Second)))

Socket. Setwritedeadline (T.add (time. Duration (5 * time. Second)))

Socket. Setreaddeadline (T.add (time. Duration (5 * time. Second)))

Defer socket. Close ()

r: = Bufio. Newreader (OS. Stdin)

for {

Switch line, OK: = r.readstring (' n '); True {

Case OK! = Nil:

Fmt. Printf ("Bye Bye!n")

Return

Default

_, Err: = socket. Write ([]byte (line))

If err! = Nil {

Fmt. PRINTLN ("Error send data,err:", err)

Return

}

Data: = Make ([]byte, 1024)

_, remoteaddr, err: = socket. READFROMUDP (data)

If err! = Nil {

Fmt. Println ("Error recv Data,err:", err)

Return

}

Fmt. Printf ("From%s:%sn", remoteaddr.string (), String (data))

}

}

}

3. You can put the representative copy of the local run test, Golang setting method has three: Setdeadline,setwritedeadline,setreaddeadline, set the deadline is the specified time stamp for the super-point, The action specifies that the timestamp connection will time out, the packet is sent again, and the accept package will time out to prompt I/O timeout

Error send data,err:write UDP 127.0.0.1:51608->127.0.0.1:12345:i/o timeout

Error recv data,err:read UDP 127.0.0.1:51608->127.0.0.1:12345:i/o Timeout

So to keep your heartbeat online you need to constantly refresh the deadline timestamp. This article is for reference only, if there is a corresponding scenario, it will be used, ^_^.

Wxgzh:ludong86


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.