Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iswarezwp opened this issue Nov 25, 2015 · 8 comments

Comments

Projects
None yet
5 participants
@iswarezwp
Copy link

commented Nov 25, 2015

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

@ianlancetaylor ianlancetaylor changed the title UDP broadcast client can not receive the answer message net: UDP broadcast client can not receive the answer message Nov 25, 2015

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Nov 25, 2015

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

CC @mikioh

@mdempsky

This comment has been minimized.

Copy link
Member

commented 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

This comment has been minimized.

Copy link
Author

commented 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

This comment has been minimized.

Copy link
Author

commented Nov 25, 2015

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

@mikioh

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

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

@mikioh mikioh closed this Nov 25, 2015

@iswarezwp

This comment has been minimized.

Copy link
Author

commented Nov 25, 2015

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

@mikioh

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

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

@iswarezwp

This comment has been minimized.

Copy link
Author

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

@golang golang locked and limited conversation to collaborators Nov 27, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.