Results 1 to 2 of 2
  1. #1
    bbq
    bbq is offline Senior Member
    Join Date
    Jun 2007
    Posts
    132
    Rep Power
    0

    Default send files through socket

    Well I tried in a lot of ways to do that, but not always works.

    If you have any idea about that and you have the secure and efective way to send files through socket I'll appreciate that.
    This the code that I am using, it works well for the small files, but for the big ones don't work

    Client
    Java Code:
    synchronized (Writing) {           
            try{
                FileInputStream f=new FileInputStream(up);
                byte bytesFile[]=new byte[f.available()];
                f.read(bytesFile);
                f.close();
                Writing.writeUTF(up.getName());
                Writing.flush();
                Writing.write(bytesFile);
                Writing.flush();
    Server
    Java Code:
     
    
          File dir=new File(Command.substring(5));
                        File n=new File(dir,reading.readUTF());                  
                        FileOutputStream nn=new FileOutputStream(n);
                        do{
                          byte bytesFile[]=new byte[reading.available()];            
                            nn.write(bytesFile);
                            nn.flush();                       
                        }while(reading.available()!=0);
                        nn.close();
    Any suggestions?
    Last edited by bbq; 07-04-2007 at 07:47 AM.

  2. #2
    Heather is offline Senior Member
    Join Date
    Jun 2007
    Posts
    164
    Rep Power
    8

    Default

    The sockets can be slows, depends on your connection.
    The best is that you send an integer indicating how many bytes are you going to send. In other side, reads the integer, and then the number of bytes that indicates.

    Another choice is that the sender closes the connection, in this way the receiver reading until the connection close.

    good luck

Similar Threads

  1. Server socket - send image to client
    By Hinty in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:39 AM
  2. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 08:10 AM
  3. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 04:05 AM
  4. Socket programming - accepting files
    By ravian in forum Networking
    Replies: 2
    Last Post: 11-29-2007, 10:40 AM
  5. XML through a socket
    By Heather in forum XML
    Replies: 2
    Last Post: 07-04-2007, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •