-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
Milestone
Description
The udp server can receive and send back message with no errors, but the go version udp client can not receive the server side reply. I also write a client with python, and it works.
Here is the server side code with golang:
package main
import (
"fmt"
"net"
"os"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: " , err)
os.Exit(0)
}
}
func main() {
ServerAddr,err := net.ResolveUDPAddr("udp",":2345")
CheckError(err)
ServerConn, err := net.ListenUDP("udp", ServerAddr)
CheckError(err)
defer ServerConn.Close()
buf := make([]byte, 1024)
for {
n,addr,err := ServerConn.ReadFromUDP(buf)
fmt.Println("Received ",string(buf[0:n]), " from ",addr)
if err != nil {
fmt.Println("Error: ",err)
}
ServerConn.WriteToUDP([]byte("ack"), addr)
}
}
And the client side code:
package main
import (
"fmt"
"net"
"time"
"strconv"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: " , err)
}
}
func main() {
ServerAddr,err := net.ResolveUDPAddr("udp","255.255.255.255:2345")
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp", ":0")
CheckError(err)
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
CheckError(err)
defer Conn.Close()
i := 0
for {
msg := strconv.Itoa(i)
i++
buf := []byte(msg)
_,err := Conn.Write(buf)
if err != nil {
fmt.Println(msg, err)
}
time.Sleep(time.Second * 1)
buf = make([]byte, 1024)
n,addr,err := Conn.ReadFrom(buf)
fmt.Println("Received ",string(buf[0:n]), " from ",addr)
}
}
The python version client code:
#!/usr/bin/env python
import socket, traceback
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
for i in range(1,100):
try:
s.sendto("server here",('255.255.255.255', 2345))
message, address = s.recvfrom(1024)
print "Got data from", address,":",message
# Acknowledge it.
s.sendto("I am here", address)
except (KeyboardInterrupt, SystemExit):
raise
except:
traceback.print_exc()
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
Select code repository
Activity
[-]UDP broadcast client can not receive the answer message[/-][+]net: UDP broadcast client can not receive the answer message[/+]ianlancetaylor commentedon Nov 25, 2015
CC @mikioh
mdempsky commentedon Nov 25, 2015
Your Go and Python code don't match. Try using ListenUDP instead of DialUDP, and the WriteTo and ReadFrom methods instead of Write and Read.
iswarezwp commentedon Nov 25, 2015
@mdempsky You means I should use ListenUDP in the client code?
BTW, the go version server can work with the python version client.
iswarezwp commentedon Nov 25, 2015
Also, if the go version client do not use a broadcast IP, it works correctly.
mikioh commentedon Nov 25, 2015
This is not an issue of net package. See the following:
iswarezwp commentedon Nov 25, 2015
What is the correct way to write the client code with go?
mikioh commentedon Nov 25, 2015
Please ask that sort of question at https://github.com/golang/go/wiki#the-go-community.
iswarezwp commentedon Nov 25, 2015
@mikioh Thank you very much, just like @mdempsky pointed, I use ListenUDP at the client side and it works, but that still a little confused at the first time for me.