Printing from a buffered reader

P

Paul Morrison

I am creating a program using sockets which connects to a port and sends a
text file to a socket. That socket then relays the message back, taking each
line at a time, each line is preceded by LINE. Once the socket has relayed
the whole text file it sends the string END. This class should put the text
file back together, omitting the LINE and END strings. Basically, I need to
end up with the same text file that I had initially, but it is taken apart
and then put back together again. I have chosen to use a BufferedReader to
read from the socket, and a PrintWriter to print back out to the screen. I
have the following code, but am unsure as to how to differentiate between
the actual text and the LINE and END parts. Any help would be much
appreciated.

-----

import java.io.*;
import java.net.*;

public class Agassi
{

private PrintWriter output;
private BufferedReader input;

public Agassi()
{
}

public static void main(String args[])
{
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);
}
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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top