当前位置: 首页>>代码示例>>Golang>>正文


Golang net.ListenTCP函数代码示例

本文整理汇总了Golang中net.ListenTCP函数的典型用法代码示例。如果您正苦于以下问题:Golang ListenTCP函数的具体用法?Golang ListenTCP怎么用?Golang ListenTCP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ListenTCP函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewTCPJSONHandler

func NewTCPJSONHandler(a *giles.Archiver, addPort, queryPort, subscribePort int) *TCPJSONHandler {
	var err error
	t := &TCPJSONHandler{a: a, errors: make(chan error)}

	t.addAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(addPort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", addPort, err)
	}
	t.addConn, err = net.ListenTCP("tcp", t.addAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	t.queryAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(queryPort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", queryPort, err)
	}
	t.queryConn, err = net.ListenTCP("tcp", t.queryAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	t.subscribeAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(subscribePort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", subscribePort, err)
	}
	t.subscribeConn, err = net.ListenTCP("tcp", t.subscribeAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	return t
}
开发者ID:gtfierro,项目名称:giles2,代码行数:33,代码来源:handlers.go

示例2: server_thread

func server_thread(wg_server *sync.WaitGroup) {
	defer wg_server.Done()
	server_state = RUNNING
	logger.Println("Launching server...")
	runtime.GOMAXPROCS(runtime.NumCPU())
	tsp_task_manager.CreateTaskManager()

	// listen workers
	laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:5000")
	if nil != err {
		logger.Printf("ResolveTCPAddr (worker) error: %v\n", err)
		server_state = STOPPED
		return
	}
	listener, err := net.ListenTCP("tcp", laddr)
	if err != nil {
		logger.Printf("workers listen error: %v\n", err)
		server_state = STOPPED
		return
	}
	var wg_workers_accept sync.WaitGroup
	wg_workers_accept.Add(1)
	go accept_workers(listener, &wg_workers_accept)

	// listen clients
	laddr, err = net.ResolveTCPAddr("tcp", "127.0.0.1:6000")
	if nil != err {
		logger.Printf("ResolveTCPAddr (client) error: %v\n", err)
		server_state = STOPPED
		return
	}
	listener, err = net.ListenTCP("tcp", laddr)
	if err != nil {
		logger.Printf("clients listen error: %v\n", err)
		server_state = STOPPED
		return
	}
	var wg_clients_accept sync.WaitGroup
	wg_clients_accept.Add(1)
	go accept_clients(listener, &wg_clients_accept)
	logger.Println("Server work is started")

	// listen commands
	for {
		cmd := <-ch_cmd
		if cmd == SERVER_QUIT {
			// server quit
			server_state = STOPPED
			wg_workers_accept.Wait()
			wg_clients_accept.Wait()
			logger.Println("Server work is finished")
			return
		}
	}
}
开发者ID:Alnen,项目名称:university_distributed_os_course,代码行数:55,代码来源:main_server.go

示例3: main

func main() {
	addr := ":8053"
	at, _ := net.ResolveTCPAddr("tcp6", addr)
	_, e := net.ListenTCP("tcp6", at)
	if e != nil {
		fmt.Printf("%s\n", e.Error())
	}
	au, _ := net.ResolveTCPAddr("tcp4", addr)
	_, e = net.ListenTCP("tcp4", au)
	if e != nil {
		fmt.Printf("%s\n", e.Error())
	}
}
开发者ID:tjyang,项目名称:cpam,代码行数:13,代码来源:AddressInUsed.go

示例4: startServer

func (fileServer *FileServer) startServer() {
	tcpaddr, err := net.ResolveTCPAddr("tcp", "localhost:"+strconv.Itoa(fileServer.Port))
	fileServer.check(err)
	if fileServer.Rf.Isdown == 1 {
		fileServer.Rf.Isdown = 0
		fmt.Printf("Node %d up again\n", fileServer.Rf.Id())
		fileServer.Rf.Up()
		if len(fileServer.NeedClose) != 0 {
			<-fileServer.NeedClose
		}
		if len(fileServer.CloseAll) != 0 {
			<-fileServer.CloseAll
		}
		time.Sleep(10 * time.Second)
	}
	tcp_acceptor, err := net.ListenTCP("tcp", tcpaddr)
	fileServer.check(err)
	defer tcp_acceptor.Close()
	go fileServer.backEnd()
	go fileServer.Rf.ProcessEvents()
	for {
		if len(fileServer.CloseAll) != 0 {
			fileServer.Rf.Shutdown()
			break
		} else if fileServer.Rf.IsClosedRaft() {
			break
		} else {
			for len(fileServer.CloseAll) == 0 && fileServer.Rf.LeaderId() != fileServer.Id {
				if tcp_acceptor != nil {
					tcp_acceptor.Close()
					tcp_acceptor = nil
				}
				time.Sleep(1 * time.Second)
			}
			if len(fileServer.CloseAll) != 0 {
				fileServer.Rf.Shutdown()
				break
			} else if fileServer.Rf.IsClosedRaft() {
				break
			}
			if tcp_acceptor == nil {
				tcp_acceptor, err = net.ListenTCP("tcp", tcpaddr)
				fileServer.check(err)
			}
			tcp_conn, err := tcp_acceptor.AcceptTCP()
			fileServer.check(err)
			go fileServer.serve(tcp_conn)
		}
	}
	tcp_acceptor.Close()
}
开发者ID:rahulshcse,项目名称:cs733,代码行数:51,代码来源:server.go

示例5: NewTCPServer

func NewTCPServer(t lotf.Tail, addr *net.TCPAddr) (*StreamServer, error) {
	listener, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return nil, err
	}
	return &StreamServer{t, listener, make(chan bool, 1)}, nil
}
开发者ID:wxdublin,项目名称:lotf,代码行数:7,代码来源:stream.go

示例6: main

func main() {

	logIt("SETUP", "Starting...")

	addr, err := net.ResolveTCPAddr("ip4", ":4848")
	errorCheck(err, "Problem resolving TCP address")

	listen, err := net.ListenTCP("tcp", addr)
	errorCheck(err, "TCP listening error")

	logIt("SETUP", "Ready.")

	for {
		connection, err := listen.Accept()
		if err != nil {
			continue
		}
		logIt("CONNECTION", "Got new connection")

		go newClient(connection)

	}

	os.Exit(0)
}
开发者ID:CS340,项目名称:Highscore-Server,代码行数:25,代码来源:highscoreServer.go

示例7: CreateListener

func CreateListener(flags *TorrentFlags) (listener net.Listener, externalPort int, err error) {
	nat, err := CreatePortMapping(flags)
	if err != nil {
		err = fmt.Errorf("Unable to create NAT: %v", err)
		return
	}
	listenPort := flags.Port
	if nat != nil {
		var external net.IP
		if external, err = nat.GetExternalAddress(); err != nil {
			err = fmt.Errorf("Unable to get external IP address from NAT: %v", err)
			return
		}
		log.Println("External ip address: ", external)
		if listenPort, err = chooseListenPort(nat, listenPort); err != nil {
			log.Println("Could not choose listen port.", err)
			log.Println("Peer connectivity will be affected.")
		}
	}
	listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: listenPort})
	if err != nil {
		log.Fatal("Listen failed:", err)
	}
	log.Println("Listening for peers on port:", listenPort)
	externalPort = listenPort
	return
}
开发者ID:shangma,项目名称:Taipei-Torrent,代码行数:27,代码来源:listen.go

