package Chatting;
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");
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();
for(int i=1;i<array.length;i++)
list.add(array[i]);
}
}
} 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 {
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고급' 카테고리의 다른 글
퇴장기능추가 (0) | 2015.02.06 |
---|---|
awt 채팅 Server&Guest (0) | 2015.02.06 |
awt 양방향채팅(Server)3/3 (0) | 2015.02.04 |
awt 양방향채팅(Guest)2/3 (0) | 2015.02.04 |
awt 양방향채팅(Client )1/3 (0) | 2015.02.04 |