⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whiteboardserver.java

📁 自己编写的一个java电子白板程序
💻 JAVA
字号:
/**
 * 
 */
package server;

/**
 * @author Administrator
 *
 */

import java.awt.*;
import java.net.*;
import java.io.*;

public class WhiteBoardServer {

	/**
	 * @param args
	 */
	final static int DEFAULT_NUM = 30;
	final static int SERVER_PORT = 5678;
	ServerSocket serverSocket;
	UserThread[] users;
	//commandCenter
	private int commandCenter;
	final static int DRAW_LINE = 0;
	final static int TRANSLATE_POINT = 1;
	final static int USER_SYNC = 2;


	public WhiteBoardServer(){

		try{
		serverSocket = new ServerSocket(SERVER_PORT,5);
		users = new UserThread[DEFAULT_NUM];
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
	void init(){
		for(int i=0;i<DEFAULT_NUM;i++){
			users[i] = new UserThread(this,i);
			users[i].start();
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WhiteBoardServer wbServer = new WhiteBoardServer();
		wbServer.init();
	}

}
class UserThread extends Thread{
	int userID;
	WhiteBoardServer wbServer;
	ServerSocket serverSocket;
	boolean connected = false;
	DataInputStream io_in = null;
	DataOutputStream io_out = null;
	DataBag databag = new DataBag(0,0,0,0,0,0);
	UserThread[] users;
	boolean sync = false;
	private int commandCenter;
	final static int DRAW_LINE = 0;
	final static int TRANSLATE_POINT = 1;
	final static int REQUEST_SYNC = 2;
	final static int ANSWER_SYNC = 3;
	/*add in 2008-5-21*/
	final static int ERASE =4;
	final static int ZOOM_IN = 5;
	final static int ZOOM_OUT = 6;
	/*add in 2008-5-21*/
	Socket usersocket = null;

	UserThread(WhiteBoardServer wbServer,int userID){
		this.wbServer = wbServer;
		this.serverSocket = wbServer.serverSocket;
		this.userID = userID;
		this.users = wbServer.users;
	}
	public void run(){
		while(true){
		try{	
			
			usersocket = serverSocket.accept();
			System.out.println("userID:"+userID+" accept socket");
			System.out.println("remotePort:"+usersocket.getPort()+"\nlocalPort:"+usersocket.getLocalPort());
			if(usersocket.isConnected()& !usersocket.isClosed()){
				connected = true;
				io_in = new DataInputStream(usersocket.getInputStream());
				io_out = new DataOutputStream(usersocket.getOutputStream());
				if(countUser()==1){
					sync = true;
				}
			}else{
				connected = false;
			}
			while(connected){
				if(countUser()>1 && sync == false){
					DataBag databag_sync = new DataBag(0,0,0,0,0,REQUEST_SYNC);
					commandCenter(databag_sync,REQUEST_SYNC);
//					System.out.println("debug_1");
				}

				try{
				databag.color = io_in.readInt();
				databag.x0 = io_in.readInt();
				databag.y0 = io_in.readInt();
				databag.x1 = io_in.readInt();
				databag.y1 = io_in.readInt();
				databag.commandCenter = io_in.readInt();

				switch(databag.commandCenter){
				case ANSWER_SYNC:
					System.out.println("ANSWER_SYNC");
					sendSyncAnswer(databag);
					break;
				case DRAW_LINE:
					sendDraw(databag);
					break;
				case TRANSLATE_POINT:
					sendDraw(databag);
					break;
					
			/*add in 2008-5-21*/	
				case ERASE:
					System.out.println("ERASE");
					sendDraw(databag);
					break;
				case ZOOM_IN:
					System.out.println("ZOOM_IN");
					sendDraw(databag);
					break;
				case ZOOM_OUT:
					sendDraw(databag);
					break;
			/*add in 2008-5-21*/
					
				}

				}catch(java.net.SocketException se){
					System.err.println("java.net.SocketException");
					connected = false;
				}
				if(usersocket.isClosed()){
					connected =false;
					System.out.println("socket closed");
				}
			}
			
		}catch(IOException ioe){
		//	ioe.printStackTrace();
			System.out.println("IOException1");
			connected = false;
		}
		}
	}
	public void commandCenter(DataBag databag,int commandCenter){
		if(commandCenter == REQUEST_SYNC){
			sendSync(databag);
		}
	}
	void sendDraw(DataBag databag){
		for(int i=0;i<users.length;i++){
			if(users[i].connected == true && users[i].usersocket.isClosed())
				users[i].connected = false;
			if(users[i].connected & users[i].userID!=userID){
				users[i].send(databag);
			}
			
		}
	}
	void sendSync(DataBag databag){
//		System.out.println("debug_2");
		for(int i=0;i<users.length;i++){
			if(users[i].connected == true && users[i].usersocket.isClosed())
				users[i].connected = false;

			if(users[i].sync == true && users[i].connected){
				users[i].send(databag);
				System.out.println("send sycn to user:"+i);
				break;
			}
		}
	}
	void sendSyncAnswer(DataBag databag){
		for(int i=0;i<users.length;i++){
			if(users[i].connected == true && users[i].usersocket.isClosed())
				users[i].connected = false;
			if(users[i].sync == false && users[i].connected){
				users[i].send(databag);
				users[i].sync = true;
			
			}
		}	
	}
	void send(DataBag databag){
		try{
		io_out.writeInt(databag.color);
		io_out.writeInt(databag.x0);
		io_out.writeInt(databag.y0);
		io_out.writeInt(databag.x1);
		io_out.writeInt(databag.y1);
		io_out.writeInt(databag.commandCenter);
		}catch(IOException ioe){
			System.err.println("IOException2");
			connected = false;
//			ioe.printStackTrace();
		}
	}
	int countUser(){
		int count=0;
		for(int i=0;i<users.length;i++){
			
			if(users[i].connected)
				count++;}
		
		return count;
	} 
}
class DataBag{
	int color;
	int x0;
	int y0;
	int x1;
	int y1;
	int commandCenter;
	DataBag(int color,int x0,int y0,int x1,int y1,int commandCenter){
		this.color = color;
		this.x0 = x0;
		this.x1 = x1;
		this.y0 = y0;
		this.y1 = y1;
		this.commandCenter = commandCenter;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -