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()
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.
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()
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?
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()
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:
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?