Link to home
Start Free TrialLog in
Avatar of belim
belim

asked on

UUencode for file transfering in Client server environment

I am writing a Chat program in client server environment,and wanted to transfer file through Server,but
failed to do so.
I read the input file at the client side into byte format,and convert it into string before send it over to server with Socket.printstream,at the other end(server side) i will use socket.inputstream.readline to read the string,and then write to the file.But it prompt me error message in server side with "String out of index range:0",can anyone tell me how to encode the file which enable me to send over and decode in the server side???


Avatar of muhotrepius
muhotrepius

Can't you just send/receive a byte stream ??
converting to/from String will sure damage the binaru data (because of encodings etc)
Avatar of belim

ASKER

I cann't send the byte stream over to the server, because the first character of the string will decide what action i going to take to this data which sent over.This is the protocol that i defined.Further more, i don't think inputstream can read other data type,because i m using readline method.Anyone have a working example code that implement uuencode and uudecode to send file.
if this is YOUR OWN protocol, you'd better design to so that it can carry binary data too
Avatar of belim

ASKER

Any idea to convert it into binary format,what i need is how to convert into binary format then decode in the other end,i know that if i send it by using integer then write at the other end,should be no problem,but the problem is it is too slow.......
any idea???


ASKER CERTIFIED SOLUTION
Avatar of muhotrepius
muhotrepius

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of belim

ASKER

Any idea how to know the file size, so we can reserved for the buffer in the server side to receive the file???
You just send the file size. In the above example, before
the file, write the size. Lets say :
 outs.writeLong(fileSize);
then when you read the command and check it, you know its
a file transfer and you are sure the following bytes are
the file size so:
 long filesize = ins.readLong();
Avatar of belim

ASKER

muhotrepius,
   Below is the code for my program,may be you can have a look and comment i failed receive and send file.



Chat Client
public void actionPerformed (ActionEvent d)
{
if ( d.getSource()==cmdAttachment )
{
FileDialog fileDlg = new FileDialog(this);
fileDlg.show();
fin= fileDlg.getFile();  
File inputfile= new File(fin);
if (fileDlg.getFile() != null)
{        
File fileNameStr = new File(fileDlg.getDirectory() + fileDlg.getFile());
if ( fileNameStr.exists() )
{
fileselect.setText(fileDlg.getDirectory()+fileDlg.getFile());
int length = (int) inputfile.length();
output.println("F"+fin+"~"+(int)length+"~");    
}
}
}
if ( d.getSource()==cmdClearAttach )
{
int a=0 ;  
byte[] buffer =null;
String line=null;
File inputfile= new File(fin);
try {
FileInputStream fis= new FileInputStream(inputfile);
int length = (int) inputfile.length();
buffer = new byte[length];
fis.read(buffer);
line = new String(buffer);
output.println("C"+line);    
output.close();
fis.close();
  }
catch (Exception e)
{
System.out.println("Error: " + e.toString());
}          
}



Chat Server

byte[] buffer =new byte[filesize];
//loop: reads in msg, write it onto sharedmsg
input.read(buffer);
line = new String(buffer,0,filesize);
command=line.charAt(0);
data=line.substring(1);
               

if(command=='F')
{
while(buffer[startindex]!=126)
startindex++;
filename=new String(buffer,1,startindex-1);
stopindex=startindex+1;
while(buffer[stopindex]!=126)
stopindex++;
size=new String(buffer,startindex+1,stopindex-(startindex+1));
filesize=Integer.parseInt(size);
System.out.println(broad+"Received new fileName from "+alias+(int)buffer.length,command);
}    

else if(command=='C')
{    
FileOutputStream fos= new FileOutputStream("c:\\log\\"+filename);
for(int i = 1; i<buffer.length; i++)
fos.write(buffer[i]);
fos.close();
System.out.println(ind+"1 file copied from "+alias,'Q');
filesize=30;
                   
}



What if the user does both actions ???
Then on the server you read both commands in
one buffer !
So you have to parse the whole line or read
the stream part by part.
Anyway to have one file transfer in
two commands is not a good idea.
Also don't forget println() adds "\n\r" at the end.
You can take advantage ot this though:
instead of input.read(buffer);
use BufferedReader and readLine()
Also println/readLine works on my machine but
i'm not sure the encoding works the same way on all
VMs/OSes
.
Avatar of belim

ASKER

muhotrepius,
    If you don't mind! can i send you the file,so you can help me to ammend and make it work! Please give me your email address so i can send you the file.My appologize because i am begineer in JAVA,therefore really out of idea to continue...need some direction from expert here.I will accept you comment here and give you the point.



oki
ten90425<at>rambler<dot>bg
Avatar of belim

ASKER

muhotrepius ,
   Files sent, my address is lim_be@hotmail.com,Looking forward your  reply......
Avatar of belim

ASKER

Still waiting for the answer.....
pleasr me by mail
Avatar of belim

ASKER

Still waiting for the answer.....
pleasr me by mail
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to muhotrepius

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
Avatar of Mick Barry
try

import sun.misc.UUDecoder;

UUDecoder decoder = new UUDecoder();
byte[] out = decoder.decodeBuffer( encodedstring );