File transfer using sockets

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

Gordon Beaton

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.

Then why did you decide to do it that way? Who implemented the server?

Or is this homework?

At any rate it shouldn't be hard to take something like this:

"LINE this is a line of text"

then, using methods in the String class, turn it into this:

"this is a line of text"

that you can write to the output.


Please don't start multiple threads with the same question.

/gordon
 
Y

Yu SONG

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

Use *InputStream* instead of Reader

--
Song

/* E-mail.c */
#define User "Yu.Song"
#define At '@'
#define Warwick "warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
J

John Davison

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

First things first, what if the text file you are reading contains the
words LINE or END? Secondly, if you want to copy a file byte for byte
(using the old IO) use InputStream and OutputStream.

From what I know, there are two things you can do to know when you've
received the complete file on the other side. One is for the sender to
first send the size of the file so the other side knows how many bytes
to expect. The second is to close the socket after all the bytes have
been sent, so the other side knows it's over when it reads -1 (EOF).

John Davison
Compass Engineering Group
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top