==============클라이언트=======================
package server;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import org.w3c.dom.events.MouseEvent;
class Client extends Frame implements ActionListener,MouseListener {
TextField tf,tf2;
TextArea ta;
Button b,b2;
List list;
Dialog dia;
BufferedReader br;
BufferedWriter bw;
PopupMenu pm;
String id;
MenuItem mi;
Client(String id) throws Exception {
super(id+"님의 채팅창입니다");
this.id = id;
mi = new MenuItem("귓말");
pm = new PopupMenu();
pm.add(mi);
dia = new Dialog(this);
tf = new TextField(15);
ta = new TextArea();
b = new Button("나가기");
b2 = new Button("보내기"); //쪽지 보내기
list = new List();
tf2=new TextField(); // 쪽지보내기 텍필
list.add(pm);
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.setLayout(new BorderLayout());
p2.setLayout(new BorderLayout());
p1.add(ta);
p1.add(list, "East");
p2.add(tf);
p2.add(b, "East");
add(p1, "Center");
add(p2, "South");
list.addMouseListener(this);
tf.addActionListener(this);
b.addActionListener(this);
b2.addActionListener(this);
mi.addActionListener(this);
}
void initNet() throws Exception {
Socket socket = new Socket("localhost", 8877);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
sendMsg("enter/" + id);
}
public void readMsg() {
try {
while (true) {
String line = br.readLine();
System.out.println(line);
String array[] = line.split("/");
switch (array[0]) {
case "enter":
ta.append(array[1] + "님 입장~\n");
break;
case "msg":
ta.append(array[1] + "\n");
break;
case "guestlist":
list.removeAll();
int len = array.length;
for (int i = 1; i < len; i++)
list.add(array[i]);
break;
case "귓속말":
String arr[] = array[1].split("&");
ta.append("["+arr[0]+"->"+arr[1]+"]"+arr[2] + "\n");
break;
}
}
} catch (Exception e) {
System.out.println("읽다가오류남~");
}
}
public void sendMsg(String msg) throws Exception {
bw.write(msg + "\n");
bw.flush();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == tf) {
try {
sendMsg("msg/" + tf.getText());
tf.setText("");
} catch (Exception ee) {
System.out.println("보내다가오류남~");
}
} else if (e.getActionCommand().equals("나가기")) {
try {
System.exit(0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource()==b2){
try {
ta.append("["+id+"->"+list.getSelectedItem()+"]"+tf2.getText() + "\n");
sendMsg("귓속말/"+id+"&"+list.getSelectedItem()+"&"+tf2.getText());
dia.setVisible(false);
dia.dispose();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource() == mi){
dia.add(tf2);
dia.add(b2);
dia.setLayout(new GridLayout(2,0));
dia.setBounds(300, 300, 200, 200);
dia.setVisible(true);
}
}
public void mousePressed(java.awt.event.MouseEvent e) {
if(e.getButton()==3 && list.getSelectedItem() != null ){
pm.show(list, e.getX(), e.getY());
}
}
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String args[]) throws Exception {
Client client = new Client(args[0]);
client.initNet();
client.setBounds(200, 200, 500, 400);
client.setVisible(true);
client.readMsg();
}
}
====================서버==========================
package server;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
class Server {
ArrayList<Guest> list;
void initNet() throws Exception {
list = new ArrayList<Guest>();
ServerSocket ss = new ServerSocket(8877);
while (true) {
Socket s = ss.accept();
Guest g = new Guest(this, s);
g.start();
addGuest(g);
}
}
void addGuest(Guest g) {
list.add(g);
System.out.println("접속자수:" + list.size());
}
public void talkMsg(String talk, String talk2, String talk3) {
//talk 보낸놈
//2 받을놈
//3 할말
for (Guest g : list) {
if(g.id.equals(talk2)){
try {
g.sendMsg("귓속말/"+talk+"&"+talk2+"&"+talk3);
} catch (Exception e) {
System.out.println("게스트에서 귓말보내다가 에러"+e.getMessage());
}
}
}
}
void removeGuest(Guest g) {
list.remove(g);
System.out.println("접속자수:" + list.size());
}
void broadcast(String msg) throws Exception {
for (Guest g : list) {
g.sendMsg(msg);
}
}
void makeGuestlist() throws Exception { // guestlist/홍길동/김길동/이길동/
StringBuffer buffer = new StringBuffer("guestlist/");
for (Guest g : list) {
buffer.append(g.id + "/");
}
broadcast(buffer.toString());
}
public static void main(String args[]) throws Exception {
Server server = new Server();
server.initNet();
}
}
====================게스트(서버랑 함꼐)==============================
package server;
import java.io.*;
import java.net.*;
import java.util.*;
class Guest extends Thread {
String id;
Server server;
Socket socket;
BufferedReader br;
BufferedWriter bw;
Guest(Server server, Socket socket) throws Exception {
this.server = server;
this.socket = socket;
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
}
public void run() {
try {
while (true) {
String line = br.readLine();
System.out.println(line+"읽음");
String array[] = line.split("/");
switch (array[0]) {
case "enter":
id = array[1];
server.makeGuestlist();
server.broadcast(line);
break;
case "msg":
String str = "msg/[" + id + "]" + array[1];
server.broadcast(str);
break;
case "귓속말":
String[] talk=array[1].split("&");
server.talkMsg(talk[0],talk[1],talk[2]);
//talk[0] 보낼넘
//talk[1] 할말
}
}
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage()+"겟메시지");
System.out.println(id + "님 읽다가 에러남ㅠㅠ");
//server.removeGuest(this);
try {
server.broadcast("exit/" + id +"나갓다");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void sendMsg(String msg) throws Exception {
bw.write(msg + "\n");
bw.flush();
}
}
자바 메모리 Stack , heap 정적,동적 할당에 대해 다시 공부 (0) | 2015.02.07 |
---|---|
모니터 하나로 가상 모니터 만들기 (0) | 2015.02.07 |
오라클 group_id()를 이용해 총합 , 평균 , count 세보기 (0) | 2015.02.06 |
JSP include 간단 개념 (0) | 2015.02.06 |
JSP application , session과 화면이동 3가지 방법 (0) | 2015.02.06 |