자바 채팅창 구현한것니다. 고수님들 보시고 좀 도와주세요
딥레드
안녕하세요 현재 자바의 세상에 빠져들고 있는 한학생입니다
다름이 아니오라..제가 자바채팅창을 구현했는데..
귓속말 기능및 접속창 기능을 추가할려는데 생각보다 너무 어렵네요
제가 보고나면 이해가 좀 빠를것 같아서 고수님들게 귀찮지만
이렇게 귓속말 구현좀 부탁드리고 자합니다.
고수님들 보시고 도와 주시기 바랍니다.
-------------------------Server.java-----------------------
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
public final static int PORT = 7979;
Vector clients;
Manager manager;
Listener listener;
public Server() throws Exception {
clients = new Vector();
manager = new Manager();
manager.start();
System.out.println(Manager Start);
listener = new Listener();
listener.start();
System.out.println(Listener Start:port = + PORT);
System.out.println(Service Start);
}
public static void main(String[] args) throws Exception {
Server server = new Server();
}
// 신규접속자
class Listener extends Thread {
ServerSocket ss;
public Listener() throws Exception {
ss = new ServerSocket(Server.PORT);
}
public void run() {
for(;;) {
try {
Socket con = ss.accept();
manager.addConnection(con);
} catch(Exception e) {;}
}
}
} // end of Listener class
//접속자 관리
class Manager extends Thread {
public void addConnection(Socket con) throws Exception {
Client client = new Client(con);
client.start();
clients.addElement(client);
}
public void run() {
for(;;) {
try {
for(int i = 0; i clients.size(); i++) {
Thread client = (Thread)clients.elementAt(i);
if(!client.isAlive()) {
clients.remove(i);
break;
}
}
Thread.sleep(500);
} catch(Exception e) {;}
}
}
}
class Client extends Thread {
BufferedReader in;
PrintStream out;
Client(Socket con) throws Exception {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
out = new PrintStream(con.getOutputStream());
}
public void run() {
try {
for(;;) {
String str = in.readLine();
broadcasting(str);
}
} catch(Exception e) {
try {
in.close();
out.close();
} catch (Exception ex) {;}
}
}
public void broadcasting(String str) throws Exception {
for(int i = 0; i clients.size(); i++) {
Client client = (Client)clients.elementAt(i);
client.out.println(str);
client.out.flush();
}
}
}
} // end of Server class
-------------------ChatClient.java-------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ChatClient extends JFrame
implements Runnable, ActionListener, WindowListener {
private JTextArea talkArea; // 대화 내용이 나타나는 부분
private JTextField talkField; // 대화를 적는 부분
private final static String SERVER_ADDR = 127.0.0.1;
private final static int SERVER_PORT = 7979;
Socket s;
BufferedReader in;
PrintStream out;
public ChatClient() throws Exception {
super(Chat Client Test);
addWindowListener(this);
//생성
talkArea = new JTextArea();
talkField = new JTextField();
JButton sendBtn = new JButton(보내기);
//설정
talkArea.setEditable(false);
talkField.addActionListener(this);
sendBtn.addActionListener(this);
// 배치
JRootPane rootPane = getRootPane();
rootPane.setLayout(new BorderLayout());
rootPane.add(BorderLayout.CENTER, talkArea);
JPanel btmPane = new JPanel(new BorderLayout());
btmPane.add(BorderLayout.CENTER, talkField);
btmPane.add(BorderLayout.EAST, sendBtn);
rootPane.add(BorderLayout.SOUTH, btmPane);
// 연결
s = new Socket(SERVER_ADDR, SERVER_PORT);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintStream(s.getOutputStream());
Thread runner = new Thread(this);
runner.start();
}
public static void main(String[] args) throws Exception {
ChatClient client = new ChatClient();
client.pack();
client.setVisible(true);
}
// Runnable 구현부
public void run() {
try {
for(;;) {
String talk = in.readLine();
talkArea.append(talk + \n);
}
} catch(Exception e) {
}
}
// Action 이벤트 구현부
public void actionPerformed(ActionEvent e) {
String talk = talkField.getText();
if(talk.trim().length() == 0) return;
try {
send(talk);
} catch(Exception ex) {;}
talkField.setText();
}
void send(String talk) throws Exception {
out.println(talk);
out.flush();
}
// Window 이벤트 구현부
public void windowClosing(WindowEvent e) {
try {
out.close();
in.close();
s.close();
} catch(Exception ex) {;}
System.exit(0);
}
public void windowActivated(WindowEvent e) {;}
public void windowClosed(WindowEvent e) {;}
public void windowDeactivated(WindowEvent e) {;}
public void windowDeiconified(WindowEvent e) {;}
public void windowIconified(WindowEvent e) {;}
public void windowOpened(WindowEvent e) {;}
}
| 번호 | 제 목 | 글쓴이 | 날짜 |
|---|---|---|---|
| 2752135 | [질문]칼라피커 콘트롤에 특정칼라를 선택된상태로만들려면 어떻게하면되나여? | 널위해 | 2026-10-21 |
| 2752110 | $box.find("img").width()에서 이미지에.. | 해뜰참 | 2026-10-21 |
| 2752024 | 소숫점 뺄셈 !! ㅠㅜ | 초롬 | 2026-10-20 |
| 2751970 | 자바 게임 만들려는데 부탁드립니다 (1) | 라온제나 | 2026-10-19 |
| 2751908 | 자바스크립트 처리속도를 체크할 수 있는 방법 있나요? (1) | 테이 | 2026-10-19 |
| 2751822 | 그누보드 아이프레임으로 구현했는데 제목 클릭해서 들어가면 푸터가 동동...떠다녀요 | 내꺼하자 | 2026-10-18 |
| 2751790 | 자바 그래픽 관련을 공부하고 싶은데요.. 어디 좋은 사이트나 책 없을까요?? (1) | 큰재 | 2026-10-18 |
| 2751733 | CTF가머죠? (5) | 두동 | 2026-10-17 |
| 2751709 | 배열부분 궁금한 부분이있습니다. | 딸기우유 | 2026-10-17 |
| 2751677 | 포인터 책을 보다가,,, (2) | 동백 | 2026-10-17 |
| 2751648 | 익스플로 버전에 따라서 생기는 공백.. (1) | 모아 | 2026-10-17 |
| 2751623 | DEL 태그의에서 선굵기 및 컬러지정이 가능한가 해서요~ (4) | 비치나 | 2026-10-16 |
| 2751573 | 테마 업로드 에러.. (1) | 꽃 | 2026-10-16 |
| 2751548 | 수학? 물리?? (3) | 찬늘 | 2026-10-16 |
| 2751461 | 상단네비 fixed ie6 핵 해결방안이 있을까요? | 맑다 | 2026-10-15 |
| 2751436 | 레이아웃을 잡고 코딩을 하는데 드림위버에서의 결과와 IE에서 결과가 다르게 나오네요. (3) | 키클 | 2026-10-15 |
| 2751403 | 형들좀도와주세요string compression (1) | 알렉산더 | 2026-10-14 |
| 2751349 | 링크 다운로드하기 | 팬지 | 2026-10-14 |
| 2751267 | 플래시8 사용에서 플레이어 만들었는데요 (1) | 블1랙캣 | 2026-10-13 |
| 2751182 | 회원가입 | 파랑새 | 2026-10-12 |
