Skip to content

net: UDP broadcast client can not receive the answer message #13391

@iswarezwp

Description

@iswarezwp

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()

Activity

changed the title [-]UDP broadcast client can not receive the answer message[/-] [+]net: UDP broadcast client can not receive the answer message[/+] on Nov 25, 2015
added this to the Unplanned milestone on Nov 25, 2015
ianlancetaylor

ianlancetaylor commented on Nov 25, 2015

@ianlancetaylor
Contributor
mdempsky

mdempsky commented on Nov 25, 2015

@mdempsky
Contributor

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

iswarezwp commented on Nov 25, 2015

@iswarezwp
Author

@mdempsky You means I should use ListenUDP in the client code?

BTW, the go version server can work with the python version client.

iswarezwp

iswarezwp commented on Nov 25, 2015

@iswarezwp
Author

Also, if the go version client do not use a broadcast IP, it works correctly.

mikioh

mikioh commented on Nov 25, 2015

@mikioh
Contributor

This is not an issue of net package. See the following:

iswarezwp

iswarezwp commented on Nov 25, 2015

@iswarezwp
Author

What is the correct way to write the client code with go?

mikioh

mikioh commented on Nov 25, 2015

@mikioh
Contributor

Please ask that sort of question at https://github.com/golang/go/wiki#the-go-community.

iswarezwp

iswarezwp commented on Nov 25, 2015

@iswarezwp
Author

@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.

locked and limited conversation to collaborators on Nov 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mdempsky@mikioh@iswarezwp@ianlancetaylor@gopherbot

        Issue actions

          net: UDP broadcast client can not receive the answer message · Issue #13391 · golang/go