本文整理汇总了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)
}
示例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)
}
示例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
}
}
示例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
}
}
示例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)
}
示例6: copy
func copy(dst, src *net.TCPConn) {
dst.ReadFrom(src)
src.Close()
dst.Close()
}
示例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
}