Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

File Transfer Java Server Program and a sample client application that tests the server

Posted on 2003-03-10
12
Medium Priority
?
293 Views
Last Modified: 2012-06-27
I have to write a JAva Server application that uses a socket connection to allow a client to specify a file name and have the server send the contents of the file or indicate that the file does not exist.

Also the client should save the file to a local disk.
0
Comment
Question by:ist249
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8107255
That's interesting for you
0
 

Author Comment

by:ist249
ID: 8107288
Able to connect client to server.
Please help me how to open search for the file and send it back to the client
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8107324
Reading from a input stream and writing to an output stream always seems to work. :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:jcaldwel
ID: 8107466
Jakarta's Commons project has an FTP Client built into it if you don't want to re-invent one.

http://jakarta.apache.org/commons/index.html

Of course you would have to find an FTP Server for your server.

0
 
LVL 1

Accepted Solution

by:
pronane earned 300 total points
ID: 8111047
Here is the code to do this;

page 86
import java.io.*;
import java.net.*;
public class FileReaderServer
{
public static void main (String[ ] args)
{
try
{
// create server socket and listen on port 9999
ServerSocket listen = new ServerSocket (9999);
while (true)
{
System.out.println (“waiting for connection”);
Socket socket = listen.accept ( );
BufferedReader from_client =
new BufferedReader (
new InputStreamReader
(socket.getInputStream ( ));
page 87
PrintWriter to_client =
new PrintWriter
(socket.getOutputStream ( ));
String filename = from_client.readLine ( );
File inputFile = new File (filename);
if (!inputFile.exists ( ))
{
to_client.println (“cannot open ” +
filename);
to_client.close ( );
from_client.close ( );
socket.close ( );
continue;
}
// read lines from file and send to the client
System.out.println (“reading ” + filename);
BufferedReader input =
new BufferedReader (
new FileReader (inputFile));
String line;
while ((line = input.readLine ( )) != null)
to_client.println (line);
to_client.close ( );
from_client.close ( );
socket.close ( );
}
}
page 88
catch (Exception ex)
{
System.err.println (ex);
}
}
}


// File Reader Client
import java.io.*;
import java.net.*;
public class Client
{
public static void main (String[ ] args)
{
try
{
// read in command line arguments
if (args.length != 2)
{
System.out.println (“need host and filename”);
System.exit (1);
}
String host = args [0];
String filename = args [1];
//open socket to host on port 999
Socket socket = new Socket (host, 9999);
BufferedReader from_server =
new BufferedReader (
new InputStreamReader (
socket.getInputStream ( )));
PrintWriter to_server = new PrintWriter (
Socket.getOutputStream ( ));
// send filename to server, read and print lines
// from server until serve closes the connection
to_server.println (filename);
to_server.flush ( );
String line;
while ((line = from_server.readLine ( )) != null)
System.out.println (line);
}
catch (Exception ex);
{
System.err.println (ex);
}
}
}


The server is executed by:
java FileReaderServer
and the client is started by:
java Client hostname filename
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8117687
you wanna attach a footnote or a bibliography to that shameless cut and paste?  It's one thing not to credit the proper author or source, but I hope you kids at least remove the page numbers before you turn in your homework. ;)
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8119740
If it will help my score, I could post all of the source to org.apache.commons.net.ftp as an example of an FTP Client.
0
 
LVL 1

Expert Comment

by:pronane
ID: 8120394
I only posted that from my notes, its a program I did way back and had to document in lecture notes.  Lazynes I suppose ;)
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8125940
>>its a program I did way back and had to document in lecture notes

You gotta be careful. If one were to do a quick verbatim search on the web using your code, they may think you were really the distinguished gentleman from Ireland, Prof. David Sinclair of DCU. A small price to pay for Lazynes, i suppose.  ;)
0
 

Expert Comment

by:CleanupPing
ID: 9058803
ist249:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9700496
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to pronane

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question