TODO:Golang UDP连接简单测试慎用Deadline

TODO:Golang UDP连接简单测试慎用Deadline

UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768是UDP的正式规范。UDP在IP报文的协议号是17。在网络中它与TCP协议一样用于处理数据包,是一种无连接的协议。在OSI模型中,在第四层——传输层,处于IP协议的上一层。UDP有不提供数据包分组、组装和不能对数据包进行排序的缺点,也就是说,当报文发送之后,是无法得知其是否安全完整到达的。

1.Golang UDP服务

  1. package main
  2. import (
  3.     “fmt”
  4.     “net”
  5. )
  6. func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) {
  7.     _, err := conn.WriteToUDP([]byte(“From server: Hello I got your mesage “), addr)
  8.     if err != nil {
  9.         fmt.Printf(“Couldn’t send response %v”, err)
  10.     }
  11. }
  12. func main() {
  13.     p := make([]byte, 2048)
  14.     addr := net.UDPAddr{
  15.         Port: 12345,
  16.         IP: net.ParseIP(“127.0.0.1”),
  17.     }
  18.     ser, err := net.ListenUDP(“udp”, &addr)
  19.     if err != nil {
  20.         fmt.Printf(“Some error %vn”, err)
  21.         return
  22.     }
  23.     for {
  24. _,     remoteaddr, err := ser.ReadFromUDP(p)
  25.         fmt.Printf(“Read a message from %v %s n”, remoteaddr, p)
  26.         if err != nil {
  27.             fmt.Printf(“Some error %v”, err)
  28.             continue
  29.         }
  30.         go sendResponse(ser, remoteaddr)
  31.     }
  32. }

2.Golang UDP 客户端

  1. package main
  2. import (
  3.     “bufio”
  4.     “fmt”
  5.     “net”
  6.     “os”
  7.     “time”
  8. )
  9. func main() {
  10.     addr, err := net.ResolveUDPAddr(“udp”, “:12345”)
  11.     if err != nil {
  12.         fmt.Println(“net.ResolveUDPAddr fail.”, err)
  13.         os.Exit(1)
  14.     }
  15.     socket, err := net.DialUDP(“udp”, nil, addr)
  16.     if err != nil {
  17.         fmt.Println(“net.DialUDP fail.”, err)
  18.         os.Exit(1)
  19.     }
  20.     t := time.Now()
  21.     socket.SetDeadline(t.Add(time.Duration(5 * time.Second)))
  22.     // socket.SetWriteDeadline(t.Add(time.Duration(5 * time.Second)))
  23.     // socket.SetReadDeadline(t.Add(time.Duration(5 * time.Second)))
  24.     defer socket.Close()
  25.     r := bufio.NewReader(os.Stdin)
  26.     for {
  27.         switch line, ok := r.ReadString(‘n’); true {
  28.         case ok != nil:
  29.             fmt.Printf(“bye bye!n”)
  30.             return
  31.         default:
  32.             _, err := socket.Write([]byte(line))
  33.             if err != nil {
  34.                 fmt.Println(“error send data,err:”, err)
  35.                 return
  36.             }
  37.             data := make([]byte, 1024)
  38.             _, remoteAddr, err := socket.ReadFromUDP(data)
  39.             if err != nil {
  40.                 fmt.Println(“error recv data,err:”, err)
  41.                 return
  42.             }
  43.             fmt.Printf(“from %s:%sn”, remoteAddr.String(), string(data))
  44.         }
  45.     }
  46. }

3.可以把代表拷贝的本地运行测试,Golang的设置方法有三个:SetDeadline,SetWriteDeadline,SetReadDeadline,设置了Deadline是指定时间戳为超时点,操作指定时间戳连接就会超时,再次发送包,接受包就会超时会提示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

所以要保持心跳在线就需要不断刷新Deadline的时间戳。本文仅供参考,如果有相应场景,会使用到,^_^。


wxgzh:ludong86
qrcode_for_gh_6bb1f39ae99c_258
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
最新发布
05-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值