R Alam

Greenhorn
+ Follow
since Nov 14, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by R Alam

Produce an application that sends a string to a server specifying a filename, and the server opens that file and reads it. The file name and it contents are then sent to a client residing �somewhere� on the network.
Above is the problem specification. Below is the list of code of two java files and what they are doing is: Client asking the file to be transfered to the server (e.g. H:/raheel.txt) and then server is reading the contents of the file and then again server is requesting for the output i.e. where to save file and again client is giving the path (e.g. H:/temp.txt). What i really don't understand is with the last sentence of the problem specification i.e. "The file name and it contents are then sent to a client residing �somewhere� on the network" and how it will work out. I would really appreciate if someone please look into this for me, cheers.
// FileClient.java
import java.net.*;
import java.io.*;
public class FileClient
{
private static final int SERVERPORT = 9999;
public static void main(String[] argv)
{
String inFileName, s, x, outFileName, c, currentLine;
BufferedReader inFile;
PrintWriter outFile;
try
{
System.out.println();
System.out.println();
// Get the filename
Socket conn = new Socket("", SERVERPORT);
BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please Enter a file name to be sent to the server");
inFileName = keyIn.readLine();
// Open the connection to the server and convert the raw streams
// Socket conn = new Socket("LRC-S072", SERVERPORT);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
// Send the filename to the server
pw.println(inFileName);
PrintWriter pow = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
pow.println(inFileName);

System.out.println("Enter output filename:");
outFileName = keyIn.readLine();
inFile = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFileName)));

while ((currentLine = inFile.readLine()) != null)
outFile.println(currentLine);
// release the streams
inFile.close();
outFile.close();
/* System.out.println("Press any key to see the name and cotnents of the file on this client");
c = EasyIn.getString();
//client opens the file and reads it
BufferedReader inFilen = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
while((x = inFilen.readLine()) != null)
{
System.out.println(x);
}
*/
// Read the results line by line, and write them to the display (we could of
// course get a PrintWriter corresponding to a local file, and write the
// results there instead
/*while ((s = br.readLine()) != null)
System.out.println(s);*/
br.close();
pw.close();
inFile.close();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
EasyIn.pause();
}
}
===========================================================================
// FileServer.java
import java.net.*;
import java.io.*;

public class FileServer
{
private static final int PORT = 9999;
public static void main(String[] argv)
{
String inFileName, currentLine, x;
try
{
System.out.println("");
System.out.println("FileServer Waiting for connection");
// Listen for inbound connections, when one comes in, get the Socket object for it,
// and convert the streams of that socket
ServerSocket s = new ServerSocket(PORT, 1);
Socket conn = s.accept();
System.out.println("");
System.out.println("");
System.out.println("<+++++++++++++Client is connected+++++++++++++++++ ");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));//, true);
// Get the filename from the client
inFileName = br.readLine();
// Get an input stream for the corresponding file and turn it into a BufferedReader
BufferedReader inFile = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
// Read the file line by line, and write it back over the network
pw.println(inFileName);
while ((currentLine = inFile.readLine()) != null)
System.out.println(currentLine);
//PrintWriter outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFileName)));
//pw.println(currentLine);
/*BufferedReader inFiles = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
while((currentLine = inFiles.readLine())!= null)
pw.println(inFileName); */
/* PrintWriter psw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()), true);
psw.println("Files Reached at the Srerver side"); */
br.close();
pw.close();
// psw.close();
inFile.close();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
EasyIn.pause();
}
}