본문 바로가기
IT/JAVA

Java 객체 직렬화

by 최고영회 2013. 12. 21.
728x90
반응형
SMALL
public void sendObjTest()
{
DatagramSocket socket = null;
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
// 변수 초기화 
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
// 값 setting
Properties prop = new Properties();
prop.put("certId", "yhkim");
prop.put("clientIp", "192.168.3.31");
// 객체 쓰기
oos.writeObject(prop);
oos.flush();
byte[] Buf= baos.toByteArray();
socket = new DatagramSocket();
InetAddress client = InetAddress.getByName("192.168.3.116");
DatagramPacket packet = new DatagramPacket(Buf, Buf.length, client, 1234);
socket.send(packet);    // 전송
System.out.println("DONE SENDING");
} catch(Exception e) {
e.printStackTrace();
}finally
{
if(socket != null) socket.close();
}
}

Properties 객체가 아닌 사용자 객체를 이용하려고 할때 유의점은 아래와 같다.
client 에서의 객체와 server 의 객체가 같은데도 classNotFoundException 이 발생할 경우 client와 server의 각 class의 
package 가 동일 해야 한다. 


728x90
반응형
LIST

'IT > JAVA' 카테고리의 다른 글

java unsigned int  (0) 2013.12.21
Java http, https URL 접근 및 response xml data 읽기  (0) 2013.12.21
Java ProcessBuilder, Runtime,, linux 에서 사용하기  (0) 2013.12.21
NIO 이용 파일 카피  (0) 2013.12.21
Java 로 mail 보내기  (0) 2013.12.21