import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame implements ActionListener {
private static final long serialVersionUID = 2987021233750012438L;
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JTextArea t1 = new JTextArea();
JTextField t2 = new JTextField(30);
JButton b1 = new JButton("发送");
JButton bb = new JButton("启动服务器");
ServerSocket ssock;
Accepter accepter;
Waiter wait;
public Server() {
JScrollPane jj = new JScrollPane(t1);
this.getContentPane().add(jj, "Center");
t1.setEditable(false);// 滚动面板不可编辑
p1.add(t2);
p1.add(b1);
p2.add(bb);
t2.addActionListener(this);
b1.addActionListener(this);
bb.addActionListener(this);
this.getContentPane().add(p2, "North");
this.getContentPane().add(p1, "South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("服务端");
this.setSize(600, 400);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Server();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1 || e.getSource() == t2) {
try {
t1.append("服务端说:" + t2.getText() + "\r\n");
wait.out.writeUTF(t2.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == bb) {
try {
ssock = new ServerSocket(5555);
accepter = new Accepter(this, ssock);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
class Accepter extends Thread {
Server fp;
ServerSocket ssock;
public Accepter(Server fp, ServerSocket ssock) {
this.fp = fp;
this.ssock = ssock;
this.start();
}
public void run() {
Socket sock;
try {
sock = ssock.accept();
fp.wait = new Waiter(fp, sock);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Waiter extends Thread {
DataOutputStream out;
DataInputStream in;
Server fp;
Socket sock;
public Waiter(Server fp, Socket sock) {
this.fp = fp;
this.sock = sock;
try {
out = new DataOutputStream(sock.getOutputStream());
in = new DataInputStream(sock.getInputStream());
String msg = in.readUTF();
fp.t1.append("客户端说:" + msg + "\r\n");
out.writeUTF("欢迎你来!");
this.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String msg = null;
while (true) {
try {
msg = in.readUTF();
fp.t1.append("客户端说:" + msg + "\r\n");
} catch (IOException e) {
e.printStackTrace();
break;
}
}
try {
in.close();
out.close();
sock.close();
fp.ssock.close();
JOptionPane.showMessageDialog(null, "系统出现错误或服务器已关闭!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
java 网上聊天(发送方发送数据,接收方能够接到,反之亦然)
需积分: 9 139 浏览量
2010-06-28
10:03:40
上传
评论
收藏 10KB RAR 举报
buzhidao10
- 粉丝: 0
- 资源: 3
最新资源
- 使用C语言与python分别实现文件的读写功能
- zephyr sdk package 2
- zephyr sdk package 1
- optimization.ipynb
- 数据库标识码BSM重排序工具、重构标识码工具
- 基于C语言的校园导航系统报告.doc
- __init__.py
- tensorflow-gpu-2.6.5-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-rocm-2.13.1.600-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp311-cp311-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