import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class Client extends Frame implements ActionListener {
TextArea ta;
TextField tf;
BufferedWriter bw;
String id;
BufferedReader br;
Client(String id) {
this.id = id;
ta = new TextArea();
tf = new TextField();
add(ta);
add(tf, "South");
tf.addActionListener(this);
}
void initNet() throws Exception {
Socket s = new Socket("localhost", 8888);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
InputStreamReader isr = new InputStreamReader(is);
OutputStreamWriter osw = new OutputStreamWriter(os);
bw = new BufferedWriter(osw);
br = new BufferedReader(isr);
sendMsg("enter/" + id);
}
public void actionPerformed(ActionEvent e) {
try {
sendMsg("msg/" + tf.getText());
tf.setText("");
} catch (Exception ee) {
System.out.println("보내다가 오류남~");
}
}
void sendMsg(String msg) throws Exception {
bw.write(msg + "\n");
bw.flush();
}
public void readMsg() {
try {
while (true) {
String line = br.readLine();
String[] array = line.split("/");
switch (array[0]) {
case "msg":
ta.append(array[1] + "\n");
break;
}
}
} catch (Exception e) {
System.out.println("읽다가에러남~");
}
}
public static void main(String args[]) throws Exception {
Client client = new Client(args[0]);
client.initNet();
client.setBounds(200, 200, 400, 300);
client.setVisible(true);
client.readMsg();
}
}
'개발하자 > JAVA고급' 카테고리의 다른 글
awt 채팅 Client (0) | 2015.02.06 |
---|---|
awt 양방향채팅(Server)3/3 (0) | 2015.02.04 |
awt 양방향채팅(Guest)2/3 (0) | 2015.02.04 |
양방향채팅 (0) | 2015.02.04 |
awt 일대일채팅(클라이언트만 보냄) (0) | 2015.02.04 |