import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

/*
1.Socket 생성자에 서버의IP와 서버의 동작 포트값을(10001)을 인자로 넣어 생성한다.
소켓이 성공적으로 생성되었다면 서버와 접속이 성공적으로 되었다는 것을 의미한다.
2.생선된 socket으로부터 InputStream 과 OutputStream을 구한다.
3.InputStream 은BufferReader 형식으로 변환하고 OutputStream은 PrintWriter 형식으로 변환한다.
4.키보드로 부터 한줄 씩 입력닫는 BufferReader객체를 생성한다.
5.키보드로부터 한줄을 입력받아 PrintWriter에 있는 Printl()메소드를 이용해서 서버에게 전송한다.
6.서버가 다시 봔환하는 문자열을 BufferReader에 있는 readLine()메소드를 이용해서 읽어 들인다.
읽어들인 문자열은 화면에 출력한다.
7.4,5,6을 키보드로부터 quit문자열을 입력받을 때까지 반복한다.
8.키보드로부터 quit문자열이 입력되면 IO객체와 소켓의 close()메소드를 호출한다.
*/
public class ChattingClient 
{
public static void main(String[] args) 
{
CreateChattingUI ccu =new CreateChattingUI();

ccu.setSize(500,500);
ccu.pack();
ccu.setVisible(true);
}
}

//채팅방UI 만드는 클래스
class CreateChattingUI extends JFrame
{
JPanel jp,bottom_jp;
BorderLayout bl;
static JTextArea top_jta;
JTextArea member_jta;
static JTextField username_jtf,msg_jtf;
// JComponent msg_jtf;
JButton msgSend_jb;
static Socket soc;

public CreateChattingUI()
{
super("자바로 만든 채팅방");
//소켓접속
try
{
soc = new Socket("127.0.0.1",10001);
//InputStream in = new ByteArrayInputStream();

}
catch (ConnectException ce)
{
System.out.println("호스트와 연결안됨");
}
catch(Exception e)
{
e.printStackTrace();
}

this.setLayout(new BorderLayout());

jp = new JPanel();
bl = new BorderLayout(2,2);
top_jta = new JTextArea();//왼쪽위 텍스트창
member_jta = new JTextArea(2,12);
username_jtf = new JTextField("대화명",10);
msg_jtf = new JTextField("메세지를 입력하시길 바랍니다.",42);
bottom_jp = new JPanel(new FlowLayout());
msgSend_jb = new JButton("메세지보내기");

top_jta.setEditable(false);
top_jta.setFont(new Font("굴림",Font.BOLD,12));
//System.out.println(msg_jtf.isRequestFocusEnable());

/*
textarea에 스크롤 붙이기
*/
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(600,300));
scrollPane.setBorder(BorderFactory.createTitledBorder(""));
scrollPane.getViewport().add(top_jta, null);
jp.setLayout(bl);
jp.add(scrollPane,BorderLayout.WEST);

jp.add(member_jta,BorderLayout.EAST);
//msg_jtf.setSize(350,30);
bottom_jp.add(username_jtf);
bottom_jp.add(msg_jtf);
bottom_jp.add(msgSend_jb);
this.add(jp,BorderLayout.NORTH);
this.add(bottom_jp,BorderLayout.SOUTH);
//msg_jtf.addFocusListener((new EventManager()).focusGained(new FocusEvent()));
/*
텍스트 필드 클릭시 초기화 이벤트
*/
msg_jtf.addFocusListener(new FocusAdapter()
{
public void focusGained(FocusEvent fe)
{
JTextField tepJtf = (JTextField)fe.getSource();
tepJtf.setText("");
//System.out.println(((JTextField)fe.getSource()).getText());
}
});

username_jtf.addFocusListener(new FocusAdapter()
{
public void focusGained(FocusEvent fe)
{
JTextField tepJtf = (JTextField)fe.getSource();
tepJtf.setText("");
//System.out.println(((JTextField)fe.getSource()).getText());
}
});
/*
1.내용입력하고 보낼시 서버에 전달한다.2번
2.자기 메세지는 바로 텍스트에어리어창에 첫줄에 기록한다.1번
3.그리고 다른 클라이언트 메세지 입력시 창에 뜨게 한다.3번
*/
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
//JTextField jtd = (JTextField)ae.getSource();
String username = username_jtf.getText();
String msg = msg_jtf.getText();
if(username.equals("") || username.equals("대화명"))
{
username = "손님";
}

//username = (Font.getFont(username,new Font("바탕",Font.ITALIC,12))).getFontName();
top_jta.append(username+" : "+msg+"\n");
//top_jta.setLineWrap(true);
msg_jtf.setText("");

sendMsg(username,msg,soc);
}
};
msg_jtf.addActionListener(al);
msgSend_jb.addActionListener(al);
}
static void sendMsg(String username,String msg,Socket soc)
{
InputStream in = null;
OutputStream out = null;
OutputStream out2 = null;
PrintWriter pw = null;
BufferedReader br = null;
OutputStreamWriter osw = null;
BufferedReader br2 = null;

try
{
//in = new ByteArrayInputStream(msg.getBytes());
//in = 
//System.out.println("11111111111111111");
if(soc!=null)
{
//System.out.println("2222222222222");
in = soc.getInputStream();
out = soc.getOutputStream();
// out2 = new ByteArrayOutputStream();
// out2.write(msg.getBytes());
//InputStream in2 = new InputStream();
//br2 = new BufferedReader(new InputStreamReader(in));

//System.out.println(msg.getBytes());
br = new BufferedReader(new InputStreamReader(in));
pw = new PrintWriter(new OutputStreamWriter(out));
pw.println(msg.trim());
pw.flush();
String line=null;
//System.out.println("11111111");
//System.out.println("줄 : "+out2.readLine());
//System.out.println(out2.readLine());
while((line=br.readLine())!=null)
{
//pw.println(line);
//pw.flush();
//String echo = br.readLine();
System.out.println(line);
//System.out.println(msg);
//System.out.println("서버 : "+line);
}
//pw.flush();
}

}
catch (Exception ioe)
{
ioe.printStackTrace();
}
finally
{

try
{
if(br!=null)
br.close();
if(soc!=null)
{
soc.close();
}
}                                                                                                                                                        
catch (IOException ioe)
{

}
}
}
}

'자바 > 채팅방' 카테고리의 다른 글

swing으로 만든 멀티채팅소스  (0) 2010.10.27
멀티서버네번째소스  (0) 2010.10.26
자바 멀티채팅클라이언트 기본소스  (0) 2010.10.24
자바 멀티서버 기본소스  (0) 2010.10.24
채팅방 Client UI  (0) 2010.10.21

+ Recent posts