• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

FTP Socket Programming

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!
I was wondering if any of you could help me out. I've basically got 3 Java programs. One is called "Copier.java" which lets the user specify the location of a txt file to be copied to a new location on the computer.
The other two are called "FileServer" and "FileClient" and when they are both running, the client program asks for a txt file to fetch and then displays the contents of the txt file in the command prompt (as using j2sdk1.4.2_02 not Jbuilder etc).
With those files I have to create the following:
To produce an application that sends a string to a server specifying a filename, and the server opens that file and reads(displays) it. The file name and it contents are then sent to a client residing somewhere else on the PC. I knoiw this is easy, but i've never used Java before and any pointers will be very helpful.
Aims:
* To design the above (�file transfer� application)
* To implement the above using TCP sockets
Basically, all I have to do is merge the "Copier.java" program to the "FileServer.java" and "FileClient.java" to acehive this, but i'm getting nowhere!
I'll post the codes below:
Thanks!
 
Tony Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Copier.java
import java.io.*;
public class Copier
{
public static void main(String[] args)
{
String inFileName, outFileName, currentLine;
BufferedReader inFile;
PrintWriter outFile;
// All kinds of i/o errors can jump up and byte us--disk-full errors,
// non-existent files, you name it--so here and in the following files
// we basically wrap everything in a "try"
try
{
System.out.println("I am a file copier");
// Let the user specify the file to be copied and the name of the output
// file. We can get the user's keystrokes from System.in, but System.in is
// a raw InputStream, so we convert it to something friendlier--something
// we can call readLine() on

BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter input filename:");
inFileName = keyIn.readLine();
System.out.println("Enter output filename:");
outFileName = keyIn.readLine();
keyIn.close();
// With the names in hand, we get the input and output streams of the
// corresponding files, and again turn them into more easily usable objects

inFile = new BufferedReader(new InputStreamReader(new FileInputStream(inFileName)));
outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outFileName)));

// now loop over the input object, reading a line at a time, and writing that
// value to the output object; readLine() is defined to return null when the
// input terminates

while ((currentLine = inFile.readLine()) != null)
outFile.println(currentLine);

// release the streams
inFile.close();
outFile.close();
}
catch (FileNotFoundException e)
{
System.out.println("Couldn't get files.");
}
catch (IOException e)
{
System.out.println("Bad i/o happened.");
}
}
}
 
Tony Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// 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;
try
{
System.out.println("FileServer running");
// 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();
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
while ((currentLine = inFile.readLine()) != null)
pw.println(currentLine);
br.close();
pw.close();
inFile.close();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
}
}
 
Tony Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// 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;
try
{
System.out.println("I am a file fetcher");
// Get the filename
BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name of file to fetch");
inFileName = keyIn.readLine();
// Open the connection to the server and convert the raw streams
Socket conn = new Socket("my-pc", 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);
// 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();
conn.close();
}
catch (Exception e)
{
System.out.println("Oops.");
}
}
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like a homework assignment, so I'm going to start slow. Notice how the client, server and copier all have BufferedReader instances? Streams in Java work the same way whether one is writing to a file, socket, database, whatever. Where are you reading the file from? Open a BufferedReader there. Where are you writing the file to? Open a BufferedWriter there. Connect the dots.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic