Esempio n. 1
0
func sendHistory(target string) {

	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+target)
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	CheckError(err)

	defer Conn.Close()

	msg := "history "

	for k, v := range hist {

		msg += k + ":" + strconv.FormatInt(v, 10) + " "

	}

	// fmt.Println("Sending history:", msg)

	buf := []byte(msg)
	_, err = Conn.Write(buf)

	// fmt.Printf("Sent history to %s\n", target)

	if err != nil {
		fmt.Println(msg, err)
	}

}
Esempio n. 2
0
func main() {

	udpAddr, err := net.ResolveUDPAddr("udp", ":20014")
	if err != nil {
		log.Fatal(err)
	}

	udpListen, err := net.ListenUDP("udp", udpAddr)
	if err != nil {
		log.Fatal(err)
	}

	backupvalue := backup(udpListen)
	fmt.Println(backupvalue)

	udpListen.Close()

	udpAddr, err = net.ResolveUDPAddr("udp", "129.241.187.255:20014")
	if err != nil {
		log.Fatal(err)
	}

	udpBroadcast, err := net.DialUDP("udp", nil, udpAddr)
	if err != nil {
		log.Fatal(err)
	}

	primary(backupvalue, udpBroadcast)

	udpBroadcast.Close()

}
Esempio n. 3
0
func InitSendService(speed int64, from string, to string) (service *SendService, err error) {
	service = new(SendService)
	service.speed = speed

	addr, err := net.ResolveUDPAddr("udp4", from)
	if err != nil {
		return nil, err
	}
	service.addr, err = net.ResolveUDPAddr("udp4", to)
	if err != nil {
		return nil, err
	}
	service.conn, err = net.ListenUDP("udp4", addr)
	if err != nil {
		return nil, err
	}

	// conn, err := net.Dial("udp", "127.0.0.1:12345")

	// if err != nil {
	// 	panic(err)
	// }

	// service.conn = conn

	return service, nil
}
Esempio n. 4
0
func main() {
	//测试手动注册两个通道
	sip.GetChannel("10.2.42.197", "10.2.42.133")
	sip.GetChannel("10.2.42.133", "10.2.42.197")
	//链接WebRTC 服务
	go webrtcSocket.Init()
	//开启出测地址
	go sip.InitListener()

	//监听视频端口
	service := ":8088"
	udpAddr, err := net.ResolveUDPAddr("udp", service)
	checkError(err)
	conn, err := net.ListenUDP("udp", udpAddr)
	checkError(err)
	fmt.Println("监听端口")
	for {
		handleVideo(conn)
	}

	//监听语音端口
	go func() {
		serviceAudio := ":11113"
		udpAddrAudio, err := net.ResolveUDPAddr("udp", serviceAudio)
		checkError(err)
		connAudio, err := net.ListenUDP("udp", udpAddrAudio)
		checkError(err)

		for {
			handleAudio(connAudio)
		}
	}()

}
func main() {
	addr, err := net.ResolveUDPAddr("0.0.0.0:10000")
	if err != nil {
		handle_err(err)
	}

	addr_out, err := net.ResolveUDPAddr("localhost:10020")
	if err != nil {
		handle_err(err)
	}

	c, err := net.ListenUDP("udp4", addr)
	if err != nil {
		handle_err(err)
	}

	c_out, err := net.DialUDP("udp4", nil, addr_out)
	if err != nil {
		handle_err(err)
	}

	buf := make([]byte, 2000)
	for {
		length, _, err := c.ReadFrom(buf)
		if err != nil {
			handle_err(err)
		}
		c_out.Write(buf[0:length])
	}
}
Esempio n. 6
0
func send(target, msg, tempo string) {

	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+target)
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	CheckError(err)

	// fmt.Println(port)

	defer Conn.Close()

	msg += ":" + tempo

	buf := []byte(msg)

	_, err = Conn.Write(buf)

	// fmt.Printf("Sent message %s to %s\n", msg, target)
	if err != nil {
		fmt.Println(msg, err)
	}
}
Esempio n. 7
0
// Accepts a UdpAction and a one-way channel to write the results to.
func DoUdpRequest(udpAction UdpAction, resultsChannel chan HttpReqResult, sessionMap map[string]string) {

	address := SubstParams(sessionMap, udpAction.Address)
	payload := SubstParams(sessionMap, udpAction.Payload)

	if udpconn == nil {
		ServerAddr, err := net.ResolveUDPAddr("udp", address) //"127.0.0.1:10001")
		if err != nil {
			fmt.Println("Error ResolveUDPAddr remote: " + err.Error())
		}

		LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
		if err != nil {
			fmt.Println("Error ResolveUDPAddr local: " + err.Error())
		}

		udpconn, err = net.DialUDP("udp", LocalAddr, ServerAddr)
		if err != nil {
			fmt.Println("Error Dial: " + err.Error())
		}
	}
	//defer Conn.Close()
	start := time.Now()
	if udpconn != nil {
		_, err = fmt.Fprintf(udpconn, payload+"\r\n")
	}
	if err != nil {
		fmt.Printf("UDP request failed with error: %s\n", err)
		udpconn = nil
	}

	elapsed := time.Since(start)
	resultsChannel <- buildUdpResult(0, 200, elapsed.Nanoseconds(), udpAction.Title)

}
Esempio n. 8
0
func main() {
	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10001")
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	Conn.SetReadBuffer(1024 * 1024 * 1024)
	Conn.SetWriteBuffer(1024 * 1024 * 1024)

	CheckError(err)

	defer Conn.Close()

	start := time.Now()
	for i := 0; i < maxTestCount; i++ {
		msg := strconv.Itoa(i)
		//i++
		buf := []byte(msg)
		_, err := Conn.Write(buf)
		if err != nil {
			fmt.Println(msg, err)
		}
		time.Sleep(time.Nanosecond * 100)
	}

	timeDiff := time.Since(start)
	timeDiffInSec := float64(timeDiff) * 1e-9
	fmt.Printf("Excecution time:%f sec, %f ops/s\n", timeDiffInSec, maxTestCount/timeDiffInSec)
}
Esempio n. 9
0
func main() {
	udpAddr, err := net.ResolveUDPAddr("udp", port)
	checkError(err)
	Listener, err := net.ListenUDP("udp", udpAddr)
	checkError(err)
	
	if kind == "server" {
		svr = server.NewServer()
		go svr.Start(Listener)
	
		sigmask()
	
		svr.Stop()
		clean()
	}
	else if kind == "client" {
		cli := client.NewClient(Listener)
		serverUdp , err := net.ResolveUDPAddr("udp", serverAddr)
		checkError(err)
		go cli.Start(serverUdp)
		
		sigmask()
		cli.Stop()
		clean()
	}
	else {
		log.Println("cli parameter wrong")
		os.Exit(1)
	}
}
Esempio n. 10
0
func (this *SocksProxy) read(pack *RequestPack) {
	log.Println(pack.DSTAddr, pack.DSTPort)
	//获取一个可用的端口
	packConn, err := net.ListenPacket("udp", "127.0.0.1:0")
	localPortStr := strings.Split(packConn.LocalAddr().String(), ":")[1]
	log.Println(localPortStr)

	remotAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:"+localPortStr)
	locaAddr, _ := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(pack.DSTPort))
	conn, err := net.ListenUDP("udp", locaAddr)
	if err != nil {
		log.Println("连接客户端失败", err.Error())
	}
	portBuf := bytes.NewBuffer([]byte{})
	localPortInt, _ := strconv.Atoi(localPortStr)
	binary.Write(portBuf, binary.LittleEndian, int16(localPortInt))
	port := portBuf.Bytes()
	_, err = conn.WriteToUDP([]byte{5, 0, 0, 1, 127, 0, 0, 1, port[0], port[1]}, remotAddr)
	buf := make([]byte, 1024)
	n, _, err := conn.ReadFromUDP(buf)
	if err != nil {
		log.Println("haha", err.Error())
	}
	log.Println("udp----client---", buf[:n])

}
Esempio n. 11
0
File: miax.go Progetto: ikravets/ev
func newMiaxMcastServer(c Config, src *miaxMessageSource, i int) (mms *miaxMcastServer) {
	laddr, err := net.ResolveUDPAddr("udp", c.LocalAddr)
	errs.CheckE(err)
	mcaddr, err := net.ResolveUDPAddr("udp", c.FeedAddr)
	errs.CheckE(err)
	laddr.Port += i + LocalPortShift
	mcaddr.Port += i
	mcaddr.IP[net.IPv6len-1] += (byte)(i)
	gapP := c.GapPeriod
	if 0 == gapP {
		gapP = 0xFFFFFFFFFFFFFFFF
	}
	mms = &miaxMcastServer{
		laddr:     laddr,
		mcaddr:    mcaddr,
		src:       src,
		cancel:    make(chan struct{}),
		num:       i,
		gap:       0 != c.GapSize,
		gapSize:   c.GapSize,
		gapPeriod: gapP,
		gapCnt:    0,
	}
	return
}
Esempio n. 12
0
func main() {

	serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
	CheckError(err)

	localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8079")
	CheckError(err)

	conn, err := net.DialUDP("udp", localAddr, serverAddr)
	CheckError(err)

	defer conn.Close()
	i := 0

	fmt.Println("Client connected to: ", serverAddr)

	for {
		t := test.Packet_Type(1)
		h := true
		msg := &test.Packet{Type: &t, Heartbeat: &test.Heartbeat{Heartbeat: &h}}
		i++
		buff, _ := proto.Marshal(msg)
		_, err := conn.Write(buff)
		CheckError(err)
		time.Sleep(time.Second * 1)
	}

	fmt.Println("Connection closed...")
}
Esempio n. 13
0
func isLocalAddress(local, localRemote string) bool {
	// Resolve the IP returned by the STUN server first.
	localRemoteAddr, err := net.ResolveUDPAddr("udp", localRemote)
	if err != nil {
		return false
	}

	// Try comparing with the local address on the socket first, but only if
	// it's actually specified.
	addr, err := net.ResolveUDPAddr("udp", local)
	if err == nil && addr.IP != nil && !addr.IP.IsUnspecified() {
		return addr.IP.Equal(localRemoteAddr.IP)
	}

	// Fallback to checking IPs of all interfaces
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return false
	}
	for _, addr := range addrs {
		ip, _, err := net.ParseCIDR(addr.String())
		if err != nil {
			continue
		}
		if ip.Equal(localRemoteAddr.IP) {
			return true
		}
	}
	return false
}
Esempio n. 14
0
func setup() error {

	// Resolve bind address
	saddr, err := net.ResolveUDPAddr("udp", env.GetDefault("COLLECTD_PROXY_BIND", "127.0.0.1:25827"))
	if err != nil {
		return err
	}

	// Listen udp socket
	proxy, err = net.ListenUDP("udp", saddr)
	if err != nil {
		return err
	}

	// Resolve target address
	taddr, err := net.ResolveUDPAddr("udp", env.GetDefault("COLLECTD_PROXY_TARGET", "127.0.0.1:25826"))
	if err != nil {
		return err
	}

	target, err = net.DialUDP("udp", nil, taddr)
	if err != nil {
		return err
	}

	return nil
}
Esempio n. 15
0
func main() {

	argcnt := len(os.Args)

	if argcnt != 2 {
		fmt.Println("usage: udptest.exe [IPv6_of_LobaroBox:Port]\r\nExample: udptest.exe [fe80:0000:0000:0000:0211:7d00:0030:8e3f]:5684")
		return
	}

	//Remote Addr
	BoxAddr, err := net.ResolveUDPAddr("udp6", os.Args[1])
	if err != nil {
		log.Fatal(err)
		return
	}

	LocalAddr, err := net.ResolveUDPAddr("udp6", ":0") //:0 => OS sets local port
	if err != nil {
		log.Fatal(err)
		return
	}

	c, err := net.ListenUDP("udp6", LocalAddr)
	if err != nil {
		log.Fatal(err)
		return
	}
	defer c.Close()

	fmt.Println("Start Listening to:", c.LocalAddr())

	//readsocket loop
	go func(c *net.UDPConn) {
		for {
			rxMsg := make([]byte, 512)
			n, remote, err := c.ReadFromUDP(rxMsg)

			if err != nil {
				log.Fatal(err)
				return
			} else {
				fmt.Println("Got from ", remote, " ", n, "Bytes: ", string(rxMsg[:n]))
			}
		}
	}(c)

	//write every second current time string to remote "LOBARO IPV6 UNIVERSAL BOX"
	for {
		b := []byte(time.Now().String())
		n, err := c.WriteTo(b, BoxAddr)
		if err != nil {
			log.Fatal(err)
			return
		} else {
			fmt.Println("Wrote to ", BoxAddr, " ", n, " Bytes: ", string(b[:n]))
		}

		time.Sleep(1000 * time.Millisecond)
	}
}
Esempio n. 16
0
func Udp_init(localListenPort, broadcastListenPort, message_size int, send_ch, receive_ch chan Udp_message) (err error) {
	//Generating broadcast address
	baddr, err = net.ResolveUDPAddr("udp4", "255.255.255.255:"+strconv.Itoa(broadcastListenPort))
	if err != nil {
		return err
	}

	//Generating localaddress
	tempConn, err := net.DialUDP("udp4", nil, baddr)
	defer tempConn.Close()
	tempAddr := tempConn.LocalAddr()
	laddr, err = net.ResolveUDPAddr("udp4", tempAddr.String())
	laddr.Port = localListenPort

	//Creating local listening connections
	localListenConn, err := net.ListenUDP("udp4", laddr)
	if err != nil {
		return err
	}

	//Creating listener on broadcast connection
	broadcastListenConn, err := net.ListenUDP("udp", baddr)
	if err != nil {
		localListenConn.Close()
		return err
	}

	go udp_receive_server(localListenConn, broadcastListenConn, message_size, receive_ch)
	go udp_transmit_server(localListenConn, broadcastListenConn, send_ch)

	//	fmt.Printf("Generating local address: \t Network(): %s \t String(): %s \n", laddr.Network(), laddr.String())
	//	fmt.Printf("Generating broadcast address: \t Network(): %s \t String(): %s \n", baddr.Network(), baddr.String())
	return err
}
Esempio n. 17
0
func main() {
	var (
		laddr  *net.UDPAddr
		mcaddr *net.UDPAddr

		conn  *net.UDPConn
		lconn *net.UDPConn

		err error
	)

	laddr, err = net.ResolveUDPAddr("udp", ":0")
	if err != nil {
		logrus.Panicf("Error resolving UDP address with %v", err)
	}

	mcaddr, err = net.ResolveUDPAddr("udp", "239.255.2.122:1234")
	if err != nil {
		logrus.Panicf("Error resolving UDP address with %v", err)
	}

	conn, err = net.ListenMulticastUDP("udp", nil, mcaddr)
	if err != nil {
		logrus.Panic(err)
	}

	lconn, err = net.ListenUDP("udp", laddr)
	if err != nil {
		logrus.Panic(err)
	}

	go listen(conn)
	go write(conn, lconn, mcaddr)
}
Esempio n. 18
0
func exampleClient(address string) {
	ServerAddr, err := net.ResolveUDPAddr("udp", address)
	if err != nil {
		_ = fmt.Errorf("Error %v", err)
		return
	}

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	if err != nil {
		_ = fmt.Errorf("Error %v", err)
		return
	}

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	if err != nil {
		_ = fmt.Errorf("Error %v", err)
		return
	}

	defer Conn.Close()
	for {
		msg := &udpserver.ClientMessage{}
		msg.ID = "123"
		msg.Body = "Hello World"
		buf, _ := msg.ToJSON()
		_, err := Conn.Write(buf)
		if err != nil {
			fmt.Println(msg, err)
		}
		time.Sleep(time.Second * 1)
	}
}
Esempio n. 19
0
func NewUdpProxy(listenAddr, targetAddr string) (*UdpProxy, error) {
	var listenUDP, targetUDP *net.UDPAddr
	var err error
	if listenUDP, err = net.ResolveUDPAddr("udp", listenAddr); err != nil {
		return nil, err
	}
	if targetUDP, err = net.ResolveUDPAddr("udp", targetAddr); err != nil {
		return nil, err
	}

	listenConn, err := net.ListenUDP("udp", listenUDP)
	if err != nil {
		return nil, err
	}
	// TODO http://play.golang.org/p/ygGFr9oLpW
	// for per-UDP-packet addressing in case one proxy handles multiple connections
	targetConn, err := net.DialUDP("udp", nil, targetUDP)
	if err != nil {
		listenConn.Close()
		return nil, err
	}

	return &UdpProxy{
		listenConn:      listenConn,
		listenAddr:      listenUDP,
		targetConn:      targetConn,
		targetAddr:      targetUDP,
		packets:         make(chan []byte, BufferedPackets),
		proxyClosed:     golib.NewOneshotCondition(),
		writeErrors:     make(chan error, buf_write_errors),
		Stats:           stats.NewStats("UDP Proxy " + listenAddr),
		OnError:         OnErrorClose,
		writePausedCond: sync.Cond{L: new(sync.Mutex)},
	}, nil
}
Esempio n. 20
0
func sampleTest() {
	master_addr := "127.0.0.1:10001"
	slave1_addr := "127.0.0.1:10002"
	slave2_addr := "127.0.0.1:10003"

	master_addrs := []string{master_addr}
	slave_addrs := []string{slave1_addr, slave2_addr}
	master_heartbeat := new(Heartbeat)
	master_heartbeat.Initialize(master_addr, slave_addrs, slave_addrs)
	slave1_heartbeat := new(Heartbeat)
	slave1_heartbeat.Initialize(slave1_addr, master_addrs, master_addrs)
	// remove slave2_heartbeat to test dead function
	// slave2_heartbeat := new(Heartbeat)
	// slave2_heartbeat.Initialize(slave2_addr, master_addrs, master_addrs)

	// need receive at least one notification (packet) to start detection
	time.Sleep(time.Second * 3)
	master_udpaddr, err := net.ResolveUDPAddr("udp", master_addr)
	checkError(err)
	slave2_udpaddr, err := net.ResolveUDPAddr("udp", slave2_addr)
	checkError(err)
	socket2, err := net.ListenUDP("udp", slave2_udpaddr)
	checkError(err)
	socket2.WriteToUDP([]byte(slave2_addr), master_udpaddr)

	deadChan := master_heartbeat.GetDeadChan()
	for {
		dead := <-deadChan
		fmt.Println(dead)
		updated_slave_addrs := []string{slave1_addr}
		master_heartbeat.Update(updated_slave_addrs, updated_slave_addrs)
	}
}
Esempio n. 21
0
func (c *Client) pingUdp() {

	var pingtime = 1 * time.Second

	localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:0", c.localAddr))
	if err != nil {
		panic(err)
	}
	daemonAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", c.daemonAddr, c.daemonPortUdp))

	if err != nil {
		panic(err)
	}
	c.logger.Println("starting to pindg address", daemonAddr)
	con, err := net.DialUDP("udp", localAddr, daemonAddr)
	if err != nil {
		panic(err)
	}
	t := time.NewTimer(pingtime)
	s := c.stop.Future().AsChan()

	for {
		select {
		case <-s:
			return

		case <-t.C:
			con.Write([]byte(fmt.Sprintf("%s:%d", c.uuid, c.localPort)))
			t.Reset(pingtime)
		}
	}

}
Esempio n. 22
0
func main() {
	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10001")
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	CheckError(err)

	defer Conn.Close()
	i := 0

	for {
		msg := strconv.Itoa(i)
		i++
		buf := []byte(msg)
		_, err := Conn.Write(buf)

		if err != nil {
			fmt.Println(msg, err)
		}

		time.Sleep(time.Second * 1)
	}
}
Esempio n. 23
0
func udp_init() (err error) {
	//Generating broadcast address
	baddr, err = net.ResolveUDPAddr("udp4", "255.255.255.255:"+strconv.Itoa(30000))
	if err != nil {
		return err
	}

	//Generating localaddress
	tempConn, err := net.DialUDP("udp4", nil, baddr)
	defer tempConn.Close()
	tempAddr := tempConn.LocalAddr()
	laddr, err = net.ResolveUDPAddr("udp4", tempAddr.String())
	laddr.Port = 30000 //localListenPort

	//Creating local listening connections
	//localListenConn, err := net.ListenUDP("udp4", laddr)
	//if err != nil {
	//	return err
	//	}

	//Creating listener on broadcast connection
	//broadcastListenConn, err := net.ListenUDP("udp", baddr)
	//if err != nil {
	//	localListenConn.Close()
	//	return err
	//}

	//go udp_receive_server(localListenConn, broadcastListenConn, 1024, receive_ch)
	//go udp_transmit_server(localListenConn, broadcastListenConn, send_ch)

	fmt.Printf("Generating local address: \t Network(): %s \t String(): %s \n", laddr.Network(), laddr.String())
	fmt.Printf("Generating broadcast address: \t Network(): %s \t String(): %s \n", baddr.Network(), baddr.String())
	return err
}
func main() {
	// Listen for data through this address:port
	local, err := net.ResolveUDPAddr("udp4", ":20010")
	conn2, err := net.ListenUDP("udp4", local)
	if err != nil {
		log.Fatal(err)
	}

	// Send data to this address:port (broadcast address)
	remote, err := net.ResolveUDPAddr("udp4", "129.241.187.23:20010")
	if err != nil {
		log.Fatal(err)
	}

	conn, err := net.DialUDP("udp4", nil, remote)
	if err != nil {
		log.Fatal(err)
	}

	read_channel := make(chan []byte)

	go ListenThing(conn2, read_channel)
	go WriteThing(conn)

	for {
		log.Println(<-read_channel)
	}
}
Esempio n. 25
0
func _TestMuxOverUDP(t *testing.T) {
	// Bind acceptor link
	aaddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:44000")
	if err != nil {
		t.Fatalf("resolve a-addr: %s", err)
	}
	alink, err := BindUDPLink("udp", aaddr)
	if err != nil {
		t.Fatalf("bind udp a-link: %s", err)
	}

	// Bind dialer link
	daddr, err := net.ResolveUDPAddr("udp", "0.0.0.0:44001")
	if err != nil {
		t.Fatalf("resolve d-addr: %s", err)
	}
	dlink, err := BindUDPLink("udp", daddr)
	if err != nil {
		t.Fatalf("bind udp d-link: %s", err)
	}

	// Resolve dialer address
	addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:44000")
	if err != nil {
		t.Fatalf("resolve addr: %s", err)
	}

	ee := newEndToEnd(t, alink, dlink, addr, 10)
	ee.Run()
}
Esempio n. 26
0
func main() {

	//net.UDPConn("")
	//set up send-socket
	remote_addr, _ := net.ResolveUDPAddr("udp", "129.241.187.255:20018") //129.241.187.255 using 15 because of workspace
	socket_send, err := net.DialUDP("udp", nil, remote_addr)
	CheckError(err)

	//set up a listen-socket
	port, _ := net.ResolveUDPAddr("udp", ":20018")
	socket_listen, err := net.ListenUDP("udp", port)
	CheckError(err)

	//close sockets when done
	defer socket_listen.Close()
	defer socket_send.Close()

	for {
		//Send message
		msg := "hei server"
		socket_send.Write([]byte(msg))

		//Listen to message
		var buffer [1024]byte //64
		length, addr, err := socket_listen.ReadFromUDP(buffer[:])
		log.Println(length)
		log.Println(addr)
		log.Println(err)
		log.Println(string(buffer[:]), "\n")
		time.Sleep(1 * time.Second)

	}

}
Esempio n. 27
0
func main() {
	theCache = cache.New(time.Hour, time.Minute)

	var err error
	server, err = net.ResolveUDPAddr("udp", "208.67.220.220:53")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Can't resolve server address: %v\n", err)
		os.Exit(1)
	}

	addr, err := net.ResolveUDPAddr("udp", "0.0.0.0:53")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Can't resolve service address: %v\n", err)
		os.Exit(1)
	}
	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Can't listen on udp port: %v\n", err)
		os.Exit(1)
	}

	replyCh = make(chan *reply)
	go sendReply(conn)

	for {
		reqData := make([]byte, 2048)
		nr, client, err := conn.ReadFromUDP(reqData)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to read from client: %v\n", err)
			//checkTempErr(err)
			continue
		}
		go proxyClientDNS(reqData[:nr], client)
	}
}
Esempio n. 28
0
func ListenAndServe(addr string, port int) {
	//addr_tcp, err := net.ResolveTCPAddr("tcp", l)
	//if err != nil {
	//	panic(err)
	//}
	//listen_tcp, err := net.ListenTCP("tcp", addr_tcp)
	//if err != nil {
	//	panic(err)
	//}
	//go tcp_listen(listen_tcp)
	rtp_l := fmt.Sprintf("%v:%v", addr, port)
	rtcp_l := fmt.Sprintf("%v:%v", addr, port+1)
	addr1, err := net.ResolveUDPAddr("udp", rtp_l)
	if err != nil {
		panic(err)
	}
	listen_rtp, err := net.ListenUDP("udp", addr1)
	if err != nil {
		panic(err)
	}
	go udp_rtp_listen(listen_rtp)

	addr2, err := net.ResolveUDPAddr("udp", rtcp_l)
	if err != nil {
		panic(err)
	}
	listen_rtcp, err := net.ListenUDP("udp", addr2)
	if err != nil {
		panic(err)
	}
	go udp_rtcp_listen(listen_rtcp)
}
Esempio n. 29
0
func main() {
	var bindAddress string
	var port int

	flag.IntVar(&port, "port", 9125, "Port to listen on")
	flag.IntVar(&port, "p", 9125, "Port to listen on")

	flag.StringVar(&bindAddress, "bind", "0.0.0.0", "IP Address to listen on")
	flag.StringVar(&bindAddress, "b", "0.0.0.0", "IP Address to listen on")

	flag.StringVar(&prefix, "prefix", "statsrelay", "The prefix to use with self generated stats")

	flag.BoolVar(&verbose, "verbose", false, "Verbose output")
	flag.BoolVar(&verbose, "v", false, "Verbose output")

	flag.Parse()

	if len(flag.Args()) == 0 {
		log.Fatalf("One or most host specifications are needed to locate statsd daemons.\n")
	}

	hashRing = consistent.New()
	hashRing.NumberOfReplicas = 1

	for _, v := range flag.Args() {
		var addr *net.UDPAddr
		var err error
		host := strings.Split(v, ":")

		switch len(host) {
		case 1:
			log.Printf("Invalid statsd location: %s\n", v)
			log.Fatalf("Must be of the form HOST:PORT or HOST:PORT:INSTANCE\n")
		case 2:
			addr, err = net.ResolveUDPAddr("udp", v)
			if err != nil {
				log.Printf("Error parsing HOST:PORT \"%s\"\n", v)
				log.Fatalf("%s\n", err.Error())
			}
		case 3:
			addr, err = net.ResolveUDPAddr("udp", host[0]+":"+host[1])
			if err != nil {
				log.Printf("Error parsing HOST:PORT:INSTANCE \"%s\"\n", v)
				log.Fatalf("%s\n", err.Error())
			}
		default:
			log.Fatalf("Unrecongnized host specification: %s\n", v)
		}

		if addr != nil {
			udpAddr[v] = addr
			hashRing.Add(v)
		}
	}

	epochTime = time.Now().Unix()
	runServer(bindAddress, port)

	log.Printf("Normal shutdown.\n")
}
Esempio n. 30
0
func send(target, msg string) {
	ServerAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:"+target)
	CheckError(err)

	LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
	CheckError(err)

	Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
	CheckError(err)

	// fmt.Println(port)
	defer fmt.Printf("Finished sending %s to %s\n", msg, target)
	defer Conn.Close()

	// for {

	buf := []byte(msg)
	_, err = Conn.Write(buf)
	if err != nil {
		fmt.Println(msg, err)
	}

	// time.Sleep(time.Second * 1)
	// }
}