サーバーソケットとソケットクラスを使用して、Javaでtelnetのようなプログラムを書いています。これは、クライアント プログラムの現在のコードです。ユーザーは、接続したいサーバー名とポートを入力します。
static Socket getSocket()
{
while(true)
{
System.out.println("What server do you want to connect to on which port?");
String info = sc.nextLine();
String host = info.split(" ")[0];
int port = Integer.parseInt(info.split(" ")[1]);
try
{
InetAddress ip = InetAddress.getByName(host);
return new Socket(ip, port);
}
catch (UnknownHostException e)
{
System.err.println("The host is unknown.");
}
catch (IOException e)
{
System.err.println("Network error.");
}
}
}
ローカルホストに接続しようとしましたが、うまくいきました。次に、IPアドレスをネットワーク名として使用してリモートコンピューターで友人と接続しようとしましたが、例外が発生して機能しませんでした。リモートサーバーに接続するために使用する名前.