java - 数据报包和套接字的未知错误 - Java Networking

标签 java networking

嘿,我一直在研究数据报类,但我就是不明白为什么我的“服务器”不会从“客户端”接收数据包。

我已经在我自己的 PC 上同时运行的服务器和客户端上对其进行了测试,并且它工作得很好,但是如果我尝试将服务器移至另一台 PC,它...不会。

现在我知道我一定是在套接字/地址/端口方面做错了什么......我以前没有使用过网络,所以我了解不多。

这是服务器代码:

    import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.UnknownHostException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.sql.Connection;
import java.util.ArrayList;

public class ServerThread extends Thread {

    private boolean needsToRun;
    private DatagramSocket socket;

    public ServerThread() {
        super();
        needsToRun = true;
        try {
            socket = new DatagramSocket(4446);
        }
        catch (SocketException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void run() {
        while(needsToRun) {
            byte[] buf = new byte[265];
            DatagramPacket packet = new DatagramPacket(buf,buf.length);
            try {
                socket.receive(packet);
            }
            catch(IOException e) {
                e.printStackTrace();
            }
            String data = new String(packet.getData(),0,packet.getLength());
            if(data != null)
                System.out.println(data);
        }
        socket.close();
    }
}

您会看到一些未使用的导入,但我只是精简了代码以使其变得基本(其中大部分来 self 实际上希望服务器在收到数据后对其进行的操作,但我已经使该部分正常工作)。

这是客户端代码:

    import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Main {

        public static void main(String[] args) throws SocketException, UnknownHostException, IOException {

            // get a datagram socket
        DatagramSocket socket = new DatagramSocket();

            // send request
        String testInfo = "Hi";
        byte[] buf = testInfo.getBytes();

        /*This could possible be a point at which the code won't work.
         *to get the bytes of the IP address of the computer I'm trying to run the server 
         *on I just did
         * byte[] address = InetAddress.getLocalHost().getAddress();
         * on the computer I was running the server on. I'm 95% sure that the IP addresses 
         * match.
         */
        byte[] inet = {-64,-88,1,5};
        InetAddress address = InetAddress.getByAddress(inet);
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4446);
        try {
            socket.send(packet);
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        socket.close();
    }
}

我已经检查过代码的明显部分,例如端口和地址(请参阅代码内注释)。

所以我真正想要的是,如果您不明白为什么代码无法工作,那么您能告诉我一些端口可能出现问题的事情以及诸如此类的事情(端口可能被关闭等(除非我也相当确定端口是开放的))。

感谢您的任何建议。

最佳答案

I've tested it with the server and client both running on my own PC at once and it works perfectly, but if I try to move the server over to another PC, it...doesnt.

我预计这是某种网络或防火墙问题,而不是 Java 客户端和服务器应用程序的问题。 (他们在同一台机器上运行时可以说话的证据表明了这一点......)

检查这些事情:

  • 检查客户端是否可以解析服务器的IP地址;例如尝试对其进行 ping 操作或在已知服务器支持的其他端口上连接到它。

  • 检查客户端和服务器计算机上的软件防火墙是否允许端口 4446 上的 UDP 流量。

  • 如果两台计算机之间有网桥和/或路由器,请检查它是否没有阻止端口 4446 上的 UDP 流量。

关于java - 数据报包和套接字的未知错误 - Java Networking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4181218/

相关文章:

networking - SOCKS5 绑定(bind)是持久的还是一次性的?

linux - 从 Linux 上的 docker 容器内部连接到主机需要打开防火墙端口

java - 如何从字符串中替换 "}"?

linux - 为什么增加网络缓冲区大小不能减少丢包?

linux - Polymer serve 拒绝外部连接

java - ClassLoader.getSystemResourceAsStream 和 getClass().getResourceAsStream() 的区别

c - 任何了解网络并将其用于编程的快速指南?

java - 线程 "main"java.util.NoSuchElementException 中出现异常?

java - 包可见性

java - 为 Spring 应用程序寻找持久计时器