I am looking for a good solution for client / server communication with UDP sockets in Go.
The examples I found on the Internet show how to send data to the server, but they do not teach how to send it back to the client.
To demonstrate, my program does the following:
My client program creates a socket on a 4444 port, for example:
con, err := net.Dial("udp", "127.0.0.1:4444")
I sent the line and the local address to the server so that it can print the line and send an OK message. I use gob for this:
enc := gob.NewEncoder(con) enc.Encode(Data{"test", con.LocalAddr().String()})
The structure of my data is as follows:
type Data struct{ Msg string Addr string }
My server listens on port 4444 and decodes the Gob correctly, but how can I send an OK message? I use the client address for this (on the server .go file):
con, err := net.Dial("udp", data.Addr)
Then I get the error code:
write udp 127.0.0.1:35290: connection refused
When a client tries to connect to port 4444 of the server, the client creates a port with a random number (in this case 35290) so that they can communicate. I know that I should not pass the client address to the server, but conn.RemoteAddress () does not work. A solution that detects a customer address will be most appreciated.
Obl .: I know that there is ReadFromUDP, so I can read the package. Do I have to read it, open the client address and send the data to Gob so that it can decode it?