I am programming a binary protocol. The demo with telnet and the max of 5 
messages was for testing this problem. 
I need to be able send even when not recieving data. The program now works, 
when de the client breaks the connection, there is no problem. 
When de server stops the connection it some times works, some times not and 
even chrashes.

Perry
On Wednesday, March 31, 2021 at 5:13:12 AM UTC+2 matt....@gmail.com wrote:

> 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 <perr...@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...@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/a070623a-ca11-4a05-b3d7-71896295ead9n%40googlegroups.com.

Reply via email to