(一)实验目的
1、掌握JAVA中与网络程序开发相关的知识点;
2、理解并掌握网络编程开发思想及方法;
3、熟悉项目开发的分包方法和依据;
4、实现聊天室中客服端和服务器端的实现方法;
5、熟悉多线程程序开发方法;
6、完善界面设计。
(二)实验内容和步骤
1、完成聊天程序的服务器端和客服端的界面设计;
2、完成优化聊天程序的服务器端多线程编码;
3、完成与优化聊天程序的客服端编码。
👨🏫 代码参考 pxj 佬
💖 ServerChatOnline.java
java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerChatOnline extends JFrame implements ActionListener
{
private JTextArea jta;
private JScrollPane jsp;
private JPanel Jp;
private JTextField jtf;
private JButton jb;
private BufferedWriter bw;
private void create_windows()
{
jta = new JTextArea();
// 编辑文本区域不可编辑
jta.setEditable(false);
// 将文本域丢到滑动条中
jsp = new JScrollPane(jta);
Jp = new JPanel();
jtf = new JTextField(10);
jb = new JButton("发送");
Jp.add(jtf);
Jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(Jp, BorderLayout.SOUTH);
this.setTitle("聊天室-服务器");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta = new JTextArea();
// 编辑文本区域不可编辑
jta.setEditable(false);
// 将文本域丢到滑动条中
jsp = new JScrollPane(jta);
Jp = new JPanel();
jtf = new JTextField(10);
jtf.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 10)
{
String text = jtf.getText();
text = "s:" + text;
jta.append(text + "\n");
try
{
bw.write(text);
bw.newLine();
bw.flush();
jtf.setText("");
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
});
jb = new JButton("发送");
jb.addActionListener(this);
Jp.add(jtf);
Jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(Jp, BorderLayout.SOUTH);
this.setTitle("聊天室-服务器");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void work()
{
try
{
// 创建服务端套接字
ServerSocket serverSocket = new ServerSocket(10086);
// 等待链接
Socket socket = serverSocket.accept();
// 获取socket输入流
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 获取socket输出流
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = null;
// 循环读取数据,将其拼接到文本域中
while ((line = br.readLine()) != null)
{
// 将传来的数据在文本域中显示出来
jta.append(line + System.lineSeparator());
}
// 关闭socket
serverSocket.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public ServerChatOnline()
{
create_windows();
work();
}
@Override
public void actionPerformed(ActionEvent e)
{
String text = jtf.getText();
text = "s:" + text;
jta.append(text + "\n");
try
{
bw.write(text);
bw.newLine();
bw.flush();
jtf.setText("");
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
💖 ServerChatOnline.java
java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerChatOnline extends JFrame implements ActionListener
{
private JTextArea jta;
private JScrollPane jsp;
private JPanel Jp;
private JTextField jtf;
private JButton jb;
private BufferedWriter bw;
private void create_windows()
{
jta = new JTextArea();
// 编辑文本区域不可编辑
jta.setEditable(false);
// 将文本域丢到滑动条中
jsp = new JScrollPane(jta);
Jp = new JPanel();
jtf = new JTextField(10);
jb = new JButton("发送");
Jp.add(jtf);
Jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(Jp, BorderLayout.SOUTH);
this.setTitle("聊天室-服务器");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta = new JTextArea();
// 编辑文本区域不可编辑
jta.setEditable(false);
// 将文本域丢到滑动条中
jsp = new JScrollPane(jta);
Jp = new JPanel();
jtf = new JTextField(10);
jtf.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 10)
{
String text = jtf.getText();
text = "s:" + text;
jta.append(text + "\n");
try
{
bw.write(text);
bw.newLine();
bw.flush();
jtf.setText("");
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
});
jb = new JButton("发送");
jb.addActionListener(this);
Jp.add(jtf);
Jp.add(jb);
this.add(jsp, BorderLayout.CENTER);
this.add(Jp, BorderLayout.SOUTH);
this.setTitle("聊天室-服务器");
this.setSize(300, 300);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void work()
{
try
{
// 创建服务端套接字
ServerSocket serverSocket = new ServerSocket(10086);
// 等待链接
Socket socket = serverSocket.accept();
// 获取socket输入流
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 获取socket输出流
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line = null;
// 循环读取数据,将其拼接到文本域中
while ((line = br.readLine()) != null)
{
// 将传来的数据在文本域中显示出来
jta.append(line + System.lineSeparator());
}
// 关闭socket
serverSocket.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public ServerChatOnline()
{
create_windows();
work();
}
@Override
public void actionPerformed(ActionEvent e)
{
String text = jtf.getText();
text = "s:" + text;
jta.append(text + "\n");
try
{
bw.write(text);
bw.newLine();
bw.flush();
jtf.setText("");
} catch (IOException ex)
{
ex.printStackTrace();
}
}
}
💖 ChatOnlineDemo.java
java
public class ChatOnlineDemo
{
public static void main(String[] args)
{
new Thread(new Thread1()).start();
new Thread(new Thread2()).start();
}
}
class Thread1 implements Runnable
{
@Override
public synchronized void run()
{
ServerChatOnline S = new ServerChatOnline();
}
}
class Thread2 implements Runnable
{
@Override
public synchronized void run()
{
ClientChatOnline C = new ClientChatOnline();
}
}