FAQ
I'm not sure the SetReadDeadline workable on binary.Read? I mean is there
any chance that it will wait forever instead of my assigned read timeout "2
second"

full code: http://play.golang.org/p/t8qCa02oth

package main

import (
"encoding/binary"
"log"
"net"
"time"
)

type T struct {
H10 [10]byte
H20 [20]byte
}
func main() {
c, err := net.Dial("tcp", "www.google.com:80")
if err != nil {
log.Fatalln(err)
}
s := "GET /index.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
var t T
_, err = c.Write([]byte(s))
//err = binary.Write(c, binary.LittleEndian, s)
if err != nil {
log.Fatalln(err)
}

c.SetReadDeadline(time.Now().Add(2*time.Second)) // FIXME:
err = binary.Read(c, binary.LittleEndian, &t) // FIXME:
if err != nil {
log.Fatalln(err)
}
log.Println(t)
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at May 28, 2013 at 11:35 am
    Did you try it? Did it work?


    On 28/05/2013, at 21:26, dlin wrote:


    I'm not sure the SetReadDeadline workable on binary.Read? I mean is there any chance that it will wait forever instead of my assigned read timeout "2 second"

    full code: http://play.golang.org/p/t8qCa02oth

    package main

    import (
    "encoding/binary"
    "log"
    "net"
    "time"
    )

    type T struct {
    H10 [10]byte
    H20 [20]byte
    }
    func main() {
    c, err := net.Dial("tcp", "www.google.com:80")
    if err != nil {
    log.Fatalln(err)
    }
    s := "GET /index.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
    var t T
    _, err = c.Write([]byte(s))
    //err = binary.Write(c, binary.LittleEndian, s)
    if err != nil {
    log.Fatalln(err)
    }

    c.SetReadDeadline(time.Now().Add(2*time.Second)) // FIXME:
    err = binary.Read(c, binary.LittleEndian, &t) // FIXME:
    if err != nil {
    log.Fatalln(err)
    }
    log.Println(t)
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dlin at May 28, 2013 at 11:46 am
    It works roughly. But, I think it may not pass serious test by separate
    packet into pieces.

    I traced the source code.
    encoding/binary call io.ReadFull,
    io.ReadFull may call r.Read() multiple times.

    I'm wonder what's the implementation of SetReadDeadline().
    If it just work on first r.Read() call the deadline will failed.
    On Tuesday, May 28, 2013 7:35:08 PM UTC+8, Dave Cheney wrote:

    Did you try it? Did it work?



    On 28/05/2013, at 21:26, dlin <dli...@gmail.com <javascript:>> wrote:


    I'm not sure the SetReadDeadline workable on binary.Read? I mean is there
    any chance that it will wait forever instead of my assigned read timeout "2
    second"

    full code: http://play.golang.org/p/t8qCa02oth

    package main

    import (
    "encoding/binary"
    "log"
    "net"
    "time"
    )

    type T struct {
    H10 [10]byte
    H20 [20]byte
    }
    func main() {
    c, err := net.Dial("tcp", "www.google.com:80")
    if err != nil {
    log.Fatalln(err)
    }
    s := "GET /index.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
    var t T
    _, err = c.Write([]byte(s))
    //err = binary.Write(c, binary.LittleEndian, s)
    if err != nil {
    log.Fatalln(err)
    }

    c.SetReadDeadline(time.Now().Add(2*time.Second)) // FIXME:
    err = binary.Read(c, binary.LittleEndian, &t) // FIXME:
    if err != nil {
    log.Fatalln(err)
    }
    log.Println(t)
    }

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at May 28, 2013 at 11:49 am

    On Tue, May 28, 2013 at 7:45 PM, dlin wrote:

    I traced the source code.
    encoding/binary call io.ReadFull,
    io.ReadFull may call r.Read() multiple times.

    I'm wonder what's the implementation of SetReadDeadline().
    If it just work on first r.Read() call the deadline will failed.
    one SetReadDeadline will enforce deadline on all future read calls
    (before next SetReadDeadline, of course).

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 28, '13 at 11:26a
activeMay 28, '13 at 11:49a
posts4
users3
websitegolang.org

3 users in discussion

Dlin: 2 posts Dave Cheney: 1 post Minux: 1 post

People

Translate

site design / logo © 2022 Grokbase