Page 473 - 2020학년도 MDP과제발표회 자료집 (통신과) (3)
P. 473
/* *-----------* 내부 쓰레드 클래스 *-----------* */
public class sendDataThread extends Thread {
private SocketChannel sdt_SocketChannel;
private String data;
public sendDataThread(SocketChannel sc, String d){
sdt_SocketChannel = sc;
data = d;
}
@Override
public void run() {
try{
// 데이터 전송
sdt_SocketChannel.write(ByteBuffer.wrap(data.getBytes()));
} catch (Exception e1) {
}
}
}
public class readDataThread extends Thread {
@Override
public void run() {
try{
setSocket(IP, PORT);
} catch (IOException e){
e.printStackTrace();
}
// 소켓 생성 완료를 메인 UI 스레드에 알림.
m_handler.obtainMessage();
m_handler.sendEmptyMessage(SOCKET_CREATE_SUCCESS);
// 데이터 읽기 시작.
try {
while(true) {
// 셀렉터의 select() 메소드로 준비된 이벤트가 있는지 확인한다.
m_Selector.select();
// 셀렉터에 저장된 이벤트들(SelectionKey) 을 하나씩 처리한다.
Iterator it = m_Selector.selectedKeys().iterator();
인천전자마이스터고등학교
- 493 - 정보통신기기과 493