package com.Toxic.iFile;
import java.io.*;
import java.net.*;
public class RemoteFileClient {
protected BufferedInputStream socketReader;
protected PrintWriter socketWriter;
protected String hostIp;
protected int hostPort;
public RemoteFileClient(String hostIp, int hostPort) {
this.hostIp = hostIp;
this.hostPort=hostPort;
}
public String getFile(String fileNameToGet) {
StringBuffer fileLines = new StringBuffer();
try {
socketWriter.println(fileNameToGet);
socketWriter.flush();
File file = new File("/Users/Toxic/Desktop/new.mp4"); /* receive path and file name*/
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(file));
int read;
while((read = socketReader.read())!= -1) {
fout.write(read);
}
fout.close();
}
catch(IOException e) {
System.out.println("Error reading from file: "+fileNameToGet);
}
return fileLines.toString();
}
public void setUpConnection() {
try {
Socket client = new Socket(hostIp,hostPort);
socketReader = new BufferedInputStream(client.getInputStream());
socketWriter = new PrintWriter(client.getOutputStream());
}
catch(UnknownHostException e) {
System.out.println("Error1 setting up socket connection: unknown host at "+hostIp+":"+hostPort);
}
catch(IOException e) {
System.out.println("Error2 setting up socket connection: "+e);
}
}
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
}catch(IOException e) {
System.out.println("Error tearing down socket connection: "+e);
}
}
public static void main(String args[]) throws IOException {
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",8001); /* Ip address & port to get connection to server */
remoteFileClient.setUpConnection();
StringBuffer fileContents = new StringBuffer();
fileContents.append(remoteFileClient.getFile("/Users/Toxic/Desktop/-.mp4")); /* path and file name to get file from server*/
}
}
/**
* MultithreadRemoteFileServer.java
* Toxic 2011.05.26
*
*/
package com.Toxic.iFile;
import java.io.*;
import java.net.*;
public class MultithreadRemoteFileServer {
int listenPort;
public MultithreadRemoteFileServer(int listenPort) {
this.listenPort = listenPort;
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort, 5);
Socket incomingConnection = null;
while(true) {
incomingConnection = server.accept();
handleConnection(incomingConnection);
}
} catch(BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch(IOException e) {
System.out.println("Unable to instatiate a ServerSocket on port: " + listenPort);
}
}
public void handleConnection(Socket connectionToHandle) {
new Thread(new ConnectionHandler(connectionToHandle)).start();
}
public static void main(String args[]) {
MultithreadRemoteFileServer server = new MultithreadRemoteFileServer(8001); /* server port to provide service*/
server.acceptConnections();
}
}
/**
* ConnectionHandler.java
* Toxic 2011.05.26
*
*/
package com.Toxic.iFile;
import java.io.*;
import java.net.*;
public class ConnectionHandler implements Runnable {
protected Socket socketToHandle;
public ConnectionHandler(Socket socketToHandle) {
this.socketToHandle = socketToHandle;
}
public void run() {
try {
BufferedOutputStream streamWriter = new BufferedOutputStream(socketToHandle.getOutputStream());
BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
String fileToRead = streamReader.readLine();
BufferedInputStream fileReader = new BufferedInputStream(new FileInputStream(fileToRead));
int read;
while((read = fileReader.read())!= -1) {
streamWriter.write(read);
}
fileReader.close();
streamWriter.close();
streamReader.close();
} catch(Exception e) {
System.out.println("Error handling a client: " + e);
e.printStackTrace();
}
}
}