?
Solved

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

Posted on 2003-03-10
12
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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