示例8: NewTestServer

func NewTestServer(t testing.TB, protocol uint8) *TestServer {
	laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}

	listen, err := net.ListenTCP("tcp", laddr)
	if err != nil {
		t.Fatal(err)
	}

	headerSize := 8
	if protocol > protoVersion2 {
		headerSize = 9
	}

	srv := &TestServer{
		Address:    listen.Addr().String(),
		listen:     listen,
		t:          t,
		protocol:   protocol,
		headerSize: headerSize,
		quit:       make(chan struct{}),
	}

	go srv.serve()

	return srv
}
开发者ID:robmccoll,项目名称:gocql,代码行数:29,代码来源:conn_test.go

示例9: main

func main() {

	flag.Parse() // Scan the arguments list

	if *versionFlag {
		fmt.Println("Version:", APP_VERSION)
	}

	service := ":" + strconv.FormatInt(*serverPort, 10)
	tcpAddr, err := net.ResolveTCPAddr("ip4", service)
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		stop := false
		for !stop {
			buf := make([]byte, 100)
			n, err := conn.Read(buf[0:])
			if err != nil {
				stop = true
			}
			fmt.Fprintf(conn, "HTTP/1.0 200 OK\r\n\r\n")
			os.Stdout.Write(buf[0:n])
		}
		conn.Close()
	}
}
开发者ID:robot-head,项目名称:MixLister,代码行数:33,代码来源:mix_lister.go

示例10: ListenOnLocalRandomPort

