オブジェクトを送信するための簡単コード 2007/01/23

オブジェクトを送信するための簡単コード

ソケットを使用してオブジェクトを送信したい。



protected Object send(String host, int port, Object object) {
ByteArrayOutputStream bas = null;
ObjectOutputStream bos = null;
Socket socket = null;
try {
socket = new Socket(host, port);
bos = new ObjectOutputStream(socket.getOutputStream());
bos.writeObject(object);
bos.flush();
ObjectInputStream ois = new ObjectInputStream(socket
.getInputStream());
return ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (bas != null) {
try {
bas.close();
} catch (IOException e) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return null;
}

: