简体   繁体   English

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

[英]Java TCP Socket Programming: Can not connect to remote host

I have a java program which accepts a http request from web browser and in response, program sends a text file contents to display in web browser. 我有一个Java程序,它接受来自Web浏览器的http请求,并且作为响应,程序发送文本文件内容以在Web浏览器中显示。 The program is working fine when I make request from browser which is installed on the same machine in which java code is running but when I make request from some other web browser which is not on the same machine as in which java code running, the program does not get any request. 当我从运行Java代码的同一台计算机上安装的浏览器发出请求时,该程序运行正常,但是当我从与运行Java代码的不同计算机上的其他Web浏览器发出请求时,该程序没有收到任何请求。

This is how I make request from my web browser:- 这是我从网络浏览器发出请求的方式:-

http://localhost:port_number/   
This is working fine...

This is how I make request from some other web browser which is not on my machine: 这是我从不在计算机上的其他Web浏览器发出请求的方式:

http://my_ip_address:port_number/
This is not working...

And this is my java code:- 这是我的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);
        }
    }

This is not related to the code that you've written. 这与您编写的代码无关。 You need to make your IP address publicly accessible. 您需要使您的IP地址可公开访问。 Here's is a related thread. 是一个相关的主题。

  1. Check that you are indeed listening on 0.0.0.0:32768 and not 127.0.0.1:32768 or any other particulat IP (specially if you are connected to several network). 检查您是否确实在监听0.0.0.0:32768,而不是127.0.0.1:32768或任何其他特定IP(特别是如果您连接到多个网络)。 Start a shell and use netstat -ano on Windows and netstat -anp on Unix or Mac. 启动一个shell,在Windows上使用netstat -ano,在Unix或Mac上使用netstat -anp。
  2. Check that your firewall allows remote connection to the port 32768 检查防火墙是否允许远程连接到端口32768

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

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