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