Java 我无法通过另一台计算机连接到我的服务器
我正在写一个程序,最终使我自己的文件服务器。在服务器运行时,通过在浏览器中键入Java 我无法通过另一台计算机连接到我的服务器,java,serversocket,Java,Serversocket,我正在写一个程序,最终使我自己的文件服务器。在服务器运行时,通过在浏览器中键入myIpAddress:port连接到web浏览器中的服务器时出现问题。如果我使用一台计算机同时作为服务器主机和客户机,这是可行的,但如果我使用另一台计算机作为客户机,则不可行 我有两门课,Index和ClientHandler public class Index { public static void main(String[] args) { ServerSocket server = null;
myIpAddress:port
连接到web浏览器中的服务器时出现问题。如果我使用一台计算机同时作为服务器主机和客户机,这是可行的,但如果我使用另一台计算机作为客户机,则不可行
我有两门课,Index
和ClientHandler
public class Index {
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(8080);
DevInterface myFace = new DevInterface(server);
System.out.println("Listening for connection on port 8080");
} catch (IOException e) {
e.printStackTrace();
}
int i = 0;
while(true) {
Socket client = null;
try {
client = server.accept();
ClientHandler clientHandler = new ClientHandler(client);
clientHandler.start();
System.out.println("got a client");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我的ClientHandler类将html发送到服务器
public class ClientHandler extends Thread{
private Socket client = null;
private PrintWriter out = null;
ArrayList<Socket> clientList = null;
public ClientHandler(Socket client) {
this.client = client;
}
public ClientHandler(Socket client, ArrayList<Socket> clientList) {
this.client = client;
this.clientList = clientList;
}
public void run() {
try {
out = new PrintWriter(client.getOutputStream());
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("\r\n");
out.println("<p>Hello World</p>");
out.flush();
out.close();
if(client!=null) {client.close();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public类ClientHandler扩展线程{
私有套接字客户端=null;
private PrintWriter out=null;
ArrayList clientList=null;
公共ClientHandler(套接字客户端){
this.client=client;
}
公共ClientHandler(套接字客户端、ArrayList客户端列表){
this.client=client;
this.clientList=clientList;
}
公开募捐{
试一试{
out=新的PrintWriter(client.getOutputStream());
out.println(“HTTP/1.1200ok”);
println(“内容类型:text/html”);
out.println(“\r\n”);
out.println(“你好世界””;
out.flush();
out.close();
如果(client!=null){client.close();}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
你在服务器上运行防火墙吗?我对ServerSocket和服务器很陌生,一般来说,我不相信我有防火墙在运行,但是我也不知道如何检查。这两台机器在同一个网络上吗?你能从客户端ping
服务器吗?您没有使用127.0.0.1:port
,是吗?他们在同一个网络上,我没有使用确切的127.0.0.1
地址,但我使用了我的IP地址。我搜索了您的防火墙以及如何允许访问特定端口。您可以使用公共服务名称而不是端口号,但您始终可以允许特定端口通过。您将需要管理员权限。