Help with gorotiune and udp protocol

hello everyone, i want to write a code that read from ip in udp protocol and write the data and the adress that recevie to a text file. But I can’t find the reason why it doesn’t work. can someone please advise me what the problem is and how to solve it?

this is my code:

package main

import (

"fmt"

"net"

"os"

)

func main() {

file_name := "test.txt"

number_of_data := 100

var buf chan []byte

var addr chan *net.UDPAddr

_, err := os.Create(file_name)

check_eror1(err)

file, err := os.Open(file_name)

check_eror1(err)

for i := 0; i < number_of_data; i++ {

    go readudp(addr, buf)

    //time.Sleep(3 * time.Second)

    v1, ok1 := <-addr

    check_eror2(ok1)

    v2, ok2 := <-buf

    check_eror2(ok2)

    _, err := fmt.Fprintln(file, string(i)+string(v1.IP)+string(v1.Port)+string(v2))

    fmt.Println(string(i) + string(v1.IP) + string(v1.Port) + string(v2))

    check_eror1(err)

    fmt.Println("print into the file")

}

file.Close()

}

func readudp(addr chan *net.UDPAddr, buf chan []byte) {

buf1 := make([]byte, 1500)

conn, errlisten := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: 5000, Zone: ""})

check_eror1(errlisten)

n, addr1, erread := conn.ReadFromUDP(buf1)

check_eror1(erread)

fmt.Println("test")

addr <- addr1

buf <- buf1[:n]

close(addr)

close(buf)

}

// write to file

func check_eror1(eror error) {

if eror != nil {

    fmt.Println("ERROR")

}

}

func check_eror2(eror bool) {

if eror != true {

    fmt.Println("ERROR")

}

}

What exactly does not work? Instead of printing a generic and static ERROR, you should instead print the actual error and make sure to stop right there.

Also, personally I don’t like doing the error check in a helper function. You neither can properly return the error, nor you can properly panic, as the stack trace is obfuscated through the additional check call.

hi i to that, but the debug console write the problem and i dont understand what does that mean:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x4bd24e]

goroutine 19 [running]:
main.readudp(0x0, 0x0)
c:/avishai/opp/main.go:46 +0x10e
created by main.main
c:/avishai/opp/main.go:23 +0x15b
exit status 2
Process exiting with code: 1

this is the new code i wrote:

package main

import (

"fmt"

"net"

"os"

)

func main() {

file_name := "test.txt"

number_of_data := 100

var buf chan []byte

var addr chan *net.UDPAddr

_, err := os.Create(file_name)

if err != nil {

    fmt.Println("eror in create file")

}

file, err := os.Open(file_name)

if err != nil {

    fmt.Println("eror in open file")

}

for i := 0; i < number_of_data; i++ {

    go readudp(addr, buf)

    //time.Sleep(3 * time.Second)

    v1, ok1 := <-addr

    if ok1 != true {

        fmt.Println("error to get the adress to channel v1")

    }

    v2, ok2 := <-buf

    if ok2 != true {

        fmt.Println("error to get the data to channel v2")

    }

    _, err := fmt.Fprintln(file, string(i)+string(v1.IP)+string(v1.Port)+string(v2))

    fmt.Println(string(i) + string(v1.IP) + string(v1.Port) + string(v2))

    if err != nil {

        fmt.Println("eror write to the file")

    }

    fmt.Println("print into the file")

}

file.Close()

}

func readudp(addr chan *net.UDPAddr, buf chan []byte) {

buf1 := make([]byte, 1500)

conn, errlisten := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: 5000, Zone: ""})

defer conn.Close()

if errlisten != nil {

    fmt.Println("eror in listen to adress in udp protocol")

}

n, addr1, erread := conn.ReadFromUDP(buf1)

if erread != nil {

    fmt.Println("eror in read from udp protocol")

}   

//fmt.Println("test")

addr <- addr1

buf <- buf1[:n]

/*  close(addr)

    close(buf) 

*/

}

// write to file

/*func check_eror1(eror error) {

if eror != nil {

    fmt.Println("ERROR")

}

}

func check_eror2(eror bool) {

if eror != true {

    fmt.Println("ERROR")

}

}

*/

Blockquote

I can compile and run your program, it does create the file and listens on port 5000. I can send UDP data to that port.

No crash.

Please provide more information and example data to send over the wire.

Also please use proper code formatting. Either use markdown directly or mark your code and press </> button above texteditbox.

