Skip to content

Instantly share code, notes, and snippets.

@2DNQ
Created August 25, 2017 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2DNQ/26a436ebcf7f6d6fe16e87fffe1c134b to your computer and use it in GitHub Desktop.
Save 2DNQ/26a436ebcf7f6d6fe16e87fffe1c134b to your computer and use it in GitHub Desktop.
Server and client java
package lab1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class client extends JFrame implements ActionListener
{
static JTextArea content; // Khai báo nội dung nhập liệu
static JButton send; // Khai báo nút gửi tin
static JTextField nhap, toName; // Khai báo nội dung nhập liệu
static String chuoi=""; // Tạo chuỗi chưa nội dung nhận
static String temp="",addrr=""; // Tạo chuỗi tạm
static Socket s, sB; // Khai báo socket phía client
static PrintWriter gui; // Khai báo luồn gửi tin
public static void main(String[] args) {
try
{
String ip = JOptionPane.showInputDialog(null, "Nhập IP máy chủ"); // họp thoại thông báo yêu cầu nhập ip server
new client(); // Khởi tạo giao diện
sB = new Socket(ip,2222); // Kết nối dựa trên ip vừa nhập và cổng mặc định
BufferedReader nhan = new BufferedReader(
new InputStreamReader(sB.getInputStream())); // Luồng nhận tin
while((chuoi = nhan.readLine())!= null) // Kiểm tra xem có tin để nhận hay không?
{
temp+=chuoi+"\n"; // Cộng chuỗi tin chat vào biến tạm
content.setText(temp); // Đưa nội dung lên giao diện
content.setVisible(false); // Cập nhật lại giao diện
content.setVisible(true); // Cập nhật lại giao diện
}
}
catch (Exception e) { // Xử lý ngoại lệ
e.printStackTrace();
}
}
public client() {
setSize(600,600); // Định nghĩa kích thước client
setTitle("Client"); // Tiêu đề
Font f = new Font("Arial",Font.BOLD,20); // Font chữ
content = new JTextArea(); // Nội dung chat
content.setFont(f); // Định nghĩa font cho nội dung
content.setBackground(Color.cyan); // Đặt hình nền
JScrollPane sp = new JScrollPane(content); // Cho phép scrolling khi nội dung quá dài
content.setEditable(false); // Không cho phép chỉnh sửa nội dung
send = new JButton("Gửi"); // Tạo nút gửi tin
nhap = new JTextField(30); // Tạo khung nhập tên với chiều dài tối đa
nhap.setFont(f); // Định nghĩa font cho khung
toName = new JTextField("Nhập tên bạn"); // Định nghĩa nội dung mặc định cho khung
toName.setFont(f); // Đặt font cho khung
toName.setBackground(Color.pink); // Đặt nền cho khung
add(toName, BorderLayout.PAGE_START); // Bố trí toName ở trị trí bắt đầu
add(sp, BorderLayout.CENTER); // sp ở vị trí giữa
add(nhap, BorderLayout.PAGE_END); // nhap ở vị trí cuối
nhap.addActionListener(this); // Định nghĩa sự kiện cho khung nhập
setVisible(true); // cập nhật giao diện
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Thoát khi đóng chương trình
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(nhap)) // Bắt sự kiện khi người dùng gửi
{
try
{ gui = new PrintWriter(sB.getOutputStream(),true); // luồn gửi tin
gui.println(toName.getText()+": "+nhap.getText()); // Gửi tin và kèm tên
temp+=toName.getText()+": "+nhap.getText()+"\n"; // Thêm tin gửi vào biến tạm
nhap.setText(""); // Xóa nội dung nhập liệu
nhap.requestFocus(); // Focus con trỏ vào khung nhập liệu
content.setText(temp); // Đưa lên giao diện
content.setVisible(false); // Cập nhật lại giao diện
content.setVisible(true); // Cập nhật lại giao diện
}
catch (Exception r) { // Xử lý ngoại lệ khi xảy ra lỗi
r.printStackTrace();
}
}
}
}
package lab1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
import javax.swing.text.AbstractDocument.Content;
public class server extends JFrame implements ActionListener{
static JTextArea content; // Khai báo ô nhập nội dung chat
static JButton send; // Khai báo nút gửi nội dung
static JTextField nhap, toName; // Khai báo ô nhập tên
static String chuoi=""; // Chuỗi nội dung tin chat giữa 2 máy
static String temp=""; // Chuỗi tạm chứa thông tin xuất lên giao diện
static ServerSocket serA; // Tạo socket server
static Socket s, sA; // Tạo socket kết nối
static PrintWriter gui; // Khai báo biến gửi tin
public static void main(String[] args)
{
new server(); // Hàm tạo đối tượng giao diện chat đã định nghĩa phía dưới
try
{
serA = new ServerSocket(2222); // Tạo cổng kết nối với server ở cổng 2222
s = serA.accept(); // Chấp nhận khi có kết nối
while(true) // Vòng lặp liên tục khi nhận được yêu cầu từ phía client gửi về
{
BufferedReader nhan = new BufferedReader(
new InputStreamReader(s.getInputStream())); // Luồng nhận tin từ Client sẽ được lấy qua biến này
while((chuoi = nhan.readLine())!= null) // Kiểm tra xem có tin để nhận từ Client hay không
{
temp+=chuoi+"\n"; // Khi tin mới được gửi về sẽ được tự động xuống hàng và gán vào biến tạm
content.setText(temp); // Đưa dữ liệu chat lên giao diện
content.setVisible(false); // Cập nhật lại giao diện
content.setVisible(true);// Cập nhật lại giao diện
}
}
}
catch (Exception e) { // Xử lý ngoại lệ khi gặp lỗi
e.printStackTrace();
}
}
public server() {
setSize(600,600); // Định nghĩa kích thước giao diện chat
setTitle("Server"); // Tiêu đề giao diện
Font f = new Font("Arial",Font.BOLD,20); // Định nghĩa Font chữ nội dung
content = new JTextArea(); // Tạo mới thành phần từ khai báo phía trên
content.setFont(f); // Đặt font đã định nghĩa và ô nội dung chat
content.setBackground(Color.black); // Định nghĩa ô nền nội dung
content.setEditable(false); // Đặt chế độ không có phép chỉnh sửa trên ô
JScrollPane sp = new JScrollPane(content); // Định nghĩa cho phép scrolling trên ô nội dung nếu nội dung quá dài
send = new JButton("Gửi"); // Định nghĩa nút gửi tin
nhap = new JTextField(30); // Định nghĩa độ dài kí tự nhập vào khung tên
nhap.setFont(f); // Đặt font đã khai báo phía trên cho khung tên
toName = new JTextField("Nhập tên bạn:"); // Đặt nội dung yêu cầu mặc định
toName.setFont(f); // Định nghĩa font cho toName
toName.setBackground(Color.white); // Định nghĩa nền cho toName
add(toName, BorderLayout.PAGE_START); // Chia bố cục cho toName nằm ở vị trí đầu tiên
add(sp, BorderLayout.CENTER); // Chia bố cục cho sp nằm ở vị trí giữa
add(nhap, BorderLayout.PAGE_END); // Chia bố cục cho khung nhập liệu ở vị trí cuối
nhap.addActionListener(this); // Gán sự kiện khi người dùng nhấn gửi
setVisible(true); // Cập nhật lại giao diện
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Thoát giao diện khi đóng
}
public void actionPerformed(ActionEvent e) { // Khai báo sử lý sự kiện
if (e.getSource().equals(nhap))
{
try
{
gui = new PrintWriter(s.getOutputStream(),true); // Tạo mới đối tượng gửi tin
temp+=toName.getText()+": "+nhap.getText()+"\n"; // Cộng dồn chuỗi tin nhắn vào biến tạm
gui.println(toName.getText()+": "+nhap.getText()); // Gửi tin qua mạng kèm tên
content.setText(temp); // Cập nhật lại giao diện hiển thị nội dung tin
nhap.setText(""); // Xóa trường nhập liệu
nhap.requestFocus(); // Focus con chỏ ngay ô nhập liệu
content.setVisible(false); // Cập nhật lại giao diện
content.setVisible(true); // Cập nhật lại giao diện
}
catch (Exception r) { // Xử lý ngoại lệ khi xảy ra lỗi
r.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment