Solved

UUencode for file transfering in Client server environment

Posted on 2001-09-03
18
282 Views
Last Modified: 2007-12-19
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
Comment
Question by:belim
  • 8
  • 5
  • 2
  • +3
18 Comments
 

Expert Comment

by:muhotrepius
ID: 6452525
Can't you just send/receive a byte stream ??
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6452625
converting to/from String will sure damage the binaru data (because of encodings etc)
0
 

Author Comment

by:belim
ID: 6453126
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:heyhey_
ID: 6453244
if this is YOUR OWN protocol, you'd better design to so that it can carry binary data too
0
 

Author Comment

by:belim
ID: 6455499
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
 

Accepted Solution

by:
muhotrepius earned 150 total points
ID: 6456109
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
 

Author Comment

by:belim
ID: 6467224
Any idea how to know the file size, so we can reserved for the buffer in the server side to receive the file???
0
 

Expert Comment

by:muhotrepius
ID: 6469789
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
 

Author Comment

by:belim
ID: 6471100
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
 

Expert Comment

by:muhotrepius
ID: 6473834
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
 

Author Comment

by:belim
ID: 6474729
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
 

Expert Comment

by:muhotrepius
ID: 6475853
oki
ten90425<at>rambler<dot>bg
0
 

Author Comment

by:belim
ID: 6476846
muhotrepius ,
   Files sent, my address is lim_be@hotmail.com,Looking forward your  reply......
0
 

Author Comment

by:belim
ID: 6502288
Still waiting for the answer.....
pleasr me by mail
0
 

Author Comment

by:belim
ID: 6502289
Still waiting for the answer.....
pleasr me by mail
0
 
LVL 5

Expert Comment

by:vemul
ID: 7765977
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
 

Expert Comment

by:SpideyMod
ID: 7847256
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 92

Expert Comment

by:objects
ID: 9417533
try

import sun.misc.UUDecoder;

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet web applications   metadata-complete="true" or false 3 191
How do I remove an object from a 3 53
running on tomcat not jboss eap 7.0 3 30
Coding for the first time 9 65
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question