package day0314;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
* 메세지를 보내고 읽기를 동시에 처리하기 위하여 Thread를 도입하여 클래스를 작성
* 소켓을 사용하여 서버에 연결하고 스트림을 사용하여 서버의 메세지와 나의 메세지를 서버로 전달하는 클래스
*/
@SuppressWarnings("serial")
public class SimpleThreadClient extends Frame implements ActionListener, Runnable {
private TextField tfTalk, tfNick, tfIp;
private Button btnConnect;
private TextArea taDisplay;
private Socket client; // 서버에 연결하기 위한 소켓
private DataInputStream dis; // 메세지를 읽기 위한 스트림
private DataOutputStream dos; // 메세지를 보내기 위한
public SimpleThreadClient() {
super("--------------채팅 클라이언트------------------");
tfTalk = new TextField();
tfNick = new TextField(10);
tfIp = new TextField(10);
btnConnect = new Button("접속");
taDisplay = new TextArea();
taDisplay.setEditable(false);
taDisplay.setBackground(Color.WHITE);
Panel panelNorth = new Panel();
panelNorth.add(new Label("서버주소"));
panelNorth.add(tfIp);
panelNorth.add(new Label("대화명"));
panelNorth.add(tfNick);
panelNorth.add(btnConnect);
add("North", panelNorth);
add("Center", taDisplay);
add("South", tfTalk);
btnConnect.addActionListener(this);
tfTalk.addActionListener(this);
setSize(400, 250);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}// windowClosing
@Override
public void windowClosed(WindowEvent e) {
try {
close();
} catch (IOException e1) {
e1.printStackTrace();
}// catch
}// windowClosed
});
}// SimpleClient
private void close() throws IOException {
if (dis != null) {
dis.close();
}// end if
if (dos != null) {
dos.close();
}// end if
if (client != null) {
client.close();
}// end if
}// close
@Override
public void run(){
try{
if (dis != null) {
while (true) {
// 데이터(서버의 메세지)를 읽어서 대화창(taDisplay)에 출력
taDisplay.append(dis.readUTF() + "\n");
}
}// end if
}catch(IOException ie){
JOptionPane.showMessageDialog(this, "대화상대 접속종료!!!");
}//end catch
}//run
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnConnect) {
String ip=tfIp.getText().replaceAll(" ",""); //공백 없애기
try {
connectToServer(ip);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, ip+"에는 서버가 존재하지 않습니다.");
}//catch
}// end if
if (ae.getSource() == tfTalk) {
//대화내용이 입력되면
String msg = "["+tfNick.getText()+"]"+tfTalk.getText();
//텍스트 에어리어에 출력
taDisplay.append(msg+"\n");
//서버로 전송
try {
dos.writeUTF(msg); //스트림에 메세지 쓰기
dos.flush(); //스트림의 메세지 분출
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tfTalk.setText("");
}// end if
}// actionPerformed
private void connectToServer(String ip) throws IOException {
if (client == null) {
// 소켓 생성
client = new Socket(ip, 3000);
// 스트림 연결
dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
// 메세지 읽기
//대화상대의 닉네임을 보여주고 채팅참여자가 있다는 것을 대화창에 뿌려준다.
taDisplay.append("****** "+dis.readUTF()+"님 대화참여\n");
dos.writeUTF(tfNick.getText());
dos.flush();
Thread t = new Thread(this);// has a
t.start(); //run() 호출
} else {
JOptionPane.showMessageDialog(this, "현재 채팅중입니다.");
}
}// connectToServer
private void readMsg() throws IOException {
}
public static void main(String[] args) {
new SimpleThreadClient();
}// main
}// class