Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

UUencode for file transfering in Client server environment

Posted on 2001-09-03
18
276 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 71
even odd program using while loop 3 60
web project error add remove 1 27
junit as external jar or library 7 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

789 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