www.pudn.com > P2P file system.rar > DownloadListener.java


package download; 
 
import java.net.Socket; 
import java.net.SocketException; 
import java.io.*; 
import java.net.ServerSocket; 
 
import util.FileInformation; 
 
/** 
 * @author Chen Fei 
 * @stu_no 0261104 
 * @version 1.0 
 */ 
// A class to listen to requests from other host to establish socket and down 
// file 
public class DownloadListener extends Thread { 
	// Default port set to the listener 
	public final int DEFAULT_CLIENT_PORT = 8765; 
 
	// A client socket wanting connect to listener side 
	private Socket client; 
 
	private ServerSocket listener; 
 
	private DataInputStream in; 
 
	private DataOutputStream out; 
 
	private FileInputStream fileInStream; 
 
 
	// Get the related infomation of the indicated file the client wants to 
	// download 
	public DownloadListener() { 
		super(); 
		try { 
			listener = new ServerSocket(DEFAULT_CLIENT_PORT); 
			start(); 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	} 
 
	public void close() { 
		try { 
			listener.close(); 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
	} 
 
	public void run() { 
		try { 
			// A new socket connection to the listener where file will be 
			// downloaded 
			do { 
				client = listener.accept(); 
				// in = new DataInputStream(client.getInputStream()); 
				ObjectInputStream obj = new ObjectInputStream(client.getInputStream()); 
				FileInformation fileinfo = (FileInformation)obj.readObject(); 
							 
				 
 
				// Enable/disable SO_TIMEOUT with the specified timeout, in 
				// milliseconds. 
				client.setSoTimeout(1000); 
 
				in = new DataInputStream(client.getInputStream()); 
				out = new DataOutputStream(client.getOutputStream()); 
				// Create a new file in client side 
				File localfile = new File(fileinfo.getFullPath()); 
 
/*				System.out.println("DL 2.1"); 
				// If file already exists in client memory 
				if (localfile == null || !localfile.exists()) { 
					return; 
				} 
*/ 
				// Create a new file input stream 
				fileInStream = new FileInputStream(localfile); 
				// Create a new file data array 
				byte[] fileData = new byte[4096]; 
				// Read in the data 
				int byteNumber; 
				while ((byteNumber = fileInStream.read(fileData)) != -1) { 
					// Wirte out the data 
					out.write(fileData, 0, byteNumber); 
					// Frame1.jProgressBar1.setValue(Frame1.jProgressBar1.getValue()+byteNumber); 
				} 
				// Close the file out stream 
				fileInStream.close(); 
				// If file exists already 
				// Close the in stream 
				in.close(); 
				// Close the out stream 
				out.close(); 
				 
				// Close the socket 
				// client.close(); 
			} while (!listener.isClosed()); 
			//listener.close(); 
		} 
		// Connecting listener error 
		catch (SocketException e) { 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} catch (ClassNotFoundException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
 
	} 
 
}