336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
package com.test.aa; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(5555); Socket s; while (true) { s = ss.accept(); System.out.println("입장 : " + s.getInetAddress()); PerClientThread pc = new PerClientThread(); pc.s = s; pc.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.test.aa; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; public class PerClientThread extends Thread { public Socket s; public static ArrayListalp = new ArrayList (); @Override public void run() { PrintWriter out = null; BufferedReader in = null; try { out = new PrintWriter(s.getOutputStream(), true); alp.add(out); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); for( int i = 0 ; i < alp.size() ; i++ ) { try{ alp.get(i).println(inputLine); } catch(Exception e) { } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
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 java.util.Scanner; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class Client { static String nickName; public static void main(String[] args) { JTextField textField; JTextArea textArea; PrintWriter out; BufferedReader in; try { Socket s = new Socket("118.46.60.67", 5555); out = new PrintWriter(s.getOutputStream(), true); in = new BufferedReader( new InputStreamReader(s.getInputStream())); Scanner sc = new Scanner(System.in); nickName = sc.next(); JFrame f = new JFrame("채팅"); f.setSize(600, 400); f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); textArea = new JTextArea(); textField = new JTextField(10); textField.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { out.println( nickName + " : " + textField.getText()); } } ); f.add(textField, BorderLayout.SOUTH); f.add(textArea, BorderLayout.CENTER); f.setVisible(true); while(true) { textArea.append(in.readLine()+"\n"); textField.setText(""); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
'Programming > JAVA,JSP' 카테고리의 다른 글
JDBC Realm (1) | 2016.03.09 |
---|---|
ImageFrameTest (0) | 2016.02.05 |
Java 채팅 소스 예제 #1 (0) | 2016.02.04 |
Java CardLayout Test (0) | 2016.01.26 |
정올 알고리즘 2247 도서관 문제 (0) | 2015.11.26 |