跳至主要内容

golang udp socket

package main

import (
f "fmt"
"net"
)

func main() {
addr, err := net.ResolveUDPAddr("udp", ":4822")
if err != nil {
f.Println(err)
return
}
c, err := net.ListenUDP("udp", addr)
if err != nil {
f.Println(err)
return
}
defer c.Close()
raddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4821")
c.WriteToUDP([]byte("high ! from shareMe"), raddr)
b := make([]byte, 20)
n, ra, err := c.ReadFromUDP(b)
if err != nil {
f.Println(err)
return
}
f.Println("get msg:", ra.String(), string(b[:n]))
}

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.