Javaでは、Sockets
と呼ばれるオブジェクトを使用して、ネットワーク経由で特定のIPおよびポートにデータを送信できます。そのデータは、ポートでリッスンしているServerSocket
によって受信されます。リモート接続を受け入れ、呼び出しが答えられるようになります
ソケットがサーバーソケットに接続すると、あなたがServerSocket#accept
を起動するまで、それはすべての発信コールをキューイングし、 (任意の呼び出しがキューに入れられる前に、あなたがaccept
を起動する場合、それは意志返される前にリクエストを待つ)。 accept
メソッドは、データを送受信するための入力ストリームと出力ストリームの両方を持つ通常のソケットオブジェクトを返します。
注:複数のソケットが同時に通話をキューに入れることができ、あなたが を呼び出すaccept()
たびに、サーバーが次保留中のソケットを受け入れます。 acceptメソッドは、Scanner
のnext()
メソッドのように多く動作します。 が混乱していますが、詳細はこちら ServerSocket listens without accept()です。
私は、クライアント・ソケットの出力ストリームから(accept
から入手した)サーバソケットの入力ストリームからObejctInputStream
を構築し、ObjectOutputStream
お勧めします。
と受信データを送信するために、両方のソケットが必要な場合は、単にサーバ側でObjectOutputStream
、およびクライアント側のObjectInputStream
を作成します。あなたは(あなたの割り当てはマルチスレッドなので、Runnable
が最良の選択肢である)の両方のストリームをリッスンするループ、またはRunnable
のいくつかのタイプを使用する必要があります。
典型的には、1つのこのと同様に、クライアントソケットに近づくだろう:あなたがソケットの使用を終了するときshutdown
メソッドを呼び出すことが重要である
public class ObjectSocket{
private Socket socket;
private ObjectOutputStream streamOut;
private ObjectInputStream streamIn;
public ObjectSocket(String IP, int port) throws UnknownHostException, IOException {
//Request connection.
this.socket = new Socket(InetAddress.getByName(IP), port);
}
public void sendObject(Object obj) throws IOException {
if(this.streamOut == null) this.streamOut = new ObjectOutputStream(this.socket.getOutputStream());
//Make sure the connection is still there and the socket is still open.
if(this.socket.isConnected() && !this.socket.isClosed())
//Send it!
this.streamOut.writeObject(obj);
}
public Object recieveObject() throws IOException, ClassNotFoundException {
if(this.streamIn == null) this.streamIn = new ObjectInputStream(this.socket.getInputStream());
//Cast appropriately.
return this.streamIn.readObject();
}
public void shutdown() throws IOException {
//End the connection
this.socket.close();
}
}
。ポートを開いたままにすることは危険です。
サーバ側のソケットは、ほぼ同じように近づいています
public class ServerObjectSocket {
private ServerSocket servSocket;
private Socket socket;
private ObjectOutputStream streamOut;
private ObjectInputStream streamIn;
public ServerObjectSocket(int port) throws IOException {
//Listen on this port : 0 will listen on a random port.
this.servSocket = new ServerSocket(port);
}
public void acceptConnectionRequest() throws IOException {
//Connect with any client socket that is trying this port
this.socket = servSocket.accept();
}
public Object recieveObject() throws IOException, ClassNotFoundException {
if(this.streamIn == null) this.streamIn = new ObjectInputStream(this.socket.getInputStream());
//Cast appropriately.
return this.streamIn.readObject();
}
public void sendObject(Object obj) throws IOException {
if(this.streamOut == null) this.streamOut = new ObjectOutputStream(this.socket.getOutputStream());
//Make sure the connection is still there and the socket is still open.
if(this.socket.isConnected() && !this.socket.isClosed())
//Send it!
this.streamOut.writeObject(obj);
}
public void shutdown() throws IOException {
//Close the port and end the connection : never leave a port open.
this.servSocket.close();
}
}
は、異なるネットワーク上のソケットとの間の接続をする場合、転送ポートで実行中のサーバソケットのみ可能で、覚えておいてください。 LAN上で実行されているソケットはこの問題に直面しませんが、ファイアウォールに問題がある可能性があります。
あなたがよりよく理解や例が必要な場合は、単にJavaのソケットのチュートリアルのためのインターネットを検索。ウェブの周りに浮かんでいる便利なチュートリアルがたくさんあります。見つけにくいものではありません。
どのようにアプリケーションを送信する予定ですか? – Stultuske
「オブジェクトとして、別のプログラムではない」とはどういう意味ですか? – EJP
@EJP質問タイトルで判断すると、私は彼がサーバー側でのみプログラムを実行したいと思うし、他の人はサーバー側のプログラムを使ってリモートコンピュータ上のクライアントソケットを開く。 (これは不可能です) –