336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Client.java
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; class MyFrame extends JFrame { public static JTextArea textArea; JTextField textField; PrintWriter out; public MyFrame() { textArea = new JTextArea(); textField = new JTextField(); add(BorderLayout.CENTER, textArea); add(BorderLayout.SOUTH, textField); textField.addActionListener(new FieldListener()); setSize(300,200); setTitle("채팅프로그램 Client"); setVisible(true); // GUI를 설계한다. } class FieldListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub out.println(textField.getText()); textField.setText(""); // 메세지 보내기 } } } class SendThread extends Thread { PrintWriter out; public SendThread(PrintWriter out) { this.out = out; } @Override public void run() { // TODO Auto-generated method stub super.run(); } } class RecvThread extends Thread { BufferedReader in; public RecvThread(BufferedReader in) { this.in = in; } @Override public void run() { String temp; try { while ((temp = in.readLine()) != null) { MyFrame.textArea.setText( MyFrame.textArea.getText() + "\n" + temp); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class ClientModule { MyFrame mFrame; SendThread mSendThread; RecvThread mRecvThread; Socket s; // 서버에 패킷을 보내는 Thread // 서버로부터 패킷을 받는 Thread public ClientModule() // 준비 { try { s = new Socket("192.168.10.151", 33333); PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); mFrame = new MyFrame(); // GUI 객체 mFrame.out = out; mSendThread = new SendThread(out); mRecvThread = new RecvThread(in); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } void start() // 시작 { mSendThread.start(); mRecvThread.start(); } } public class Client { public static void main(String[] args) { ClientModule cm = new ClientModule(); // 초기화 작업 cm.start(); // Thread.start(); } }Server.java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; class PerClientThread extends Thread { Socket s; static ArrayListsocketArray = new ArrayList (); @Override public void run() { // TODO Auto-generated method stub socketArray.add(s); BufferedReader br; try { br = new BufferedReader(new InputStreamReader(s.getInputStream())); String temp; while ((temp = br.readLine()) != null) { System.out.print(s.getInetAddress() + " : "); System.out.println(temp); for( int i = 0 ; i < socketArray.size() ; i++) { PrintWriter os = new PrintWriter (socketArray.get(i).getOutputStream() ,true); os.print(s.getInetAddress() + " : "); os.println(temp); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } super.run(); } } public class Test { public static void main(String[] args) throws Exception { System.out.println("Server Side"); ServerSocket ss = new ServerSocket(33333); Socket s; while (true) { s = ss.accept(); PerClientThread pct = new PerClientThread(); pct.s = s; pct.start(); } } }
'Programming > JAVA,JSP' 카테고리의 다른 글
Java CardLayout Test (0) | 2016.01.26 |
---|---|
정올 알고리즘 2247 도서관 문제 (0) | 2015.11.26 |
[Spring] 한글 인코딩 설정 (0) | 2015.10.29 |
mysql table datetime tip (0) | 2014.10.20 |
JSP, MYSQL, ECLIPSE 연동하기 (0) | 2014.10.12 |