示例#1
0
// Listen listens and accepts one incoming UDT connection on a given port,
// and pipes all incoming data to os.Stdout.
func Listen(udtAddr string, tcpAddr string, bs int64) error {
	udt, err := udt.Listen("udt", udtAddr)
	if err != nil {
		return err
	}
	log("udt listening at %s", udt.Addr())

	tcp, err := net.Listen("tcp", tcpAddr)
	if err != nil {
		return err
	}
	log("tcp listening at %s", tcp.Addr())

	done := make(chan interface{})
	defer close(done)
	run := func(l net.Listener) (err error) {
		for {
			select {
			case <-done:
				return
			default:
				var c net.Conn
				c, err = l.Accept()
				if err != nil {
					return
				}
				log("accepted connection from %s", c.RemoteAddr())

				go func() {
					defer c.Close()
					for {
						select {
						case <-done:
							return
						default:
							n, e := io.CopyN(c, c, bs)
							if e == io.EOF {
								log("read EOF, close client socket")
								return
							}
							log("Copied back %d bytes", n)
						}
					}

					return
				}()
			}
		}
	}

	go run(udt)
	go run(tcp)
	// wait until we exit.
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
		syscall.SIGTERM, syscall.SIGQUIT)
	<-sigc
	fmt.Println("Quit now")
	return nil
}
示例#2
0
// Listen listens and accepts one incoming UDT connection on a given port,
// and pipes all incoming data to os.Stdout.
func Listen(localAddr string) error {
	l, err := udt.Listen("udt", localAddr)
	if err != nil {
		return err
	}
	log("listening at %s", l.Addr())

	c, err := l.Accept()
	if err != nil {
		return err
	}
	log("accepted connection from %s", c.RemoteAddr())

	// done with listener
	l.Close()

	netcat(c)
	return c.Close()
}