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

📄 peer.java

📁 利用Java开发的一个聊天程序
💻 JAVA
字号:
package myQQ;

import java.io.*;

public class Peer extends AbstPeer {
	
	protected FileInputStream fis;
	protected FileOutputStream fos;
	
	protected BufferedInputStream bis;
	protected BufferedOutputStream bos;	

	protected PrintWriter out;
	protected BufferedReader in;
	
	protected static GUI gui;
	
	private int status = Utility.BEGIN_CONNECT;
	
	private int buffSize = 4096;
	private long fileSize = 0L;

	protected void send(int type, String message) {
		out.print(Utility.addHead(type, message) + "\n");
		out.flush();
		System.out.println("Peer has sent the message: "+  Utility.addHead(type, message));
/*		gui.setStatusContent("Asking opposite peer to reply.");	*/	
	}

	protected void sendFile() {
		File file = new File(gui.getFromFilePath());

		long size = file.length();
		long completeSize = 0L;
		float result = 0;
		long len = 0L;

		try {
			fis = new FileInputStream(file);
			byte[] buff = new byte[buffSize];

			while (completeSize < size&&len!=-1) {
				len = fis.read(buff);
				bos.write(buff);
				bos.flush();
				completeSize += len;
				result = new Long(completeSize).floatValue()/ new Long(size).floatValue() * 100F;
				gui.setStatusContent("Transfering... " + result + "%");
			}
			bos.flush();
			fis.close();
		} catch (FileNotFoundException e) {
			System.out.println("Could not found the file.");
			gui.setStatusContent("Could not found the file.");
		} catch (IOException e) {
			System.out.println("File transfer error.");
			gui.setStatusContent("File transfer error.");
		}
		
	}

	protected String receive() {
		String str = null;
		Message msg = null;
		try {
			str = in.readLine();
			msg = Utility.parseMessage(str);
			if (msg.getType() == Utility.QRY) {
				gui.openDialog(msg.getBody().get(0).toString());
				fileSize = new Long(msg.getBody().get(1).toString()).longValue();
			}
			if (msg.getType() == Utility.RPY) {
				sendFile();
			}
			System.out.println("Peer has received the message: "
					+ msg.getBody());
		} catch (IOException e) {
			gui.setStatusContent("Could not receive the message.");
		}
		return msg.getBody().get(0).toString();
	}

	protected void recvFile() {
		long completeSize = 0L;
		float result = 0;
		long len = 0L;

		File file = new File(gui.getSaveFilePath());
		try {
			fos = new FileOutputStream(file);
			byte[] buff = new byte[buffSize];
			while (completeSize < fileSize&&len!=-1) {
				try {
					Thread.sleep(15);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				
				len = bis.read(buff);
				fos.write(buff);
				fos.flush();
				completeSize += len;
				result = new Long(completeSize).floatValue()/ new Long(fileSize).floatValue() * 100F;
				gui.setStatusContent("Transfering... " + result + "%");
			}
			fos.close();
		} catch (FileNotFoundException e) {
			System.out.println("Could not found the file.");
			gui.setStatusContent("Could not found the file.");
		} catch (IOException e) {
			System.out.println("File transfer error.");
			gui.setStatusContent("File transfer error.");
		}		
	}
	
	protected void close(){
		try{
			if(fis!=null) fis.close();
			if(fos!=null) fos.close();
			if(bis!=null) {bis.close();}
			if(bos!=null) {bos.flush();bos.close();}
			if(out!=null) {out.flush();out.close();}
			if(in!=null) in.close();
		}catch(IOException e){
			gui.setStatusContent("Could not close.");
		}		
	}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}

⌨️ 快捷键说明

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