2
単純なクライアントとサーバーのペアが作成され、オブジェクトがサーバーに送信されています。私はコードをテストして、それが動作します、私はLOCALHOSTをサーバー名として使用します。JavaでのクライアントサーバーのIP構成がわからない
自分のIPアドレスを使用してサーバーに接続しようとすると、クライアントは継続的にタイムアウトします。誰かがコードを見てくれたらとても感謝しています。多くのおかげで、J.
クライアント
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Socket socket = null;
Person p = null;
try {
// My IP address entered here..
socket = new Socket("xx.xx.xxx.xxx", 3000);
// open I/O streams for objects
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
/*
// read an object from the server
p = (Person) ois.readObject();
System.out.print("Name is: " + p.getName());
oos.close();
ois.close();*/
//write object to the server
// p = new Person("HAL");
oos.writeObject(new Person("HAL"));
oos.flush();
ois.close();
oos.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
サーバー
public Server() throws Exception {
server = new ServerSocket(3000);
System.out.println("Server listening on port 3000.");
this.start();
}
こんにちはブライアン、応答のおかげで、私はプロセスを理解するのに役立ちました。ただし、上記のサーバーコンストラクターを使用すると、接続はまだタイムアウトしています。 InetAddressを自分のIPに設定しようとしても、コンパイル時にBind Exceptionを与えました。 – Jnanathan
ルータの後ろにある自宅のコンピュータでこれをやっていますか?上記のコードは私のマシン上で完璧に動作します。私が多分助けることができるスタックトレースを投稿することができれば。 –
はい。私は、私が取り組んでいるマシン上のファイアウォールをオフにしました。その奇妙な、私は最後の返信以来約12回試みた。それは一度だけ働いています。 – Jnanathan