File transfer

P

Paul Morrison

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);
}
}
}
 
O

Oscar kind

Paul Morrison said:
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!

Ok, as you asked nicely and didn't ask to get your work done for you, I'll
give it a try.


[...]
public boolean blankLine(String s)
{
if (s == "LINE") {
return true;
}
return false;
}

This won't work. Reread the chapter in your book on objects and
references, and think it over.
The error is repeated.


[...]
throw new Exception("Something has gone wrong, transfer terminated");

Ouch. This terminates your program rather suddenly. Is an IOException not
more to the point? Something went wrong with IO. Also, IOExceptions are
caught...


[...]
catch (IOException ioexception)
{
System.err.println("An error has occured: " + ioexception);
}

I'm missing something to ensure the input and output streams are always
closed. Hint: what are the blocks in try/catch/...?
 
A

Ann

Paul Morrison said:
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

Agassi about to retire if he hasn't already, change to Federer ;-)
Is port number too large?
------------------------------------------------

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);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top