网络画图板;客户端在向服务器发送信息的时候需要定义发送的协议, 简称通信协议
形状类型 int type; 0直线 1矩形 2椭圆
形状数据 int x1,y1,x2,y2;
服务器;只需要不停的接受客户端,转发给另外的客户端
客户端;需要有界面,通信协议,
服务器
客户端一上面画
客户端二上面也会有
服务器:
package 远程演示服务器;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* 服务器的主类
*
* @author Administrator
*
*/
public class ServerMain {
// static不能定义在main方法里面
public static ArrayList<Socket> socketlis = new ArrayList<Socket>();
public static void main(String[] args) {
boolean islive = true;
try {
ServerSocket server = new ServerSocket(9090);
System.out.println("等待连接客户端...");
while (islive) {
Socket socket = server.accept();
ServerThread th = new ServerThread(socket);
th.start();
socketlis.add(socket);
System.out.println(socket.getInetAddress().getHostName()
+ "连上服务器");
}
} catch (IOException e) {
islive = false;
System.out.println("端口不存在,,");
}
}
}
服务器线程来转发数据
package 远程演示服务器;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 使用线程将绘制的内容发给其他的客户端
*
* @author Administrator
*
*/
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//获取输入流
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
int type = dis.readInt();
int x1 = dis.readInt();
int y1 = dis.readInt();
int x2 = dis.readInt();
int y2 = dis.readInt();
// 转发给其它客户端
for (int i = 0; i < ServerMain.socketlis.size(); i++) {
Socket sc = ServerMain.socketlis.get(i);
if (sc != socket) {
OutputStream os = sc.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(type);
dos.writeInt(x1);
dos.writeInt(y1);
dos.writeInt(x2);
dos.writeInt(y2);
dos.flush();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端主界面
package 远程演示客户端;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 远程演示客户端
*
* @author Administrator
*
*/
public class DrawUI extends JFrame {
public static void main(String[] args) {
new DrawUI();
}
private JTextField ipFiled;
private JTextField portFiled;
private JButton Btn;
private JPanel centenr;
private Socket socket;
public DrawUI() {
this.setTitle("远程演示");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
// 绘制区域
centenr = new JPanel();
this.add(centenr,BorderLayout.CENTER);
// 右边聊天区域
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(100, 100));
right.setBackground(Color.lightGray);
this.add(right, BorderLayout.EAST);
// 下边ip设置
JPanel bottom = new JPanel();
bottom.setPreferredSize(new Dimension(500, 50));
bottom.setBackground(Color.lightGray);
this.add(bottom, BorderLayout.SOUTH);
// 设置默认的ip和端口
JLabel iplabel = new JLabel("IP");
ipFiled = new JTextField("127.0.0.1", 15);
JLabel portlabel = new JLabel("port");
portFiled = new JTextField("9090", 10);
Btn = new JButton("连接服务器");
// 创建监听器对象
BtnLisetenr blis = new BtnLisetenr();
Btn.addActionListener(blis);
// 将组件添加到窗体下部
bottom.add(iplabel);
bottom.add(ipFiled);
bottom.add(portlabel);
bottom.add(portFiled);
bottom.add(Btn);
this.setResizable(false);
this.setVisible(true);
//创建监听器对象
ClientLis lis = new ClientLis(this, centenr);
centenr.addMouseListener(lis);
}
// 获得客户端
public Socket getSocket() {
return socket;
}
// 添加链接服务器的监听器
class BtnLisetenr implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("连接服务器")) {
// 获得输入框中的ip和端口
String ip = ipFiled.getText().trim();
String ports = portFiled.getText().trim();
// 在客户端的创建中端口是整型的,所以需要将端口字符串转换成整形
int port = Integer.parseInt(ports);
try {
socket = new Socket(ip, port);
//启动接收消息的线程
receiveThread th = new receiveThread(socket, centenr);
th.start();
Btn.setText("已经连接上服务器..");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
}
客户端监听器类
package 远程演示客户端;
/*
* 监听器类
*/
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JPanel;
public class ClientLis extends MouseAdapter {
private int x1, y1, x2, y2;
private JPanel centenr;
private DrawUI ui;
Graphics g;
public ClientLis(DrawUI ui, JPanel centenr) {
this.ui = ui;
this.centenr = centenr;
}
// 按下
public void mousePressed(MouseEvent e) {
// 获得画布和按下的坐标
g = centenr.getGraphics();
x1 = e.getX();
y1 = e.getY();
}
// 释放
public void mouseReleased(MouseEvent e) {
// 获得释放时的坐标
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
Send();
}
// 将绘制的消息发送给服务器
public void Send() {
// 获得socket
Socket socket = ui.getSocket();
if (socket != null) {
try {
OutputStream os = socket.getOutputStream();
DataOutputStream oos = new DataOutputStream(os);
oos.writeInt(0);
oos.writeInt(x1);
oos.writeInt(y1);
oos.writeInt(x2);
oos.writeInt(y2);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
} else {
javax.swing.JOptionPane.showMessageDialog(null, "网络不通!!!");
}
}
}
客户端线程类,定义通信协议
package 远程演示客户端;
import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;
import javax.swing.JPanel;
/**
* 处理客户端接受服务器的消息
*
* @author Administrator
*
*/
public class receiveThread extends Thread {
private JPanel centenr;
private Socket socket;
boolean islive = true;
private DataInputStream dis;
private Graphics g;
public receiveThread(Socket socket, JPanel centenr) {
this.socket = socket;
this.centenr = centenr;
try {
// 创建输入流
InputStream is = socket.getInputStream();
dis = new DataInputStream(is);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (islive) {
try {
int type = dis.readInt();
int x1 = dis.readInt();
int y1 = dis.readInt();
int x2 = dis.readInt();
int y2 = dis.readInt();
g = centenr.getGraphics();
if (type == 0) {
g.drawLine(x1, y1, x2, y2);
}
} catch (Exception ef) {
islive = false;
ef.printStackTrace();
}
}
}
}