Solved

UUencode for file transfering in Client server environment

Posted on 2001-09-03
18
273 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
Comment Utility
Can't you just send/receive a byte stream ??
0
 
LVL 16

Expert Comment

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

Author Comment

by:belim
Comment Utility
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
 
LVL 16

Expert Comment

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

Author Comment

by:belim
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:muhotrepius
Comment Utility
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
Comment Utility
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
Comment Utility
oki
ten90425<at>rambler<dot>bg
0
 

Author Comment

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

Author Comment

by:belim
Comment Utility
Still waiting for the answer.....
pleasr me by mail
0
 

Author Comment

by:belim
Comment Utility
Still waiting for the answer.....
pleasr me by mail
0
 
LVL 5

Expert Comment

by:vemul
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
try

import sun.misc.UUDecoder;

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 86
Starting to learn JAVA, 7 46
Free Alternative to JIRA 4 51
Requested array size exceeds VM limit 3 44
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now