Home > Archive > Java Help > October 2004 > File Transfer
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Paul Morrison 2004-10-25, 4:02 pm |
| Hi, this is an assessment that I have for university, so I do not actually
expect people to give me answers as to what to do, I am just asking if it
would be possible for someone with more Java knowledge than me to have a
look at it and see if I going about it the correct way. Rather than try to
explain what it is I have to do, I will just paste the specification below,
with the code below that, sorry if this is not the done thing, Im relatively
new to this newsgroup lark!
Thank you for your time
Paul
------------------------------------------------
Specification:
The program AGASSI has two parameters:
a.. the name of the host we want to connect to
b.. the name of the file we want to read from there
c.. AGASSI should connect at port 14152 at that host and send to the
socket the requested filename (this message goes through the IO streams and
is a complete line)
the socket at that host should then do the following (i.e. you assume that
it does that, it is not your responsibility):
a.. it responds line by line, the first line is an instruction; the
possible instructions (these are all strings) are:
a.. STOP - this means that something has gone wrong and the transfer
terminates, e.g. the file is not accessible at the other end of the line
b.. LINE - this means that the next line will be a line from the
requested text file; the one after that will be an instruction again
c.. END - this signals the successful completion of the transfer
b.. AGASSI should untangle these messages and put the file back together
at your site (of the same name); it should do something sensible if the
protocol is not adhered to, or other problems arise. Hint: it is advisable
to close IO streams once communication has ceased, otherwise data loss can
occur.
------------------------------------------------
Code:
import java.io.*;
import java.net.*;
public class Agassi
{
private PrintWriter output;
private BufferedReader input;
public Agassi()
{
}
public boolean blankLine(String s)
{
if (s == "LINE") {
return true;
}
return false;
}
public boolean checkEnd(String s)
{
if (s == "END") {
return true;
}
return false;
}
public static void main(String args[]) throws Exception
{
if(args.length != 2)
{
System.err.println("usage: java Agassi URL filename");
System.exit(1);
}
InetAddress inetaddress = null;
try
{
inetaddress = InetAddress.getByName(args[0]);
}
catch(UnknownHostException unknownhostexception)
{
System.err.println("URL " + args[0] + " doesn't work");
System.exit(1);
}
Socket socket = null;
try
{
socket = new Socket(args[0], 14152);
}
catch(Exception exception)
{
System.err.println("Could not connect to host");
}
try
{
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
true);
printWriter.println(args[1]);
socket.setSoTimeout(1000);
System.err.println("Here are the contents of the file:");
do
{
String s = bufferedReader.readLine();
if (s == "STOP") {
throw new Exception("Something has gone wrong, transfer terminated");
}
if (s == "LINE")
break;
if (s == "END")
break;
System.out.println(s);
} while(true);
socket.close();
}
catch (IOException ioexception)
{
System.err.println("An error has occured: " + ioexception);
}
}
}
| |
| Paul Lutus 2004-10-25, 4:02 pm |
| Paul Morrison wrote:
> Hi, this is an assessment that I have for university, so I do not actually
> expect people to give me answers as to what to do, I am just asking if it
> would be possible for someone with more Java knowledge than me to have a
> look at it and see if I going about it the correct way. Rather than try to
> explain what it is I have to do, I will just paste the specification
> below, with the code below that, sorry if this is not the done thing, Im
> relatively new to this newsgroup lark!
Stop. Use this recipe:
1. Here is what I have to do __________________.
2. Here is how I tried to do it _______________.
3. But my method fails in the following specific way __________________.
Do not simply post your original assignment and your code without any
comments or questions.
--
Paul Lutus
http://www.arachnoid.com
| |
| Alex Kizub 2004-10-27, 3:57 am |
| Wow! A lot of mistakes...
Did they teach in University to do everything in baby steps?
That's my advice for you.
And always use something real. Why not attempt to reach some real
websites (for example http://yahoo.com) and try it?
You will see immediately how it works.
OK then. Some obvious mistakes which you did.
- don't use == for String. Use "STOP".equals(s).
== is for refernces and can be used in some circumstances, but
forget about it till you are more expereinced Java programmer.
- you don't understand what is static and what is not.
Try to read some articles about it.
Common suggestion - don't use static at all (again for a while).
Create instance of Agassi in main method and invoke some method in
this instance. For example new one called MAIN if you want.
- don't use Exception like this:
throw new Exception("Something has gone wrong, transfer terminated");
First of all you have expected "STOP", why crash programm for expected
result?
Just use return or whatever.
Second - socket will be not cloesed correctly. That's bad practic.
- while (true){} is good enough for your case. do{}while(true); works
but usually it's for another purpose - run cycle at least once. You
run it anyway...
-
private PrintWriter output;
private BufferedReader input;
are for nothing here.
- you created blankLine and endLine and don't use them. So you don't
fulfil requirements.
In general you are in right way. In couple day you will have working
programm if you will work hard :)))
Alex Kizub.
"Paul Morrison" <paul@morrison1985.freeserve.co.uk> wrote in message news:<cljhgg$tf9$1@news8.svr.pol.co.uk>...
> Hi, this is an assessment that I have for university, so I do not actually
> expect people to give me answers as to what to do, I am just asking if it
> would be possible for someone with more Java knowledge than me to have a
> look at it and see if I going about it the correct way. Rather than try to
> explain what it is I have to do, I will just paste the specification below,
> with the code below that, sorry if this is not the done thing, Im relatively
> new to this newsgroup lark!
>
> Thank you for your time
>
> Paul
>
>
> ------------------------------------------------
>
> Specification:
>
> The program AGASSI has two parameters:
>
> a.. the name of the host we want to connect to
> b.. the name of the file we want to read from there
> c.. AGASSI should connect at port 14152 at that host and send to the
> socket the requested filename (this message goes through the IO streams and
> is a complete line)
> the socket at that host should then do the following (i.e. you assume that
> it does that, it is not your responsibility):
> a.. it responds line by line, the first line is an instruction; the
> possible instructions (these are all strings) are:
> a.. STOP - this means that something has gone wrong and the transfer
> terminates, e.g. the file is not accessible at the other end of the line
> b.. LINE - this means that the next line will be a line from the
> requested text file; the one after that will be an instruction again
> c.. END - this signals the successful completion of the transfer
> b.. AGASSI should untangle these messages and put the file back together
> at your site (of the same name); it should do something sensible if the
> protocol is not adhered to, or other problems arise. Hint: it is advisable
> to close IO streams once communication has ceased, otherwise data loss can
> occur.
>
>
> ------------------------------------------------
>
>
> Code:
>
> import java.io.*;
> import java.net.*;
>
> public class Agassi
> {
>
> private PrintWriter output;
> private BufferedReader input;
>
> public Agassi()
> {
> }
>
> public boolean blankLine(String s)
> {
> if (s == "LINE") {
> return true;
> }
> return false;
> }
>
> public boolean checkEnd(String s)
> {
> if (s == "END") {
> return true;
> }
> return false;
> }
>
> public static void main(String args[]) throws Exception
> {
> if(args.length != 2)
> {
> System.err.println("usage: java Agassi URL filename");
> System.exit(1);
> }
> InetAddress inetaddress = null;
> try
> {
> inetaddress = InetAddress.getByName(args[0]);
> }
> catch(UnknownHostException unknownhostexception)
> {
> System.err.println("URL " + args[0] + " doesn't work");
> System.exit(1);
> }
> Socket socket = null;
> try
> {
> socket = new Socket(args[0], 14152);
> }
> catch(Exception exception)
> {
> System.err.println("Could not connect to host");
> }
> try
> {
> BufferedReader bufferedReader = new BufferedReader(new
> InputStreamReader(socket.getInputStream()));
> PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
> true);
> printWriter.println(args[1]);
> socket.setSoTimeout(1000);
> System.err.println("Here are the contents of the file:");
> do
> {
> String s = bufferedReader.readLine();
> if (s == "STOP") {
> throw new Exception("Something has gone wrong, transfer terminated");
> }
> if (s == "LINE")
> break;
> if (s == "END")
> break;
> System.out.println(s);
> } while(true);
> socket.close();
> }
> catch (IOException ioexception)
> {
> System.err.println("An error has occured: " + ioexception);
> }
> }
> }
| |
| Paul Morrison 2004-10-27, 3:58 am |
| Thanks for your help Alex, been working on it for a bit
> OK then. Some obvious mistakes which you did.
> - don't use == for String. Use "STOP".equals(s).
> == is for refernces and can be used in some circumstances, but
> forget about it till you are more expereinced Java programmer.
Stupid mistake, should know better really, that solved a major problem that
I was having, got a constant stream of nulls before!
> - don't use Exception like this:
> throw new Exception("Something has gone wrong, transfer terminated");
> First of all you have expected "STOP", why crash programm for expected
> result?
> Just use return or whatever.
> Second - socket will be not cloesed correctly. That's bad practic.
As the method has no return type I cant just return null, Ive now added a
socket.close() before anything else is done, is there any way that I can get
it to leave the loop here?
> - while (true){} is good enough for your case. do{}while(true); works
> but usually it's for another purpose - run cycle at least once. You
> run it anyway...
Changed this now.
> private PrintWriter output;
> private BufferedReader input;
> are for nothing here.
>
> - you created blankLine and endLine and don't use them. So you don't
> fulfil requirements.
I wrote this stuff before Id written the rest of the program, realised I
didnt need them but forgot to remove them.
Another question, is it ok to just put a txt document in as a parameter, or
will this not work?
Cheers
Paul Morrison
| |
| Tor Iver Wilhelmsen 2004-10-27, 8:57 am |
| "Paul Morrison" <paul@morrison1985.freeserve.co.uk> writes:
> As the method has no return type I cant just return null, Ive now added a
> socket.close() before anything else is done, is there any way that I can get
> it to leave the loop here?
You can use
return;
to leave void methods.
| |
| Paul Morrison 2004-10-27, 8:57 am |
| I now have the following code for the printing of the file:
try
{
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
true);
printWriter.println(args[1]);
socket.setSoTimeout(1000);
System.err.println("Here are the contents of the file:");
while (true)
{
String s = bufferedReader.readLine();
if (s == null)
break;
if (s.equals("STOP")) {
socket.close();
return;
}
if (s.equals("LINE"))
break;
if (s.equals("END"))
break;
System.out.println(s);
}
socket.close();
}
However, all I get when I run the program is the line "Here are the contents
of the file:" and nothing more. I have made a text file with just a few
lines of text, one with the word LINE, one with normal text, and one with
END. Are the break statements in the while loop correct, Im not too
experienced in using these, and dont know if Im using them correctly. The
idea is that, if the line says LINE then it skips this line and goes to the
next line, STOP terminates the transfer, and END means that the transfer has
been completed successfully.
Cheers
Paul
| |
| Alex Kizub 2004-10-27, 8:57 am |
| > As the method has no return type I cant just return null, Ive now added a
> socket.close() before anything else is done, is there any way that I can get
> it to leave the loop here?
If method is void then you can use just return;
To break loop you can use break; operator.
> Another question, is it ok to just put a txt document in as a parameter, or
> will this not work?
You mean name of file? Of course you can do it but then you have use
it as file name.
For example:
FileReader fr=new FileReader(args[0]);
Alex Kizub.
| |
| Alex Kizub 2004-10-27, 3:59 pm |
| if (s.equals("LINE")) break;
should be
if (s.equals("LINE")) continue;
That's another operator in Java :)
Also I told you use
"LINE".equals(s)
because in this case "LINE" is always Strrin when you can't be sure
about this for s.
Alex Kizub.
"Paul Morrison" <paul@morrison1985.freeserve.co.uk> wrote in message news:<clnmsj$is7$1@newsg1.svr.pol.co.uk>...
> I now have the following code for the printing of the file:
>
> try
> {
> BufferedReader bufferedReader = new BufferedReader(new
> InputStreamReader(socket.getInputStream()));
> PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),
> true);
> printWriter.println(args[1]);
> socket.setSoTimeout(1000);
> System.err.println("Here are the contents of the file:");
> while (true)
> {
> String s = bufferedReader.readLine();
> if (s == null)
> break;
> if (s.equals("STOP")) {
> socket.close();
> return;
> }
> if (s.equals("LINE"))
> break;
> if (s.equals("END"))
> break;
> System.out.println(s);
> }
> socket.close();
> }
>
> However, all I get when I run the program is the line "Here are the contents
> of the file:" and nothing more. I have made a text file with just a few
> lines of text, one with the word LINE, one with normal text, and one with
> END. Are the break statements in the while loop correct, Im not too
> experienced in using these, and dont know if Im using them correctly. The
> idea is that, if the line says LINE then it skips this line and goes to the
> next line, STOP terminates the transfer, and END means that the transfer has
> been completed successfully.
>
> Cheers
>
> Paul
|
|
|
|
|