Server side
import java.io.*;
import java.net.*;
public class RemoteFileServer {
protected int listenPort = 3000;
protected static int times = 0;
public static void main(String[] args) {
RemoteFileServer server = new RemoteFileServer();
server.acceptConnections();
}
public void acceptConnections() {
try
{
ServerSocket server = new ServerSocket(listenPort);
Socket incomingConn = null;
while (true)
{
incomingConn = server.accept();
handleConnection(incomingConn);
}
}
catch (BindException e) {
System.out.println("Unable to bind to port: " + listenPort);
}
catch (IOException e)
{
System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
}
}
public void handleConnection(Socket incomingConnection) {
try
{
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();
// we need 1. to read from target file, 2. to send to the client line by line
BufferedReader streamReader = new BufferedReader(
new InputStreamReader(inputFromSocket));
// to read from a file we need a FileReader, then we need a File, we need a fileName
// the fileName is input from Client side
FileReader fileReader = new FileReader(new File(streamReader.readLine()));
BufferedReader bufferedFileReader = new BufferedReader(fileReader);
PrintWriter streamWriter = new PrintWriter(outputToSocket);
String line = null;
while ((line=bufferedFileReader.readLine()) != null)
{
streamWriter.println(line);
}
System.out.println("Handled " + ++times + (times > 1 ? " requests." : " request."));
fileReader.close();
streamWriter.close();
streamReader.close();
}
catch (IOException e)
{
System.out.println("Error handling a client: " + e);
}
}
}
Client side.
import java.io.*;
import java.net.*;
public class RemoteFileClient {
protected String hostIP;
protected int hostPort;
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
public RemoteFileClient(String _hostIP, int _hostPort) {
hostIP = _hostIP;
hostPort = _hostPort;
}
public static void main(String args[]) {
RemoteFileClient client = new RemoteFileClient("127.0.0.1", 3000);
client.setUpConnection();
//String doc = client.getFile("c:\\users\\leo\\setwifi.bat");
String doc = client.getFile("c:\\users\\leo\\ftp\\learning.hub");
client.tearDownConnection();
System.out.println(doc);
}
// setting up a connection
public void setUpConnection() {
try
{
Socket socket = new Socket(hostIP, hostPort);
socketReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
socketWriter = new PrintWriter(socket.getOutputStream());
}
catch (UnknownHostException e) {
System.out.println("Error settting up socket connection: unknow host at "
+ hostIP + ":" + hostPort);
}
catch (IOException e)
{
System.out.println("Error setting up socket connection: " + e);
}
}
// talking to the host
public String getFile(String fileNameToGet) {
StringBuffer file = new StringBuffer();
try
{
// sending request
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line = null;
int lineNum = 0;
while ((line=socketReader.readLine()) != null)
{
lineNum++;
file.append(lineNum + " " + (line + "\n"));
}
}
catch (IOException e)
{
System.out.println("Error reading file: " + fileNameToGet);
}
return file.toString();
}
public void tearDownConnection() {
try
{
socketWriter.close();
socketReader.close();
}
catch (IOException e)
{
System.out.println("Error tearing down socket: " + e);
}
}
}