// ListenOnLocalRandomPort returns a TCP listener on a random
// localhost port.
func ListenOnLocalRandomPort() (net.Listener, error) {
	ip, err := Localhost()
	if err != nil {
		return nil, err
	}
	return net.ListenTCP("tcp", &net.TCPAddr{IP: ip, Port: 0})
}
开发者ID:camlistore,项目名称:camlistore,代码行数:9,代码来源:netutil.go

示例11: init

func (s *Server) init() {
	if s.ReadBufferSize == 0 {
		s.ReadBufferSize = defaultReadBufferSize
	}
	if s.WriteBufferSize == 0 {
		s.WriteBufferSize = defaultWriteBufferSize
	}
	if s.OSReadBufferSize == 0 {
		s.OSReadBufferSize = defaultOSReadBufferSize
	}
	if s.OSWriteBufferSize == 0 {
		s.OSWriteBufferSize = defaultOSWriteBufferSize
	}

	listenAddr, err := net.ResolveTCPAddr("tcp", s.ListenAddr)
	if err != nil {
		log.Fatalf("Cannot resolve listenAddr=[%s]: [%s]", s.ListenAddr, err)
	}
	s.listenSocket, err = net.ListenTCP("tcp", listenAddr)
	if err != nil {
		log.Fatalf("Cannot listen for ListenAddr=[%s]: [%s]", listenAddr, err)
	}
	s.done = &sync.WaitGroup{}
	s.done.Add(1)
}
开发者ID:rjmcguire,项目名称:ybc,代码行数:25,代码来源:server.go

示例12: Start

func (server *tcpServer) Start() error {
	port := portStart
	for {
		tcpAddr, err := net.ResolveTCPAddr("tcp4", ":"+strconv.Itoa(port))
		if err != nil {
			return err
		}

		server.listener, err = net.ListenTCP("tcp", tcpAddr)
		if err == nil {
			break
		}

		if port > portEnd {
			return errors.New(fmt.Sprintf("Port number exceedes %v", portEnd))
		}

		port++
	}

	//fmt.Printf("PortNumber = %v\n", port)
	server.port = strconv.Itoa(port)

	go func() {
		for {
			acceptNext := server.acceptCycle()
			if !acceptNext {
				return
			}
		}
	}()

	return nil
}
开发者ID:na-lan,项目名称:seelog,代码行数:34,代码来源:internals_tcpserver_test.go

示例13: runMetadataService

func runMetadataService(cmd *cobra.Command, args []string) (exit int) {
	signal.Notify(exitCh, syscall.SIGINT, syscall.SIGTERM)
	stderr.Print("metadata service starting...")

	unixl, err := unixListener()
	if err != nil {
		stderr.Error(err)
		return 1
	}
	defer unixl.Close()

	tcpl, err := net.ListenTCP("tcp4", &net.TCPAddr{Port: flagListenPort})
	if err != nil {
		stderr.PrintE(fmt.Sprintf("error listening on port %v", flagListenPort), err)
		return 1
	}
	defer tcpl.Close()

	if err := initCrypto(); err != nil {
		stderr.Error(err)
		return 1
	}

	go runRegistrationServer(unixl)
	go runPublicServer(tcpl)

	stderr.Print("metadata service running...")

	<-exitCh

	stderr.Print("metadata service exiting...")

	return
}
开发者ID:hwinkel,项目名称:rkt,代码行数:34,代码来源:metadata_service.go

示例14: Serve

func (r *SrsServer) Serve() error {
	// too many open files will thows a panic.
	addr, err := net.ResolveTCPAddr("tcp", r.addr)
	if err != nil {
		glog.Errorf("resolve listen address failed, err=%v", err)
		return fmt.Errorf("resolve listen address failed, err=%v", err)
	}

	var listener *net.TCPListener
	listener, err = net.ListenTCP("tcp", addr)
	if err != nil {
		glog.Errorf("listen failed, err=%v", err)
		return fmt.Errorf("listen failed, err=%v", err)
	}
	defer listener.Close()
	for {
		glog.Info("listener ready to accept client")
		conn, err := listener.AcceptTCP()
		if err != nil {
			glog.Errorf("accept client failed, err=%v", err)
			return fmt.Errorf("accept client failed, err=%v", err)
		}
		glog.Info("TCP Connected")

		go r.serve(conn)
	}
}
开发者ID:Alienero,项目名称:IamServer,代码行数:27,代码来源:server.go

示例15: testPort

func testPort() string {
	add, _ := net.ResolveTCPAddr("tcp", ":0")
	l, _ := net.ListenTCP("tcp", add)
	defer l.Close()
	port := strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
	return port
}
开发者ID:kei-yamazaki,项目名称:loggregator,代码行数:7,代码来源:logcounter_test.go


注:本文中的net.ListenTCP函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。