用Golang编写的UDP客户端无法从服务器接收消息

I have written a Java Client, which sends a message to the broadcast address.

I have also written a Java Server, which accepts all sent messages and sends the message back to the client.

Now I wanted to try to do exactly the same in Go, just for gaining some experience. The server works fine and is receiving a message and responding to the Java client.

But my Go Client is only sending a message to the Go/Java server but does not receive any message back. According to wireshark the message is sent back to the right IP and Port, but apparently the port is unreachable.

My Code is as follows: Go Server:

package main

import (
    "fmt"
    "log"
    "net"
)

func main() {
    //Resolving address
    udpAddr, err := net.ResolveUDPAddr("udp4", "0.0.0.0:8888")

    if err != nil {
        log.Println("Error: ", err)
    }

    // Build listining connections
    conn, err := net.ListenUDP("udp", udpAddr)

    defer conn.Close()

    if err != nil {
        log.Println("Error: ", err)
    }

    // Interacting with one client at a time
    for {
        fmt.Println(">>>Ready to receive broadcast packets!")

        // Receiving a message
        recvBuff := make([]byte, 15000)
        _, rmAddr, err := conn.ReadFromUDP(recvBuff)

        if err != nil {
            panic(err)
        }

        fmt.Println(">>>Discovery packet received from: " + rmAddr.String())
        fmt.Println(">>>Packet received; data: " + string(recvBuff))

        // Sending the same message back to current client
        conn.WriteToUDP(recvBuff, rmAddr)

        fmt.Println(">>>Sent packet to: " + rmAddr.String())

} }

Go Client:

package main

import (
    "fmt"
    "log"
    "net"
    "os"
)

func main() {
    service := "158.129.239.255:8888"

    // Resolving Address
    RemoteAddr, err := net.ResolveUDPAddr("udp", service)

    // Make a connection
    conn, err := net.DialUDP("udp", nil, RemoteAddr)

    defer conn.Close()

    // Exit if some error occured
    if err != nil {
        log.Fatal(err)
        os.Exit(1)
    }

    // write a message to server
    message := []byte("message")

    _, err = conn.Write(message)
    fmt.Println(">>> Request packet sent to: 158.129.239.255 (DEFAULT)")

    if err != nil {
        log.Println(err)
    }

    // Receive response from server
    buf := make([]byte, 15000)
    amountByte, remAddr, err := conn.ReadFromUDP(buf)

    if err != nil {
        log.Println(err)
    } else {
        fmt.Println(amountByte, "bytes received from", remAddr)
    }

}

Java Client:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BroadcastUDPClient {

    public static void main(String[] args) {
        // Find the server using UDP broadcast
        try {
            //Open a random port to send the package
            DatagramSocket sendSD = new DatagramSocket();
            sendSD.setBroadcast(true);

                byte[] sendData = "message".getBytes();

            //Try the 255.255.255.255 first
            try {
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("158.129.239.255"), 8888);
                sendSD.send(sendPacket);
                System.out.println(">>> Request packet sent to: 158.129.239.255 (DEFAULT)");
            } catch (Exception e) {
            }

            //Wait for a response
            byte[] recvBuf = new byte[15000];
            DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
            sendSD.receive(receivePacket);

            //We have a response
            System.out.println(">>> Broadcast response from server: " + receivePacket.getAddress().getHostAddress());
            String message = new String(receivePacket.getData()).trim();
            System.out.println(">>> Message Body: " + message);

            //Close the port!
            sendSD.close();
        } catch (IOException ex) {
            Logger.getLogger(BroadcastUDPClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

What am I doing wrong with my Go Client?

du718589
du718589 另外:您不会处理其他几个错误。而log.Fatal仍然终止程序,os.Exit将永远不会到达。
4 年多之前 回复
doumu8217
doumu8217 不要忽略您从WriteToUDP得到的错误
4 年多之前 回复

2个回答



即使您只是要从连接发送UDP数据包,通常也要使用 ListenUDP </ code> 创建连接,并使用 ReadFromUDP </ code> 和 WriteToUDP </ code>方法。</ p>

使用 DialUDP </ code>时,它将创建一个“连接”的UDP套接字,该套接字具有隐式的远程端点,该端点 将过滤传入的数据包。 在Linux connect </ code>手册页中:</ p>


如果套接字sockfd的类型为SOCK_DGRAM,则addr是默认将数据报发送到的地址 ,以及唯一从中接收数据报的地址。</ p>
</ blockquote>
</ div>

展开原文

原文

Even if you're you're only going to be sending UDP packets from the connection, you generally want to use ListenUDP to create the connection, and use the ReadFromUDP and WriteToUDP methods.

When you use DialUDP, it creates a "connected" UDP socket, with an implicit remote endpoint, which will filter incoming packets. From the Linux connect man page:

If the socket sockfd is of type SOCK_DGRAM then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.

duanniesui6391
duanniesui6391 这是一个巨大的帮助。 为什么ReadFrom在DialUDP上不起作用,这肯定不是直观的。 特别是考虑到几乎每个UDP示例都使用Dial over Listen。
3 年多之前 回复
dousi0144
dousi0144 是的,没有;)。 Go仅公开了基础berkeley套接字API的可用功能。 DialUDP使用connect syscall,因此您可以从单个主机发送和接收,这是该标准API的一部分。 UDP仍然是“无连接”的,但这提供了只需要在与单个主机通信时调用recv并发送,而不必过滤掉杂散数据包的便利。
4 年多之前 回复
duanqinqiao4844
duanqinqiao4844 谢谢! 有道理,我只会从“已连接”连接中获取消息。 但是话又说回来,为什么Go建立连接? UDP是无连接的吗?
4 年多之前 回复



如果您不忽略从WriteToUDP返回的错误,它实际上会给您一个错误:“ sendto:message to long” </ p>

在OSX上,最大UDP数据报的大小默认设置为9216字节。 您尝试发送15000字节。 </ p>

如果只想写回您收到的内容,则写</ p>

  recvBuff [:n] 
</ code> </ pre>

,其中n是之前收到的字节数。</ p>
</ div>

展开原文

原文

If you don't ignore the error you get back from WriteToUDP it will actually give you an error: "sendto: message to long"

On OSX the the maximum UDP Datagram size is set default to 9216 bytes. You try to send 15000 bytes.

If you just want to write back what you received then write

recvBuff[:n]

, where n is the number of bytes received before.

dpb75177
dpb75177 另外:amountBytes不会更改recvBuff的len。 另外,您不应该将其命名为amountBytes,而仅应命名为n。
4 年多之前 回复
dqzd92796
dqzd92796 对我来说,它的工作原理。 您使用什么机器/操作系统和Go版本? 有防火墙吗? 尝试仅在localhost:8888上收听。
4 年多之前 回复
douyin8623
douyin8623 嗨,谢谢您对此进行调查。 我刚刚分配了WriteBit的结果不正确的amountBits。 amountBits设置为7,而err显然为nil。 我也将字节数减少到合理的数量(256),但是根据wireshark,Go客户端的端口仍然无法访问
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