Go language UDP Small notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
<pre style= "margin-top:0px; margin-bottom:0px; " ><span style= "color: #0000ff;" >package</span><span style= "color: #f57900;" > </span>main
Import(
  "FMT"
  "NET"
  "Time"
)
FuncMain (){
 udpaddr,err: =net. RESOLVEUDPADDR ("UDP","127.0.0.1:1789")
  iferr!=nil{
 FMT. PRINTLN (ERR)
  return
    }
 lis,err: =net. LISTENUDP ("UDP",udpaddr)
  iferr!=nil{
 FMT. PRINTLN (ERR)
  return
    }
  deferlis. Close ()
  for{
 buf: =make ([]byte,512)
 N,addr,err: =lis. READFROMUDP (BUF)
  iferr!=nil{
 FMT. PRINTLN (ERR)
  Continue
        }
 str: =string(Buf[:n])
  gofunc(Lis*net. Udpconn,addr*net. UDPADDR,bodystring){
 Lis. WRITETOUDP ([]byte(body),addr)
 time. Sleep (3E9)
 } (LIS,addr,str)
    }
}
Package Mainimport ("bytes" "Encoding/json" "FMT" "IO" "NET" "OS") type Fileinfo struct {Name stringpath string}func server ( ) {laddr: = &net. UDPADDR{IP: []byte{127, 0, 0, 1}, Port:1789}con, err: = Net. LISTENUDP ("UDP", laddr) if err! = Nil {fmt. PRINTLN (err) return}defer con. Close () for {Buf: = make ([]byte, 1024x768) n, raddr, _: = Con. READFROMUDP (Buf) var info fileinfojson.unmarshal (Buf[:n], &info) if info. Name! = "" {if info. Path! = "" {e: = os. Chdir (info. Path) If E! = nil {con. WriteTo ([]byte (E.error ()), raddr) Continue}}con. WRITETOUDP ([]byte ("OK"), raddr) File, _: = OS. Create (info. Name) defer file.close () for {Buf: = make ([]byte, 4096) N, r, _: = Con. READFROMUDP (Buf) if r.string ()! = raddr. String () {continue}if n = = 4 && bytes. Contains ([]byte{0, D, D, D}, Buf[:n]) {File.sync () file.close () Break}file.write (Buf[:n])}}}}func client () {con, E: = Net. Dial ("UDP", "127.0.0.1:1789") if E! = nil {fmt. Println (e) Return}defer con. Close () var info Fileinfo = fileinfo{name: "WinMD5.exe", Path: "D:"}b, _: = JSON. Marshal (info) con. Write ([]byte (b)) Buf: = Make ([]byte, 1024x768) n, _: = Con. Read (Buf) if string (buf[:n] = = "OK" {File, _: = OS. Open ("3.go") for {Buf: = make ([]byte, 4096) n, E: = File.read (BUF) if E! = Nil {if E = = io. EOF {break}fmt. Println (e) Break}con. Write (Buf[:n])}con. Write ([]byte{0, 69, 79, 70}}}

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.