Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TCP套接字编程:无法连接到远程主机_Java_Sockets_Tcp - Fatal编程技术网

Java TCP套接字编程:无法连接到远程主机

Java TCP套接字编程:无法连接到远程主机,java,sockets,tcp,Java,Sockets,Tcp,我有一个java程序,它接受来自web浏览器的http请求,作为响应,程序发送一个文本文件内容以显示在web浏览器中。当我从安装在运行java代码的同一台机器上的浏览器发出请求时,该程序工作正常,但当我从与运行java代码的机器不在同一台机器上的其他web浏览器发出请求时,该程序没有收到任何请求 以下是我从web浏览器发出请求的方式:- http://localhost:port_number/ This is working fine... 这就是我如何从不在我的计算机上的其他web浏

我有一个java程序,它接受来自web浏览器的http请求,作为响应,程序发送一个文本文件内容以显示在web浏览器中。当我从安装在运行java代码的同一台机器上的浏览器发出请求时,该程序工作正常,但当我从与运行java代码的机器不在同一台机器上的其他web浏览器发出请求时,该程序没有收到任何请求

以下是我从web浏览器发出请求的方式:-

http://localhost:port_number/   
This is working fine...
这就是我如何从不在我的计算机上的其他web浏览器发出请求的方式:

http://my_ip_address:port_number/
This is not working...
这是我的java代码:-

while (true) {
        ServerSocket serverSocket = null;
        Socket clientSocket = null;
        try {
            serverSocket = new ServerSocket(32768);
            clientSocket = serverSocket.accept();
            InetAddress ia = clientSocket.getInetAddress();
            jTextArea1.append("Connected to : " + ia + "\n");
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            String inputLine, outputLine;
            String s = (String) JOptionPane.showInputDialog(this, "Enter File Name : ");
            File f = new File(s);
            if (f.exists()) {
                out.println("http/1.1 200 ok\r");
                out.println("Mime version 1.1");
                out.println("Content-Type: text/html\r");
                out.println("Content-Length: " + f.length() + "\r");
                out.println("\r");
                BufferedReader d = new BufferedReader(new FileReader(s));
                String line = " ", a;
                while ((a = d.readLine()) != null) {
                    line = line + a;
                }
                out.write(line);
                out.flush();
                jTextArea1.append("File Delivered.\n");
                d.close();
            }
            out.close();
            clientSocket.close();
            serverSocket.close();
        } catch (IOException e) {
            jTextArea1.append("Accept failed.");
            System.exit(1);
        }
    }
检查您是否确实在收听0.0.0.0:32768,而不是127.0.0.1:32768或任何其他特定IP,特别是如果您连接到多个网络。启动一个shell并在Windows上使用netstat-ano,在Unix或Mac上使用netstat-anp。 检查防火墙是否允许远程连接到端口32768
这与您编写的代码无关。您需要使您的IP地址可以公开访问。是一个相关线程。

这闻起来像是一个网络问题。。。防火墙,路由我不这么认为…我知道我在某个地方做错了什么,但不知道-在哪里尝试使用另一个客户端,如telnet:telnet 1.2.3.4:32768 80键入GET/+[enter]并告诉我们您遇到的错误。在调用后也放一个调试打印语句以接受。定义“不工作”。会发生什么?浏览器显示什么?注意:您没有发送有效的HTTP。HTTP标头中的行终止符是\r\n,而不仅仅是\r\n。MIME头上完全缺少一个行终止符以及一个冒号。新ServerSocket32768不侦听127.0.0.1或任何其他特定IP地址,它侦听0.0.0.0。这在Javadoc中没有提到。另外,如果您设置了自定义ServerSocketFactory,则不能保证新的ServerSocket32768将在0.0.0.01上侦听,尽管如此。2如果调用新的ServerSocket,则不涉及ServerSocketFactory。1°请使用官方文档支持2°ServerSocket。setSocketFactory将为整个应用程序设置默认工厂,而无需显式调用工厂1我不必使用官方文档支持它。这是您的断言,使用此代码它可以不是0.0.0.0:您支持它。你不能。没有。这是默认值。如果您想要的不是INADDR_ANY,请指定绑定地址。请看一下API。在你再次发帖之前试试看。2您提到了ServerSocketFactory。在javax.net中有一个与您现在提到的ServerSocket.setSocketFactory无关的函数,它不接受类型为ServerSocketFactory的参数,在本文中没有任何证据。