private JLabel jLabel2 = null; private JTextField textHost = null; private JTextField textName = null; private JTextArea textChat = null; private JTextArea textSend = null; private [r]
Trang 1Chương trình Chat đơn giản sử dụng giao thức TCP
Tool Eclipse
ThreadChat.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ThreadChat implements Runnable{
private Socket socket = null;
public TCPChat chat = null;
ServerSocket server = null;
BufferedReader br = null;
public ThreadChat(){
try{
nhận port 9999 làm tham số
}catch(Exception e){
e.printStackTrace();
}
new Thread(this).start();//khởi động Thread }
public void run(){
try{
while(true){
while((socket = server.accept())!= null){
//nhận kết nối từ máy khác đến
Trang 2br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String st = br.readLine();
} }
}catch(Exception ex){
}finally{
try{
socket.close();
}catch(IOException ex2){}
} }
}
TCPChat.java:
import java.awt.Color;
import java.awt.Frame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.io.PrintWriter;
import java.net.Socket;
public class TCPChat extends Frame {
private static final long serialVersionUID = 1L;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JTextField textHost = null;
private JTextField textName = null;
private JTextArea textChat = null;
private JTextArea textSend = null;
private JButton ButtonSend = null;
private Socket socket = null;
private PrintWriter out = null;
private JTextField getTextHost() {
if (textHost == null) {
textHost = new JTextField();
textHost.setBounds(new Rectangle(150, 45, 301, 31));
}
return textHost;
}
private JTextField getTextName() {
if (textName == null) {
textName = new JTextField();
textName.setBounds(new Rectangle(150, 90, 301, 31));
}
return textName;
Trang 3}
private JTextArea getTextChat() {
if (textChat == null) {
textChat = new JTextArea();
textChat.setBounds(new Rectangle(31, 137, 418, 121));
textChat.setAutoscrolls(true);
}
return textChat;
}
private JTextArea getTextSend() {
if (textSend == null) {
textSend = new JTextArea();
textSend.setBounds(new Rectangle(30, 277, 298, 61));
}
return textSend;
}
private JButton getButtonSend() {
if (ButtonSend == null) {
ButtonSend = new JButton();
ButtonSend.setBounds(new Rectangle(358, 276, 93, 61)); ButtonSend.setText("Send");
ButtonSend.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed
(java.awt.event.ActionEvent e) {
String str = textSend.getText();
String name = textName.getText();
String host = textHost.getText();
try{
//Socket nhận tham số là địa chỉ Host và port
socket = new Socket(host, 9999);
out = new PrintWriter (socket.getOutputStream(),true);
out.println(name +": "+ str +"\n");//truyền chuỗi lên server để xử lý
textChat.append(name+": "+ str +"\n"); textSend.setText("");
textSend.requestFocus();
socket.close();
}catch(Exception ex){
try{
if(socket !=null)
socket.close();
}catch(Exception ex2){
ex2.printStackTrace();
} ex.printStackTrace();
} }
});
}
return ButtonSend;
}
Trang 4public static void main(String[] args) {
}
public TCPChat() {
super();
initialize();
textHost.requestFocus();
ThreadChat obj = new ThreadChat();
obj.chat = this;
}
private void initialize() {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(30, 45, 105, 30)); jLabel1.setText("IP Adress");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(30, 90, 105, 31)); jLabel2.setText("User name");
this.setLayout(null);
this.setSize(483, 366);
this.setTitle("TCP Chat");
this.setBackground(Color.lightGray);
this.add(jLabel1, null);
this.add(jLabel2, null);
this.add(getTextHost(), null);
this.add(getTextName(), null);
this.add(getTextChat(), null);
this.add(getTextSend(), null);
this.add(getButtonSend(), null);
}
public void show(String str){
textChat.append(str);
}
} // @jve:decl-index=0:visual-constraint="10,10"
Kết quả có dạng: