Reputation: 5451
Hello everyone I have implemented a Server client program in Java such that the client can download the files from the server.
But the problem is that I want to exchange some messages(strings) between client and server before the file download. Anything which I used in JAVA to exchange strings between them was able to exchange strings between them but then the file was not downloaded properly. I don't know why.
Client Code for downloading File :
byte[] b = new byte[1024];
int len = 0;
long bytcount = 1024;
File fp = new File("/home/luv/Desktop/LUVSAXENA_IT.docx");
RandomAccessFile ra=new RandomAccessFile(fp,"rw");
ra.seek(0);
InputStream i = sock.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(i));
InputStream is = sock.getInputStream();
while ((len = is.read(b, 0, 1024)) != -1) {
System.out.println("len"+len+"\n");
bytcount = bytcount + 1024;
//decrypt
ra.write(b, 0, len);
}
is.close();
ra.close();
sock.close();
Server Code for sending File :
byte[] buf = new byte[1024];
OutputStream os = sock.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(os, 1024);
File folder = new File("/home/luv/NetBeansProjects/Shared/");
File[] listoffiles=folder.listFiles();
String s;
int i=0;
File fp = new File("/home/luv/NetBeansProjects/Shared/LUVSAXENA_IT.docx");
RandomAccessFile ra = new RandomAccessFile(fp,"r");
long bytecount=1024;
while((i=ra.read(buf, 0, 1024)) != -1)
{
bytecount += 1024;
out.write(buf, 0, i);
out.flush();
}
sock.shutdownOutput();
out.close();
ra.close();
sock.close();
HOW CAN I SAFELY EXCHANGE STRINGS BETWEEN CLIENT AND SERVER BEFORE DOWNLOADING THE FILE SUCH THAT AFTER IT THE FILE CAN BE DOWNLOADED SAFELY.
Thanx in Advance
Upvotes: 0
Views: 339
Reputation: 5315
You will need some kind of protocol that both server an client speak.
The easiest way is to use fixed length messages with a request-response pattern, for example, having 10 bytes for each message string (Xes here to show blanks):
Client sends "HELLOXXXXX"
Server answers "WELCOMEXXX"
Client sends "GIMMEFILEX"
Server ansers "SIZE=83736"
Client reads the next 83736 bytes
Client sends "THANKYOUXX"
Server SENDS "GOODBYEXXX"
Here is a method that helps you reading fixed length of data, it will block until all data is read. When writing data, you do not need any special code, just write it out.
private byte[] readBytes(InputStream stream, int amount) throws IOException
{
byte[] result = new byte[amount];
int read = stream.read(result, 0, amount);
if (read < 0)
{
throw new IOException("Can't read from socket");
}
while (read < amount)
{
int remaining = amount - read;
int newRead = stream.read(result, read, remaining);
if (newRead < 0)
{
throw new IOException("Can't read from socket");
}
read = read + newRead;
}
return result;
}
Upvotes: 1
Reputation: 9954
The way you have created your connection is to write raw data but read characters (Stream vs. Reader/Writer).
I would suggest to use either an ObjectInputStream
/ObjectOutputStream
or to use Base64 encoding for the file and then use Reader
/Writer
.
Also you hve to manage for yourself how to separate your string from the data.
Upvotes: 1