简体   繁体   English

为什么 DatagramSocket 在某些电脑中不发送

[英]Why DatagramSocket doesnt send in some pcs

I'm developing a net game with udp socket (DatagramSocket) between two clients (the server notifies both the respective ip and the clients establish the connection).我正在两个客户端之间使用 udp 套接字(DatagramSocket)开发一个网络游戏(服务器通知各自的 ip 和客户端建立连接)。 The problem is that the connection isn't working on all PCs.问题是连接不适用于所有 PC。 It works for some friends, but doesn't work for others.它适用于某些朋友,但不适用于其他朋友。 Denifitely the problem is sending.毫无疑问,问题出在发送上。

Send hi is a function to send a sort message repeatedly to start the port (I discovered that to receive in UDP port it's necessary to send first), and send ack it just a confirmation to the "hi" (There is a process calling receive, and obviously i send and receive in the same port)发送 hi 是一个 function 重复发送排序消息以启动端口(我发现要在 UDP 端口接收必须先发送),然后发送 ack 它只是对“hi”的确认(有一个过程调用接收,显然我在同一个端口发送和接收)

This would be the most important:这将是最重要的:

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

I hope someone can say me why it isn't working for all users.我希望有人能告诉我为什么它不适用于所有用户。

Most personal firewalls or antiviruses are blocking UDP inbound traffic.大多数个人防火墙或防病毒软件都会阻止 UDP 入站流量。 Business and education organizations are often blocking UDP other than for some common services.商业和教育组织通常会阻止 UDP,而不是一些常见的服务。 Some ISP s may also be blocking UDP traffic on unusual ports.一些 ISP 也可能在异常端口上阻止 UDP 流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM