[英]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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.