서버
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){
try{
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);
}catch(Exception e){
System.out.println("네트워크 초기화하다 오류남~");
if(br!=null){ try{br.close();}catch(Exception ee){} br=null; }
if(bw!=null){ try{bw.close();}catch(Exception ee){} bw=null; }
if(socket!=null){ try{socket.close();}catch(Exception ee){} socket=null; }
}
}
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 "exit" :
server.removeGuest(this);
server.broadcast("exit/"+id);
break;
}
}
}catch(Exception e){
server.removeGuest(this);
server.broadcast("exit/"+id);
if(br!=null){ try{br.close();}catch(Exception ee){} br=null; }
}
}
public void sendMsg(String msg){
try{
bw.write(msg+"\n"); bw.flush();
}catch(Exception e){
if(bw!=null){ try{bw.close();}catch(Exception ee){} bw=null; }
}
}
}
class Server{
ArrayList<Guest> list;
ServerSocket ss;
void initNet(){
try{
list=new ArrayList<Guest>();
ss=new ServerSocket(8877);
while(true){
Socket s=ss.accept();
Guest g=new Guest(this,s);
g.start();
addGuest(g);
}
}catch(Exception e){
if(ss!=null){ try{ss.close();}catch(Exception ee){} ss=null; }
}
}
void addGuest(Guest g){
list.add(g);
System.out.println("접속자수:"+list.size());
}
void removeGuest(Guest g){
list.remove(g);
System.out.println("접속자수:"+list.size());
}
void broadcast(String msg){
for(Guest g: list){
g.sendMsg(msg);
}
}
void makeGuestlist(){
StringBuffer buffer=new StringBuffer("guestlist/");
for(Guest g: list){
buffer.append(g.id+"/");
}
broadcast(buffer.toString());
}
public static void main(String args[]){
Server server=new Server();
server.initNet();
}
}
클라이언트
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements ActionListener{
TextField tf; TextArea ta;
Button b; List list;
BufferedReader br;
BufferedWriter bw;
String id;
Client(String id) throws Exception{
this.id=id;
tf=new TextField();
ta=new TextArea();
b=new Button("나가기");
list=new List();
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");
b.addActionListener(this);
tf.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 "exit" :
ta.append(array[1]+"님 퇴장~\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){
try{
if(e.getSource()==b){
sendMsg("exit/");
System.exit(0);
}else{
String line=tf.getText();
if(line.trim().length()>0){
sendMsg("msg/"+tf.getText());
tf.setText("");
}
}
}catch(Exception ee){
System.out.println("보내다가오류남~");
}
}
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();
}
}
'개발하자 > JAVA고급' 카테고리의 다른 글
java.util.regex.PatternSyntaxException (0) | 2015.02.10 |
---|---|
파일전송 (0) | 2015.02.10 |
awt 채팅 Server&Guest (0) | 2015.02.06 |
awt 채팅 Client (0) | 2015.02.06 |
awt 양방향채팅(Server)3/3 (0) | 2015.02.04 |