디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

123

연비화갤로그로 이동합니다. 2017.03.27 13:53:46
조회 78 추천 0 댓글 0

package test; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import java.net.*; class MulticastChatting extends JFrame implements ActionListener, Runnable { private Container con; private FileInputStream fis = null; private FileOutputStream fos = null; // 받기위한 패킷, 소켓 private DatagramPacket receivePacket; private MulticastSocket receiveSocket; // 보내기 위한 패킷, 소캣 private DatagramPacket sendPacket; private MulticastSocket sendSocket; //파일 다이얼 로그 JFileChooser fd = new JFileChooser("./"); // 화면 구성 멤버 private JLabel idLb = new JLabel(" 아이디 ", JLabel.RIGHT); private JTextField idTf = new JTextField(15); private JTextArea showTa = new JTextArea(); private JScrollPane showJsp = new JScrollPane(showTa); private JLabel sendMsgLb = new JLabel(" 메제지 ", JLabel.RIGHT); private JTextField sendMsgTf = new JTextField(); private JButton sendFileBt = new JButton(" 파일전송 "); private JButton exitBt = new JButton(" 종료 "); // Border 선언 private Border btB = BorderFactory.createRaisedBevelBorder(); private Border tfB = new BevelBorder(BevelBorder.LOWERED); //new BevelBorder(BevelBorder.RAISED); // 배경색 private Color bgColor = new Color(255, 242, 242); // JLabel 글꼴 지정 private Font lbFont = new Font("새굴림", Font.BOLD, 16); private Color lbTextColor = new Color(172, 89, 255); //JButton 모양 설정 private Font btFont = new Font("HY엽서L", 0, 14); private Color btTextColor = new Color(225, 225, 225); private Color btBgColor = new Color(0, 0, 160); //텍스트 필드 설정 private Color tfBgColor = new Color(255, 255, 230); private Color tfTextColor = new Color(0, 0, 128); private Font tfFont = new Font("HY중고딕", 0, 12); public void initComponent() { idLb.setFont(lbFont); sendMsgLb.setFont(lbFont); idLb.setForeground(lbTextColor); sendMsgLb.setForeground(lbTextColor); idTf.setFont(tfFont); idTf.setForeground(tfTextColor); idTf.setBackground(tfBgColor); idTf.setBorder(tfB); sendMsgTf.setFont(tfFont); sendMsgTf.setForeground(tfTextColor); sendMsgTf.setBackground(tfBgColor); sendMsgTf.setBorder(tfB); showTa.setBackground(new Color(244, 255, 244)); showTa.setForeground(new Color(0, 0, 128)); sendFileBt.setFont(btFont); sendFileBt.setForeground(btTextColor); sendFileBt.setBackground(btBgColor); sendFileBt.setBorder(btB); exitBt.setFont(btFont); exitBt.setForeground(btTextColor); exitBt.setBackground(btBgColor); exitBt.setBorder(btB); } public MulticastChatting() { super("Multicast Sample Messenger v0.3"); init(); start(); setSize(300, 400); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d1 = tk.getScreenSize(); Dimension d2 = this.getSize(); this.setLocation((int)(d1.getWidth()/2-d2.getWidth()/2), (int)(d1.getHeight()/2-d2.getHeight()/2)); setVisible(true); } public void init() { con = this.getContentPane(); con.setLayout(new BorderLayout(5,5)); JPanel jPn1 = new JPanel(new BorderLayout(5,5)); jPn1.add("West", idLb); jPn1.add("Center", idTf); con.add("North", jPn1); con.add("Center", showJsp); JPanel jPn3 = new JPanel(new BorderLayout(5,5)); jPn3.add("West", sendMsgLb); jPn3.add("Center", sendMsgTf); JPanel jPn31 = new JPanel(new GridLayout(1,2)); jPn31.add(sendFileBt); jPn31.add(exitBt); jPn3.add("South", jPn31); con.add("South", jPn3); initComponent(); con.setBackground(bgColor); jPn1.setBackground(bgColor); jPn3.setBackground(bgColor); jPn31.setBackground(bgColor); showTa.setEnabled(false); idTf.requestFocus(); // down 디렉토리 생성 File f = new File("./down"); if(!f.exists()) { System.out.println("파일 생성 : " + f.mkdir()); } } public void start() { idTf.addActionListener( this ); sendMsgTf.addActionListener( this ); sendFileBt.addActionListener( this ); exitBt.addActionListener( this ); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void run() { showTa.setText("##### 대화방에 입장하셨습니다. #####\n"); try { receiveSocket = new MulticastSocket(20000); receiveSocket.joinGroup(InetAddress.getByName("239.2.3.4")); } catch (IOException ex) { } String fileName = null; while(true) { receivePacket = new DatagramPacket(new byte[1024], 1024); try{ receiveSocket.receive(receivePacket); } catch (IOException ex) { } byte[] String protocal = new String(data, 0, 6); if(protocal.equals("[MESG]")) { showTa.append((new String(data, 6, 1018).trim()) + "\n"); // 새로운 파일을 열음 } else if(protocal.equals("[SFNE]")) { try { fileName = new String(data, 6, 1018).trim(); if(fileName != null) fos = new FileOutputStream(".\\down\" + fileName); } catch(FileNotFoundException ex) { ex.printStackTrace(); } } else if(protocal.equals("[SFIL]")) { try{ fos.write(data, 8, (int)data[6] * 100 + data[7]); } catch (IOException ex) { ex.printStackTrace(); } if((int)data[6] * 100 + data[7] < 1016) { try { if(fos != null) fos.close(); showTa.append(fileName + " 파일을 전송 받았습니다.\n"); } catch (IOException ex) { ex.printStackTrace(); } } } showTa.setCaretPosition(showTa.getText().trim().length()); } } public void actionPerformed(ActionEvent ev) { if(ev.getSource() == idTf) { new Thread(this).start(); idTf.setEnabled(false); sendMsgTf.requestFocus(); } else if(ev.getSource() == sendMsgTf) { String str = sendMsgTf.getText().trim(); // 전송될 메세지 if(str == null || str.length() == 0) { sendMsgTf.setText(""); sendMsgTf.requestFocus(); return; } str = idTf.getText().trim() + " >> " + str; // 메세지 전송 프로토콜을 삽입 str = "[MESG]" + str; sendBytes(str.getBytes()); sendMsgTf.setText(""); sendMsgTf.requestFocus(); } else if(ev.getSource() == exitBt) { System.exit(0); } else if(ev.getSource() == sendFileBt) { fd.setDialogTitle("전송할 파일을 선택하세요!!"); fd.showOpenDialog(this); if(fd.getSelectedFile() == null || fd.getSelectedFile().getName().length() == 0) { showTa.append("전송할 파일을 선택해야 합니다.!!\n"); showTa.setCaretPosition(showTa.getText().trim().length()); return; } showTa.append(fd.getSelectedFile().getName() + "파일을 전송합니다.\n"); showTa.setCaretPosition(showTa.getText().trim().length()); // 파일 이름 전송(with protocal) String str = "[SFNE]" + fd.getSelectedFile().getName(); // 전송될 파일이름 sendBytes(str.getBytes()); // 파일 이름 전송 byte[] str // 파일 보내기 Protocal 이름 byte[] byte[1024]; for(int i=0; i<6; i++) data[i] = strData[i]; int testInt; try { fis = new FileInputStream(fd.getSelectedFile()); while(true) { testInt = fis.read(data, 8, 1024-8); if(testInt == -1) break; // data에 읽어온 데이타의 길이를 data[6], data[7]에 나눠서 넣음 data[6] = (byte)(testInt / 100); data[7] = (byte)(testInt % 100); sendBytes(data); } } catch (FileNotFoundException ex) { } catch (IOException ex) { } finally { try { if(fis != null) fis.close(); } catch (IOException ex) { } showTa.append("파일 전송 완료!!..\n"); } showTa.setCaretPosition(showTa.getText().trim().length()); } } // 바이트 배열을 전송 시킨다. public void sendBytes(byte[] data) { // 데이터 전송 // 1.데이타그램 패킷 생성 try { sendPacket = new DatagramPacket(data, data.length, InetAddress.getByName("239.2.3.4"), 20000); } catch (Exception ex) { } //2. 멀티캐스트 소캣 생성 try { sendSocket = new MulticastSocket(); sendSocket.setTimeToLive((byte)1); sendSocket.send(sendPacket); sendSocket.close(); } catch(IOException ex) { } } } public class MulticastChattingDemo { public static void main(String [] ar) { new MulticastChatting(); } }

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
등록순정렬 기준선택
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 어떤 상황이 닥쳐도 지갑 절대 안 열 것 같은 스타는? 운영자 24/05/20 - -
32854 어째서 너를 좋아하게 되어버린걸까 WINWIN갤로그로 이동합니다. 17.05.06 89 0
32853 작년 생각만 하면 눈물난다 WINWIN갤로그로 이동합니다. 17.05.05 90 0
32852 오랜만에 행복하다 WINWIN갤로그로 이동합니다. 17.05.03 87 0
32851 5월.... WINWIN갤로그로 이동합니다. 17.05.01 82 0
32850 죽고싶다 WINWIN갤로그로 이동합니다. 17.04.27 68 0
32849 ㅁㄴㅇ 연비화갤로그로 이동합니다. 17.04.27 57 0
32848 힘들다ㅜ WINWIN갤로그로 이동합니다. 17.04.24 66 0
32847 ㅁㄴㄹ 연비화갤로그로 이동합니다. 17.04.22 89 0
32846 ㅁㄴㅇ 연비화갤로그로 이동합니다. 17.04.22 64 0
32845 ㅂㅈㄷ 연비화갤로그로 이동합니다. 17.04.18 70 0
32843 존나 떨린당 WINWIN갤로그로 이동합니다. 17.04.16 70 0
32841 디데이 ㅇㅇ(211.177) 17.04.16 82 0
32839 ㅠㅠ WINWIN갤로그로 이동합니다. 17.04.14 84 0
32838 오늘 공부 안함 WINWIN갤로그로 이동합니다. 17.04.13 73 0
32837 되는 일은 없고 WINWIN갤로그로 이동합니다. 17.04.12 82 0
32836 꿈에 동스청 나오면 좋겠다 WINWIN갤로그로 이동합니다. 17.04.12 96 1
32835 이번 주 시간 진짜 안간다.. WINWIN갤로그로 이동합니다. 17.04.11 74 0
32834 맨날 머리 아프면 ㄹㅇ 문제 있는거겠지 WINWIN갤로그로 이동합니다. 17.04.10 80 0
32833 백무 ㄱㅇㅇ님 삼가고인의 명복을 빕니다 금복주갤로그로 이동합니다. 17.04.10 114 3
32832 커져가는 내 맘 어떻게 좀 해줘 WINWIN갤로그로 이동합니다. 17.04.09 74 0
32831 들을 노래 많이 생겻다 WINWIN갤로그로 이동합니다. 17.04.07 73 0
32830 해리 앨범 빨리 듣고싶다 ㅠ-ㅠ WINWIN갤로그로 이동합니다. 17.04.07 76 0
32829 동스청 어떻게 안좋아해 WINWIN갤로그로 이동합니다. 17.04.07 103 0
32828 모르겠다모르갯어요 WINWIN갤로그로 이동합니다. 17.04.06 62 0
32827 연비화갤로그로 이동합니다. 17.04.04 62 0
32826 정신 확 드네 WINWIN갤로그로 이동합니다. 17.04.04 70 0
32825 윈윈보고싶다 WINWIN갤로그로 이동합니다. 17.04.03 80 0
32824 태어나서 죄송합니다 WINWIN갤로그로 이동합니다. 17.04.02 61 0
32823 씨발ㅋㅋㅋ WINWIN갤로그로 이동합니다. 17.03.31 66 0
32822 지구멸망앙망 WINWIN갤로그로 이동합니다. 17.03.31 67 0
32821 아좆ㅗㅗㅗㅗㅗㅗㅗㅗㅗ WINWIN갤로그로 이동합니다. 17.03.31 79 0
32820 ㅂㅈㄷ 연비화갤로그로 이동합니다. 17.03.31 74 0
32819 ㅂㄷ 연비화갤로그로 이동합니다. 17.03.27 65 0
32818 ㅂㄷ 연비화갤로그로 이동합니다. 17.03.27 74 0
32817 ㅂㄷ 연비화갤로그로 이동합니다. 17.03.27 79 0
123 연비화갤로그로 이동합니다. 17.03.27 78 0
32815 죽고싶다 WINWIN갤로그로 이동합니다. 17.03.26 71 0
32814 ㅂㄱ 연비화갤로그로 이동합니다. 17.03.25 70 0
32813 백업 연비화갤로그로 이동합니다. 17.03.25 62 0
32811 나 왜 이러고 살지 WINWIN갤로그로 이동합니다. 17.03.24 76 0
32810 윈윈보고싶다고여 WINWIN갤로그로 이동합니다. 17.03.22 69 0
32809 연비화갤로그로 이동합니다. 17.03.21 58 0
32808 연비화갤로그로 이동합니다. 17.03.21 60 0
32807 연비화갤로그로 이동합니다. 17.03.21 67 0
32806 연비화갤로그로 이동합니다. 17.03.21 68 0
32804 윈윈아 고마워 WINWIN갤로그로 이동합니다. 17.03.18 82 0
32803 내 완벽한 이상형 WINWIN갤로그로 이동합니다. 17.03.18 77 0
32802 너 같은 사람 또 없어 WINWIN갤로그로 이동합니다. 17.03.18 73 0
32801 미쳤어 WINWIN갤로그로 이동합니다. 17.03.17 60 0
32800 트리거 더 피버 존나 좋당 ㅜ0ㅜ WINWIN갤로그로 이동합니다. 17.03.16 73 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2