當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TCPConn.ReadFrom方法代碼示例

本文整理匯總了Golang中net.TCPConn.ReadFrom方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPConn.ReadFrom方法的具體用法?Golang TCPConn.ReadFrom怎麽用?Golang TCPConn.ReadFrom使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.TCPConn的用法示例。


在下文中一共展示了TCPConn.ReadFrom方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Serve

// Serve Gopher clients
func (s *Server) Serve(c *net.TCPConn) {
	defer c.Close()

	p, err := s.getPath(c)
	if err != nil {
		fmt.Fprint(c, s.ErrorListing("invalid request"))

		return
	}

	fn := s.filename(p)

	fi, err := os.Stat(fn)
	if err != nil {
		fmt.Fprint(c, s.ErrorListing("not found"))

		return
	}

	if fi.IsDir() {
		var list Listing

		filepath.Walk(fn, func(path string, info os.FileInfo, err error) error {
			if info.IsDir() {
				return list.VisitDir(info.Name(), path, s.Root, s.Host, s.Port)
			}

			list.VisitFile(info.Name(), path, s.Root, s.Host, s.Port)

			return nil
		})

		fmt.Fprint(c, list)

		return
	}

	f, err := os.Open(fn)
	if err != nil {
		fmt.Fprint(c, s.ErrorListing("couldn't open file"))
		return
	}
	defer f.Close()

	c.ReadFrom(f)
}
開發者ID:peterhellberg,項目名稱:gopher,代碼行數:47,代碼來源:server.go

示例2: send

func send(conn *net.TCPConn, file string) {
	fh, err := os.Open(file)
	if err != nil {
		log.Fatalln(err)
	}
	stat, _ := fh.Stat()
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, uint64(stat.Size()))
	_, err = conn.Write(b)
	if err != nil {
		log.Fatal(err)
	}
	r := bufio.NewReader(fh)
	n, err := conn.ReadFrom(r)
	if err != nil {
		log.Fatalln("send err ", err)
	}
	log.Println("file send size: ", n)
}
開發者ID:slene,項目名稱:gocase,代碼行數:19,代碼來源:copyfile.go

示例3: serveHTTPS

func serveHTTPS(conn *net.TCPConn, reader *bufio.Reader, codeline, headers, piece string) {
	this := func(target string) string {
		for i, rule := range regex {
			if rule.MatchString(target) {
				return proxy[i].Host
			}
		}
		return target
	}(piece)
	raddr, err := net.ResolveTCPAddr("tcp", this)
	for i := 0; i < 5 && err != nil; i++ {
		time.Sleep(time.Duration(i) * time.Second)
		raddr, err = net.ResolveTCPAddr("tcp", this)
	}
	if err != nil {
		log.Println("Parse Https:", codeline, piece, err)
		return
	}
	tcpconn, err := net.DialTCP("tcp", nil, raddr)
	if err != nil {
		// log.Println("Connect:", err)
		return
	}
	defer tcpconn.Close()
	if this == piece {
		conn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
	} else {
		tcpconn.Write([]byte(codeline + headers))
	}
	go func() {
		if _, err := tcpconn.ReadFrom(reader); err != nil {
			// log.Println("ReadFrom Left:", err)
			return
		}
	}()
	if _, err = conn.ReadFrom(tcpconn); err != nil {
		// log.Println("ReadFrom Right:", err)
		return
	}
}
開發者ID:hydra13142,項目名稱:examples,代碼行數:40,代碼來源:proxy.go

示例4: NewOpenFlowSwitch

/* Builds and populates Switch struct then starts listening
for OpenFlow messages on conn. */
func NewOpenFlowSwitch(conn *net.TCPConn) {

	if _, err := conn.ReadFrom(ofp10.NewHello()); err != nil {
		log.Println("ERROR::Switch.SendSync::ReadFrom:", err)
		conn.Close()
	}
	buf := make([]byte, 1500)
	n, _ := conn.Read(buf)
	res := ofp10.NewHello()
	res.Write(buf[:n])

	if _, err := conn.ReadFrom(ofp10.NewFeaturesRequest()); err != nil {
		log.Println("ERROR::Switch.SendSync::ReadFrom:", err)
		conn.Close()
	}
	buf2 := make([]byte, 1500)
	fres := ofp10.NewFeaturesReply()
	n, _ = conn.Read(buf2)
	fres.Write(buf2[:n])

	if sw, ok := Switches[fres.DPID.String()]; ok {
		log.Println("Recovered connection from:", sw.DPID)
		sw.conn = *conn
		go sw.SendSync()
		go sw.Receive()
	} else {
		log.Printf("Openflow 1.%d Connection: %s", res.Version-1, fres.DPID.String())
		s := new(Switch)
		s.conn = *conn
		s.DPID = fres.DPID
		s.Ports = make(map[int]ofp10.OfpPhyPort)
		s.requests = make(map[uint32]chan ofp10.OfpMsg)
		for _, p := range fres.Ports {
			s.Ports[int(p.PortNo)] = p
		}
		go s.SendSync()
		go s.Receive()
		Switches[s.DPID.String()] = s
	}
}
開發者ID:jessta,項目名稱:ogo,代碼行數:42,代碼來源:switch.go

示例5: Serve

func Serve(c *net.TCPConn) {
	defer c.Close()
	connbuf := bufio.NewReader(c)
	p, _, err := connbuf.ReadLine()
	if err != nil {
		fmt.Fprint(c, Error("invalid request"))
		return
	}
	filename := root + filepath.Clean("/"+string(p))
	fi, err := os.Stat(filename)
	if err != nil {
		fmt.Fprint(c, Error("not found"))
		return
	}
	if fi.IsDir() {
		var list Listing
		walkFn := func(path string, info os.FileInfo, err error) error {
			if info.IsDir() {
				return list.VisitDir(path, info)
			}

			list.VisitFile(path, info)
			return nil
		}

		PrintInlineText(c, header)

		filepath.Walk(filename, walkFn)
		fmt.Fprint(c, list)
		PrintInlineText(c, footer)
		return
	}
	f, err := os.Open(filename)
	if err != nil {
		fmt.Fprint(c, Error("couldn't open file"))
		return
	}
	c.ReadFrom(f)
}
開發者ID:Mic92,項目名稱:gogopherd,代碼行數:39,代碼來源:main.go

示例6: copy

func copy(dst, src *net.TCPConn) {
	dst.ReadFrom(src)
	src.Close()
	dst.Close()
}
開發者ID:octoblu,項目名稱:etcd-netfw,代碼行數:5,代碼來源:tcpproxy.go

示例7: Sendfile

// Sendfile sends count bytes from f to remote a TCP connection.
// f offset is always relative to the current offset.
func Sendfile(conn *net.TCPConn, f *os.File, count int64) (n int64, err error) {
	lr := &io.LimitedReader{N: count, R: f}
	n, err = conn.ReadFrom(lr)
	return
}
開發者ID:postfix,項目名稱:golib-1,代碼行數:7,代碼來源:sendfile.go


注:本文中的net.TCPConn.ReadFrom方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。