【问题标题】:Why DatagramSocket doesnt send in some pcs为什么 DatagramSocket 在某些电脑中不发送
【发布时间】:2020-08-08 14:03:36
【问题描述】:

我正在两个客户端之间使用 udp 套接字(DatagramSocket)开发一个网络游戏(服务器通知各自的 ip 和客户端建立连接)。问题是连接不适用于所有 PC。它适用于某些朋友,但不适用于其他朋友。无疑是发送问题。

Send hi 是一个重复发送排序消息以启动端口的功能(我发现要在 UDP 端口接收必须先发送),然后发送 ack 只是对“hi”的确认 (有一个进程调用receive,显然我在同一个端口发送和接收)

这是最重要的:

public class connection {
    protected DatagramSocket socketUDP;
    protected InetAddress address;
    protected int timeout = 50;
    protected int portSend;
    protected byte[] bufReceive = new byte[65535];
    protected byte[] bufSend;
    private boolean isUDP = true;

    public connection(String ip, int port, int timeout, boolean isUDP) {
        this.isUDP = isUDP;
        this.timeout = timeout;
        try {
            if(isUDP) {
                socketUDP = new DatagramSocket(port);
                this.portReceive = port;
                if (timeout > 0) {
                    socketUDP.setSoTimeout(timeout);
                }
                address = InetAddress.getByName(ip);
            }
            else{
                ///
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        this.rec = new receiver(this);
        this.rec.start();
    }

    private void send(int id, Object msg, boolean string){
        if(isUDP) {
            bufSend = (Integer.toString(id) + ";NR;" + (String)msg).getBytes();
            DatagramPacket packet = new DatagramPacket(bufSend, bufSend.length, address, portSend);
            try {
                socketUDP.send(packet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else {
            /////
        }
    }

    public void receive(){
        if(blockReception || socketUDP != null && !socketUDP.isConnected()){return;}
        try {
            if(isUDP) {
                String received = "";
                DatagramPacket packet
                        = new DatagramPacket(bufReceive, bufReceive.length);
                socketUDP.receive(packet);
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                received = new String(packet.getData(), 0, packet.getLength());
                if(received.equals("HI")){
                    sendAck(msgID.toClient.hi);
                    return;
                }
                System.out.println(received)
            }
            else{
                //////
            }

        }catch (Exception e){e.printStackTrace();}
    }
    
    public void setPortSend(int portSend) {
        this.portSend = portSend;
        if(isUDP) {
            socketUDP.connect(address, portSend);
            sendHi();
        }
    }
}

我希望有人能告诉我为什么它不适用于所有用户。

【问题讨论】:

  • 你有异常吗?如果没有,可能是防火墙丢弃了数据包
  • 没有抛出异常。我也考虑过防火墙,但是我所有的朋友都在 Windows 上启动了游戏,并且他们都弹出了请求网络权限的弹出窗口(他们接受了)。

标签: java sockets udp datagram


【解决方案1】:

大多数个人防火墙或防病毒软件都会阻止 UDP 入站流量。商业和教育组织通常会阻止 UDP,而不是用于某些常见服务。一些 ISP 也可能会阻止异常端口上的 UDP 流量。

【讨论】:

猜你喜欢
  • 2019-03-02
  • 1970-01-01
  • 2016-10-15
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多