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

📄 communicatepipe.java

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

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

public class CommunicatePipe implements Runnable {

	// each communicate pipe must have a socket
	// and input and output io
	private Socket s = null;
	private DataInputStream dis = null;
	private DataOutputStream dos = null;
	// DesApp's instance for encryption and dencrption
	private DesApp desApp = new DesApp();
	// for getting secret key
	private Properties pro = null;
	// for shared secret key
	private String sharedKey = null;
	// while loop control flag
	private boolean go = true;
	// bye
	private static final String BYE_BYE = "bye";
	// user name
	private String userName = null;

	// encrypt msg from server
	private String beEncMsgS = null;
	// dencrypted msg by server
	private String beDencMsgS = null;

	/*
	 * constructor
	 */
	public CommunicatePipe(Socket socket, String userName) {
		this.s = socket;
		pro = new Properties();
		this.userName = userName;
		try {
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * pipe disconnect the connected client
	 */
	private void disconnectOneClient() {
		try {
			s.close();
			dos.close();
			dis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 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;
	}

	/*
	 * read data coming from client
	 */
	private String serverReadData = null;

	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();
		}
	}

	/*
	 * setGo
	 */
	private void setGo(boolean ifGo) {
		this.go = ifGo;
	}

	// pipe control flag
	private boolean pipeOpen = true;

	public boolean getPipeState() {
		return this.pipeOpen;
	}

	private void setPipeOpen(boolean pipeOpen) {
		this.pipeOpen = pipeOpen;
	}

	public String getUserName() {
		return userName;
	}

	/*
	 * go method
	 */
	private void startPipe() {

		// load the key
		desApp.getKey(this.getSharedKey(userName + ".key", userName));

		while (go) {
			this.beDencMsgS = desApp.getDesString(this.serverRead());

			if (this.beDencMsgS.equals(BYE_BYE)) {
				this.setPipeOpen(false);
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				this.setGo(false);
				this.disconnectOneClient();
				TCPServerM.clientsNum--;
				System.out.println("Currently, server is holding "
						+ TCPServerM.clientsNum + " clients.");
				System.out.println("disconnect with user " + userName);
				extracted();
				break;
			}

			this.beEncMsgS = desApp.getEncString(userName + " said :"
					+ this.beDencMsgS);
			this.serverWrite(this.beEncMsgS);
		}
	}

	@SuppressWarnings("deprecation")
	private void extracted() {
		Thread.currentThread().stop();
	}

	public void run() {
		this.startPipe();
	}
}

⌨️ 快捷键说明

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