This tells me that the two parameters to main.readudp are nil. Looking at your sample code, I don’t see buf and addr being initialized. Can you check that?

i was check that, its initialized in the goroutine “func readudp” i was wrote that:

n, addr1, erread := conn.ReadFromUDP(buf1)

if erread != nil {

    fmt.Println("eror in read from udp protocol")

}

addr <- addr1

buf <- buf1[:n]

You are right sorry for the disorganization, here is the neatly written code, for me it crashes and I don’t understand why
this the code:

func main() {

file_name := "test.txt"

number_of_data := 100

var buf chan []byte

var addr chan *net.UDPAddr

_, err := os.Create(file_name)

if err != nil {

    fmt.Println("eror in create file")

}

file, err := os.Open(file_name)

if err != nil {

    fmt.Println("eror in open file")

}

for i := 0; i < number_of_data; i++ {

    go readudp(addr, buf)

    v1, ok1 := <-addr

    if ok1 != true {

        fmt.Println("error to get the adress to channel v1")

    }

    v2, ok2 := <-buf

    if ok2 != true {

        fmt.Println("error to get the data to channel v2")

    }

    _, err := fmt.Fprintln(file, string(i)+string(v1.IP)+string(v1.Port)+string(v2))

    fmt.Println(string(i) + string(v1.IP) + string(v1.Port) + string(v2))

    if err != nil {

        fmt.Println("eror write to the file")

    }

    fmt.Println("print into the file")

}

file.Close()

}

func readudp(addr chan *net.UDPAddr, buf chan []byte) {

buf1 := make([]byte, 1500)

conn, errlisten := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: 5000, Zone: ""})

defer conn.Close()

if errlisten != nil {

    fmt.Println("eror in listen to adress in udp protocol")

}

n, addr1, erread := conn.ReadFromUDP(buf1)

if erread != nil {

    fmt.Println("eror in read from udp protocol")

}

addr <- addr1

buf <- buf1[:n]

}

Please use a codeblock, rather than a quoteblock.

Also please tell us the exact error you see, the full output your program creates.

I am not on a computer anymore and won’t have one in reach before the kids are in bed (~3h).

my program create a file, but its still empty, i also use packet sender to send data in udp protocol to the address that i wrote in my code, the error i see in the debug console, when i run in vs code platform the code:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x4bd2b4]

goroutine 19 [running]:
main.readudp(0x0, 0x0)
c:/Users/lab3/Desktop/go_test/main.go:45 +0x114
created by main.main
c:/Users/lab3/Desktop/go_test/main.go:23 +0x15b
exit status 2
Process exiting with code: 1

this is the code:

    func main() {

            fileName := "test.txt"

            numberOfData := 2

            var buf chan []byte

            var addr chan *net.UDPAddr

            _, err := os.Create(fileName) // create file

            if err != nil {

                fmt.Println("eror in create file")

            }

            file, err := os.Open(fileName) // open file

            if err != nil {

                fmt.Println("eror in open file")

            }

            for i := 0; i < numberOfData; i++ { // create go routine

                go readudp(addr, buf) //and write the data that recive from go rouitne to  thefile

                v1, ok1 := <-addr

                if ok1 != true {

                    fmt.Println("error to get the adress to channel v1")

                }

                v2, ok2 := <-buf

                if ok2 != true {

                    fmt.Println("error to get the data to channel v2")

                }

                _, err := fmt.Fprintln(file, string(i)+string(v1.IP)+string(v1.Port)+string(v2))

                fmt.Println(string(i) + string(v1.IP) + string(v1.Port) + string(v2))

                if err != nil {

                    fmt.Println("eror write to the file")

                }

                fmt.Println("print into the file")

            }

            file.Close()

        }

        func readudp(addr chan *net.UDPAddr, buf chan []byte) { // goroutine that listen to udp and read from udp

            buf1 := make([]byte, 1500)

            conn, errlisten := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{127, 0, 0, 1}, Port: 5000, Zone: ""})

            defer conn.Close()

            if errlisten != nil {

                fmt.Println("eror in listen to adress in udp protocol")

            }

            n, addr1, erread := conn.ReadFromUDP(buf1)

            if erread != nil {

                fmt.Println("eror in read from udp protocol")

            }

            addr <- addr1

            buf <- buf1[:n]

        }

I doubt that the panic is the only output, I’m pretty sure there is one of your fmt.Printlned errors just before that.

PS: For some reason, there is a lot of empty lines in your code, that makes it hard to map the line numbers from the panic to your code. Which is line 45?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.