반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; class Ting extends JFrame implements ActionListener,Runnable{ DataInputStream dis; DataOutputStream dos; String id; Socket s; JButton b1, b2; JTextField tf1, tf2; JTextArea ta; JList jl; JLabel la1, la2, la3; Ting(){ super("Ting"); b1 = new JButton("들어가기"); b1.addActionListener(this); b2 = new JButton("나가기"); b2.setEnabled(false); tf1 = new JTextField(10); tf2 = new JTextField(15); tf2.addKeyListener(new send()); ta = new JTextArea(7,20); String[] data = {"홍보", "마케팅","IT"}; jl=new JList(data); la1 = new JLabel("아이디"); la2 = new JLabel("대화상대"); la3 = new JLabel("대화내용"); JPanel p1=new JPanel(); p1.setLayout(new FlowLayout()); p1.add(la1); p1.add(tf1); p1.add(b1); p1.add(b2); JPanel pp=new JPanel(); pp.setLayout(new BorderLayout()); pp.add("North",la2); pp.add("South",jl); JPanel p2=new JPanel(); p2.setLayout(new FlowLayout()); p2.add(new JScrollPane(ta)); p2.add(pp); JPanel p3=new JPanel(); p3.setLayout(new FlowLayout()); p3.add(la3); p3.add(tf2); this.setLayout(new BorderLayout()); this.add("North",p1); this.add("Center",p2); this.add("South",p3); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); this.setVisible(true); } class send implements KeyListener{ public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER){ String sm=tf2.getText(); try { dos.writeUTF(sm); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } tf2.setText(""); }//if }//pressed public void keyReleased(KeyEvent arg0) {} public void keyTyped(KeyEvent arg0) {} }//send public static void main(String[] args) { new Ting(); } @Override public void actionPerformed(ActionEvent e) { try{ // 접속하고자하는 서버의 ip & port 입력 s=new Socket("192.168.0.3",1111); dis=new DataInputStream(s.getInputStream()); dos=new DataOutputStream(s.getOutputStream()); id=tf1.getText(); dos.writeUTF(id); Thread t=new Thread(this); t.start(); }catch(Exception e1){ e1.printStackTrace(); } }//action @Override public void run() { while(true){ try { String mg=dis.readUTF(); ta.append(mg+"\n"); ta.setCaretPosition(ta.getText().length()); } catch (IOException e) { e.printStackTrace(); } }//while } //run }//Ting | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import java.io.*; import java.net.*; import java.util.*; public class Server extends Thread { DataInputStream dis; DataOutputStream dos; String ip; static Vector vs = new Vector(); static Vector ids = new Vector(); String id; Server(Socket c) throws Exception{ dis = new DataInputStream(c.getInputStream()); dos = new DataOutputStream(c.getOutputStream()); ip=c.getInetAddress().getHostAddress(); id = dis.readUTF(); ids.add(id); vs.add(this); // this - socket c call("**"+id+"입장"); } public void call(String mg) throws Exception{ for(int i = 0; i < vs.size(); i++) { Server t = (Server)vs.get(i); t.dos.writeUTF(mg); } }//call public void run(){ while(true){ try { String mg = dis.readUTF(); System.out.println(ip+":"+mg); call(id+":"+mg); } catch (Exception e){ e.printStackTrace(); } }//true }//run public static void main(String[] args) { System.out.println("server start"); try{ // 포트 1889 LISTENING ServerSocket s = new ServerSocket(1889); while(true){ Socket client = s.accept(); Server sv = new Server(client); sv.start(); }//while }catch(Exception e){ System.out.println("error"); } } } | cs |
반응형
'프로그래밍 > 시스템' 카테고리의 다른 글
[C-알고리즘] 버블정렬,선택정렬,삽입정렬 (0) | 2017.06.25 |
---|---|
[C-알고리즘] 유클리드 호제법 (0) | 2017.06.16 |
[java] 입출력스트림 - 파일쓰기 (0) | 2017.06.07 |
[C] #define for문 (0) | 2017.06.06 |
java 예외처리종류 & 사용방법 (2) | 2017.06.05 |