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

📄 tcpserverm.java

📁 这是我写的一个关于JAVA SOCKET的程序
💻 JAVA
字号:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


public class TCPServerM {

	// store communication pipes which are thread
	private List<CommunicatePipe> pipeList = new ArrayList<CommunicatePipe>();
	ServerSocket ss = null;
	// for one to one communication, the client side
	Socket s = null;
	private static final int PORT = 8888;
	private String userName = null;
	// for authenticating the pw hash value
	private String pwHashFromClient = null;
	private String pwHashInServer = null;
	// for getting stuff from file, like pw hash value, secret key
	private Properties pro = null;
	// for io communication between server and client
	private DataInputStream dis = null;
	private DataOutputStream dos = null; // new DataOutputStream(os);
	// for server read data
	private String serverReadData = null;
	// shared secret key
	private String sharedKey = null;
	// DesApp
	DesApp desApp = null;
	// access denied msg
	private static final String ACCESS_DENIAL = "Access denied";
	// encrypt authentication msg from server
	private String beEncMsgS = null;
	public static int clientsNum = 0;

	/*
	 * constructor
	 */
	public TCPServerM() {
		pro = new Properties();
	}

	/*
	 * initialize a server with a specified port 8888
	 */
	private void initialize() {
		try {
			ss = new ServerSocket(PORT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * server accept a client request
	 */
	private Socket acceptRequest() {
		try {
			s = ss.accept();
			/*
			 * every time accept a client request we also initialize a io input
			 * stream
			 */
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}

	/*
	 * get the corresponding user pw hash value
	 */
	private void getPwHash(String fileName, String userName) {

		try {
			pro.load(this.getClass().getResourceAsStream(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		pwHashInServer = pro.getProperty(userName);
		// return pwHashInServer;
	}

	/*
	 * get the corresponding user's secret key
	 */
	private String getSharedKey(String fileName, String userName) {

		try {
			pro.load(this.getClass().getResourceAsStream(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		sharedKey = pro.getProperty(userName);
		return sharedKey;
		// return pwHashInServer;
	}

	/*
	 * read data coming from client
	 */
	private String serverRead() {
		try {
			serverReadData = dis.readUTF();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return serverReadData;
	}

	/*
	 * write data to client
	 */
	private void serverWrite(String info) {
		try {
			dos.writeUTF(info);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * go method
	 */
	public void goServer() {
		this.initialize();
		/*
		 * first accept client twice for the username and corresponding pwhash
		 */
		while (true) {
			for (int i = 0; i < pipeList.size(); i++) {
				if (!pipeList.get(i).getPipeState()) {
					pipeList.remove(i);
				}

			}
			clientsNum = pipeList.size();

			Socket socket = this.acceptRequest();
			System.out.println("accept client request");
			System.out.println(socket);
			userName = this.serverRead();
			pwHashFromClient = this.serverRead();
			this.getPwHash(userName + ".hash", userName);
			// load the corresponding key
			// des generator is a temporary value
			desApp = new DesApp();
			desApp.getKey(this.getSharedKey(userName + ".key", userName));
			/*
			 * if the pw hash from client is match the one in the server side
			 * then create a communicate pipe for this client and add the pipe
			 * to pipeList
			 */
			if (this.pwHashFromClient.equals(this.pwHashInServer)) {
				System.out.println("authentication user:" + userName
						+ " successfully");
				// msg encryption
				beEncMsgS = "welcome " + userName + "!";
				this.serverWrite(desApp.getEncString(beEncMsgS));
				CommunicatePipe comPipe = new CommunicatePipe(socket, userName);
				new Thread(comPipe).start();
				this.pipeList.add(comPipe);
			} else {
				System.out.println("Client request has been denied, please check username and pw");
				this.serverWrite(desApp.getEncString(ACCESS_DENIAL));
			}

		}
	}

}

⌨️ 快捷键说明

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