Why are you using channels for this? I see some issues in the code, but
without knowing what you're actually trying to accomplish, it's hard to
give advice.

To do exactly what you described, I would simply change
handleDeviceConnection() to look similar to this:

func handleDeviceConnection(c net.Conn) {
  defer c.Close()
  buf := make([]byte, 2048)
  for i := 0; i > 5; i++ {
    // handle a single message
    n, err := c.Read(buf)
    if err == io.EOF {
      break
    }
    fmt.Fprintf(c, "Message Count %d, Data %s", i+1, buf[:n])
}
}

Note that TCP is stream-oriented, so the number of bytes returned from Read
is not at all guaranteed to match the number of bytes the client sent. The
returned data can be part of one or more messages. You need some other
mechanism to divide the data received into messages. For example you might
want to read a line at a time instead.

On Tue, Mar 30, 2021 at 3:11 PM Perry Couprie <perryc...@gmail.com> wrote:

> Hi,
>
> I create a tcp server with goroutines.
>
> When the tcp client closes the connection it workes.
>
> After recieving 5 message from a telnet client it tries to close the
> connection.
> Some times it works, and sometimes not.
>
> I create a smal demo : https://pastebin.com/raw/BjZWzLFq
>
> This is my first try with channels.
> What am i doing wrong ?
>
> Greeting from Amsterdam Netherland,
> Perry
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/fedfcbaf-2e7d-496a-bb6a-a31eeafe4407n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/fedfcbaf-2e7d-496a-bb6a-a31eeafe4407n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALRNMm0-vYjR4V3ChAY20dKaD1YSyNvCwcvA6C%2Bna0%2BBz0yU_w%40mail.gmail.com.

Reply via email to