首页 > 其他分享 >Go - Creating a UDP Client

Go - Creating a UDP Client

时间:2023-10-16 19:56:34浏览次数:31  
标签:UDP err use client Client Go net conn

Problem: You want to create a UDP client to send data to a UDP server.


Solution: Use the Dial function in the net package to connect to a UDP server. Then use the Write method of the net.UDPConn interface to write data to the connection.

 

Creating a UDP client can be very straightforward, and it can look exactly like the TCP client, except the network string is udp instead of tcp :

func   main ()   { 
      conn ,   err   :=   net . Dial ( "udp" ,   ":9001" ) 
      if   err   !=   nil   { 
          log . Fatal ( err ) 
      } 
      defer   conn . Close () 
      conn . Write ([] byte ( "Hello  from  UDP  client" )) 
}

Try it out: set up a UDP listener using nc on one terminal. Use the - u flag to force nc to use UDP and the - l flag to listen for incoming connections:

$  nc  - l  - u  9001

Then run your UDP client on another terminal. You should see “Hello from UDP client” printed out on the server side. 

This works for IPv4 and IPv6. To test this, you’ll use the - 6 flag to force nc to use IPv6 on the server side:

$  nc  - l  - u  - 6  9001

If you run the same client you should see the same output. 

You can also use the net.DialUDP function to create a UDP client:

func   main ()   { 
      raddr ,   err   :=   net . ResolveUDPAddr ( "udp" ,   ":9001" ) 
      if   err   !=   nil   { 
          log . Fatal ( err ) 
      } 
      conn ,   err   :=   net . DialUDP ( "udp" ,   nil ,   raddr ) 
      if   err   !=   nil   { 
          log . Fatal ( err ) 
      } 
      defer   conn . Close () 

      _ ,   err   =   conn . Write ([] byte ( "Hello  from  UDP  client" )) 

      if   err   !=   nil   { 
          log . Fatal ( err ) 
      } 
}

The net.DialUDP function takes a net.UDPAddr as argument. First, use the ne⁠t.R⁠es⁠ol⁠veUD⁠PA⁠dd⁠r function to resolve and create the address. Then use the net.DialUDP function to create the connection. 

To write to the connection use the Write method of the net.UDPConn interface. The Write method takes a byte slice as an argument and returns the number of bytes written and an error.

 

标签:UDP,err,use,client,Client,Go,net,conn
From: https://www.cnblogs.com/zhangzhihui/p/17768214.html

相关文章

  • Go - Creating a UDP Server
    Problem: YouwanttocreateaUDPservertoreceivedatafromaUDPclient.Solution: UsetheListenPacketfunctioninthenetpackagetolistenforincomingpackets.ThenusetheReadFrommethodofthePacketConninterfacetoreaddatafromtheconnecti......
  • linux安装myql-server及libmysqlclient
    1.去官网下载对应的仓库源https://dev.mysql.com/downloads/下面以centos8为例#仓库下载直链为https://dev.mysql.get/{具体的仓库名}wgethttps://dev.mysql.com/get/mysql80-community-release-el8-8.noarch.rpmyuminstall./mysql80-community-release-el8-8.noarch.r......
  • java.io.IOException: Could not find resource mapper/ProductCategoryMapper.xml 解
    java.io.IOException:Couldnotfindresourcemapper/ProductCategoryMapper.xml解决方案 一、问题背景通过MyBatisPlus测试达梦数据库过程中,运行测试类的时候,项目报错:“java.io.IOException:Couldnotfindresourcemapper/ProductCategoryMapper.xml”工程的目录......
  • django的使用
    配置python环境以及环境变量安转django在cmd输入pipinstalldjangopipshowdjangoLocation:c:\users\tian\appdata\roaming\python\python38\site-packages为django的位置将C:\Users\tian\AppData\Roaming\Python\Python38\Scripts写到环境变量中,在用户变量中点击pa......
  • mongodb 安装
    macOS12.6.7使用.tgz,安装版本4.4参考:https://www.mongodb.com/docs/v4.4/tutorial/install-mongodb-on-os-x-tarball/.tgz方式需要自己创建mongod.conf,这里放到了/etc参考:https://www.mongodb.com/docs/v4.4/reference/configuration-options/https://www.mongodb.com/docs......
  • Codeforces Round 635 (Div. 2) B. Kana and Dragon Quest game
    你需要击败一只巨龙,他有\(h\)点血量,你可以使用以下两种攻击方式:黑洞:使巨龙的血量变为\(\lfloor\frac{h}{2}\rfloor+10\)。可以使用\(n\)次。雷击:使巨龙的血量变为\(h-10\)。可以使用\(m\)次/当巨龙的血量\(h\leq0\)时,你将他击败了。询问你是否可以将他击......
  • [ARC167D] Good Permutation 题解
    题意对于一个长度为\(N\)的排列\(Q\),定义其为好的,当且仅当对于任意整数\(i\in\left[1,N\right]\),在进行若干次操作\(i\leftarrowQ_i\)后可以得到\(i=1\)。给定一个排列\(P\),定义一次操作为交换两个数。定义\(M\)为可以将\(P\)变为一个好的的排列的最小操......
  • Codeforces Round 637 (Div. 2) - Thanks, Ivan Belonogov! A. Nastya and Rice
    纳斯塔亚掉了\(n\)个谷物,每个谷物的重量范围在\([a-b,a+b]\)。她猜测谷物的总重量范围在\([c-d,c+d]\)。询问她的猜测是否正确。显然,若\([n(a-b),n(a+b)]\)和\([c-d,c+d]\)有交,则她的猜测正确。view#include<bits/stdc++.h>typedeflonglongll;......
  • client-go实战之七:准备一个工程管理后续实战的代码
    欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos本篇概览本文是《client-go实战》系列的第八篇,主要内容是新建一个golang工程,用于管理代码,后面整个系列的代码都会保存在这个工程中工程结构简述此工程打算写一个简单......
  • Go - Creating a TCP Client
    Problem: YouwanttocreateaTCPclienttosenddatatoaTCPserver.Solution: UsetheDialfunctioninthenetpackagetoconnecttoaTCPserver. CreatingaTCPclientissimilartocreatingaTCPserverbutevensimpler.Themaindifferenceistha......