Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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???


0
belim
Asked:
belim
  • 8
  • 5
  • 2
  • +3
1 Solution
 
muhotrepiusCommented:
Can't you just send/receive a byte stream ??
0
 
heyhey_Commented:
converting to/from String will sure damage the binaru data (because of encodings etc)
0
 
belimAuthor Commented:
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
heyhey_Commented:
if this is YOUR OWN protocol, you'd better design to so that it can carry binary data too
0
 
belimAuthor Commented:
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???


0
 
muhotrepiusCommented:
You can always use the stream's read(byte[])/write(byte[])
and then do with the byte arrays whatever you want.
It's a good idea to wrap the streams with DataInputStream
,DataOutputStream to make your life easier.
Here's an example:
import java.net.*;
import java.io.*;

class EE1Client {
   public static void main(String[] a) throws Exception{
      byte[] binaryData = {0,1,2,3,4,5,6,7,8,9};
      Socket sock = new Socket("localhost", 4321);
      DataOutputStream outs = new DataOutputStream(sock.getOutputStream());
      outs.writeChars("F_TRSF_CMD");
      outs.write(binaryData);
      outs.flush();
      outs.close();
   }
}

class EE1Server{
   public static void main(String[] a) throws Exception{
      ServerSocket srvSock = new ServerSocket(4321);
      Socket sock = srvSock.accept();
      DataInputStream ins = new DataInputStream(sock.getInputStream());
      byte[] binaryData = new byte[10];
      char[] cmdArr = new char[10];
      int i=0;
      for(i = 0; i < cmdArr.length; i++)
         cmdArr[i] = ins.readChar();
      String cmd = new String(cmdArr);
      System.out.println("cmd =" + cmd);
      if(cmd.equals("F_TRSF_CMD")) {
         ins.read(binaryData, 0, 10);
         for(i = 0; i < binaryData.length; i++)
            System.out.println("binary["+i+"]="+binaryData[i]);
      }
      ins.close();
   }
}

Add error checking, more parameters to the FILE_TRANSFER_COMMAND /like the file lenght in the beginnig/ and it's a working file transfer :)
0
 
belimAuthor Commented:
Any idea how to know the file size, so we can reserved for the buffer in the server side to receive the file???
0
 
muhotrepiusCommented:
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();
0
 
belimAuthor Commented:
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;
                   
}



0
 
muhotrepiusCommented:
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
.
0
 
belimAuthor Commented:
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.



0
 
muhotrepiusCommented:
oki
ten90425<at>rambler<dot>bg
0
 
belimAuthor Commented:
muhotrepius ,
   Files sent, my address is lim_be@hotmail.com,Looking forward your  reply......
0
 
belimAuthor Commented:
Still waiting for the answer.....
pleasr me by mail
0
 
belimAuthor Commented:
Still waiting for the answer.....
pleasr me by mail
0
 
vemulCommented:
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
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
 
objectsCommented:
try

import sun.misc.UUDecoder;

UUDecoder decoder = new UUDecoder();
byte[] out = decoder.decodeBuffer( encodedstring );
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 8
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now