i/o timeout for read on UDP connection - how to make several attempts to send/read?

134 Views Asked by At

I make a peer to peer application on Go and I can't catch an exception related to the timeout on read from the UDP connection.

I need the program to make maximum count attempts to send a message and receive a response, and if nothing works, then exit the loop and continue working.

However, I can't catch the i/o timeout exception - the program ends after the first timeout.

How can I fix it?


....
bufRes := make([]byte, DATAGRAM_SIZE)

for count > 0 {
        conn.SetReadDeadline(time.Now().Add(TIMEOUT)) // set Timeout
        count--

        // send message
        buf := composeHandChakeMessage(..some code...)

        _, err := conn.Write(buf)
        if err != nil {
            PanicMessage(err)
            continue
        }

        //recieve answer
        _, _, err = conn.ReadFromUDP(bufRes)
        if err != nil {
            if errors.Is(err, os.ErrDeadlineExceeded) {
                PanicMessage("The respondent is no longer responding\n")                
                continue
            } else {
                PanicMessage(err)               
                continue
            }
        }
        if err == nil {         
            break
        }
    }

....
2024/01/03 14:57:32 The respondent is no longer responding
goroutine 1 [running]:
log.Panic({0xc0000c5a20?, 0x27?, 0x708080?}) ...

the error is : read udp addr 1 -> addr 2: i/o timeout

0

There are 0 best solutions below