자바로 간단한 에코 서버를 만들었습니다. 내가 로컬로 시도하면 정상적으로 작동합니다. 그러나 IP 주소와 서버가 실행중인 포트 번호를 사용하여 다른 컴퓨터에서 연결하려고하면 결코 연결되지 않습니다. 다른 컴퓨터의 서버에 연결하기 위해 수행해야 할 다른 작업이 있습니까?Java 서버를 실행중인 다른 컴퓨터에 연결하는 방법은 무엇입니까?
import java.net.Socket;
import java.net.ServerSocket;
public class EchoServer {
public static void main(String[] args) throws Exception {
// create socket
int port = 4444;
ServerSocket serverSocket = new ServerSocket(port);
System.err.println("Started server on port " + port);
// repeatedly wait for connections, and process
while (true) {
// a "blocking" call which waits until a connection is requested
Socket clientSocket = serverSocket.accept();
System.err.println("Accepted connection from client");
// open up IO streams
In in = new In (clientSocket);
Out out = new Out(clientSocket);
// waits for data and reads it in until connection dies
// readLine() blocks until the server receives a new line from client
String s;
while ((s = in.readLine()) != null) {
out.println(s);
}
// close IO streams, then socket
System.err.println("Closing connection with client");
out.close();
in.close();
clientSocket.close();
}
}
}
방화벽이 연결을 차단했을 수 있습니다. 짧은 테스트를 위해 해제하십시오. 당신은 라우터에 의해 다른 컴퓨터와 분리되어 있습니까? – Tudor
코드 추가 서버 소켓은 어떻게 만듭니 까? –
'netstat -na'를 호출하고 서버를 바인드 한 주소를 확인하십시오. 0.0.0.0:* (또는 ::: * ipv6의 경우) –