📄 simserver.java
字号:
package net;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import main.*;
import panel.*;
import shape.*;
public class SimServer {
public ServerSocket serverSocket;
public Vector<Socket> clientSocket;
public Vector<ObjectOutputStream> oos;
private final SimPaint aMain;
boolean serveing;
public SimServer(SimPaint apaint) {
aMain = apaint;
clientSocket = new Vector<Socket>();
oos = new Vector<ObjectOutputStream>();
serveing = false;
}
public void sendMessage(String address, String message, Socket client) {
for (int index = 0; index < clientSocket.size(); ++index) {
try {
ObjectOutputStream os = oos.elementAt(index);
if (client == clientSocket.elementAt(index)) {
continue;
}
os.writeObject(new String("chat"));
os.writeObject(new String(address));
os.writeObject(new String(message));
os.flush();
} catch (IOException e) {
}
}
}
public void stopServer() {
serveing = false;
Iterator<ObjectOutputStream> itS = oos.iterator();
while (itS.hasNext()) {
try {
ObjectOutputStream os = itS.next();
os.writeObject(new String("close"));
os.flush();
} catch (IOException e) {
}
}
}
public boolean startServer(int port) {
if (isServeing()) {
stopServer();
}
try {
serverSocket = new ServerSocket(port);
serveing = true;
new Listening(aMain, this).start();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public void sendPaint(SimPaintPanel paint, Socket client) {
for (int index = 0; index < clientSocket.size(); ++index) {
try {
ObjectOutputStream os = oos.elementAt(index);
if (client == clientSocket.elementAt(index)) {
continue;
}
os.writeObject(new String("paint"));
os.writeObject(new SimPaintPanel(paint));
os.flush();
} catch (IOException e) {
}
}
}
public boolean isServeing() {
return serveing;
}
public void sendShape(String panelID, Socket client) {
for (int index = 0; index < clientSocket.size(); ++index) {
try {
ObjectOutputStream os = oos.elementAt(index);
if (client == clientSocket.elementAt(index)) {
continue;
}
os.writeObject(new String("shapeClear"));
os.writeObject(new String(panelID));
os.flush();
} catch (IOException e) {
}
}
}
public void sendSize(String panelID, Dimension size, Socket client) {
for (int index = 0; index < clientSocket.size(); ++index) {
try {
ObjectOutputStream os = oos.elementAt(index);
if (client == clientSocket.elementAt(index)) {
continue;
}
os.writeObject(new String("size"));
os.writeObject(new String(panelID));
os.writeObject(new Dimension(size));
os.flush();
} catch (IOException e) {
}
}
}
public void sendShape(String panelID, SimShape shape, Socket client) {
for (int index = 0; index < clientSocket.size(); ++index) {
try {
ObjectOutputStream os = oos.elementAt(index);
if (client == clientSocket.elementAt(index)) {
continue;
}
os.writeObject(new String("shapeAdd"));
os.writeObject(new String(panelID));
os.writeObject(new SimShape(shape));
os.flush();
} catch (IOException e) {
}
}
}
public void sendShape(String panelID, SimShape shape, int index, Socket client) {
for (int indexi = 0; indexi < clientSocket.size(); ++indexi) {
try {
ObjectOutputStream os = oos.elementAt(indexi);
if (client == clientSocket.elementAt(indexi)) {
continue;
}
os.writeObject(new String("shapeUpdate"));
os.writeObject(new String(panelID));
os.writeObject(new SimShape(shape));
os.write(index);
os.flush();
} catch (IOException e) {
}
}
}
public void sendShape(String panelID, int index, Socket client) {
for (int indexi = 0; indexi < clientSocket.size(); ++indexi) {
try {
ObjectOutputStream os = oos.elementAt(indexi);
if (client == clientSocket.elementAt(indexi)) {
continue;
}
os.writeObject(new String("shapeDelete"));
os.writeObject(new String(panelID));
os.write(index);
os.flush();
} catch (IOException e) {
}
}
}
}
class Listening extends Thread {
private final SimPaint aMain;
private final SimServer server;
public Listening(SimPaint apaint, SimServer aserver) {
aMain = apaint;
server = aserver;
}
@Override
public void run() {
try {
Socket socket;
socket = server.serverSocket.accept();
while (server.isServeing()) {
server.clientSocket.addElement(socket);
server.oos.addElement(new ObjectOutputStream(socket.getOutputStream()));
aMain.paintFrame.sendPaintAll();
new SListening(aMain, server, socket).start();
socket = server.serverSocket.accept();
}
server.serverSocket.close();
} catch (IOException e) {
}
}
}
class SListening extends Thread {
private final SimPaint aMain;
private SimServer server;
private Socket client;
public SListening(SimPaint apaint, SimServer aserver, Socket socket) {
aMain = apaint;
server = aserver;
client = socket;
}
@Override
public void run() {
try {
String line;
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
line = (String) ois.readObject();
while (true) {
if (line.equals("paint")) {
SimPaintPanel newPaintPanel = (SimPaintPanel) ois.readObject();
newPaintPanel.init(true);
aMain.paintFrame.addImageTab('[' + newPaintPanel.getID() + ']',
newPaintPanel);
server.sendPaint(newPaintPanel, client);
} else if (line.equals("shapeAdd")) {
String id = (String) ois.readObject();
SimShape shape = (SimShape) ois.readObject();
aMain.paintFrame.shapeOperation(id, shape);
server.sendShape(id, shape, client);
} else if (line.equals("shapeClear")) {
String id = (String) ois.readObject();
aMain.paintFrame.shapeOperation(id);
server.sendShape(id, client);
} else if (line.equals("size")) {
String id = (String) ois.readObject();
Dimension size = (Dimension) ois.readObject();
aMain.paintFrame.setCanvasSize(id, size);
server.sendSize(id, size, client);
} else if (line.equals("shapeDelete")) {
String id = (String) ois.readObject();
int index = ois.read();
aMain.paintFrame.shapeOperation(id, index);
server.sendShape(id, index, client);
}else if (line.equals("chat")) {
String address = (String) ois.readObject();
String message = (String) ois.readObject();
aMain.addMessage(address, message);
server.sendMessage(address, message, client);
} else if (line.equals("shapeUpdate")) {
String id = (String) ois.readObject();
SimShape shape = (SimShape) ois.readObject();
int index = ois.read();
aMain.paintFrame.shapeOperation(id, shape, index);
server.sendShape(id, shape, index, client);
} else if (line.equals("close")) {
break;
}
line = (String) ois.readObject();
}
ois.close();
client.close();
} catch (Exception e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -