Javaで単純なエコーサーバーを作成しました。私はそれをローカルで試してみると、うまくいくはずです。しかし、サーバーが稼動している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の場合) –