Results 1 to 6 of 6
  1. #1
    santhosh_el is offline Member
    Join Date
    Feb 2009
    Posts
    50
    Rep Power
    0

    Default socket communication taking 4 minutes to transfer 1mbytes to client

    dear sir,

    i have java client code to transfer 1 mb of data from client to server.my server is c code. i am using dataInputStream and dataOutputStream.my problem is taking 4 or 5 minutes to transfer data from client to server.

    can anybody help me
    santhosh babu

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    its possible using just the data input stream on its own, in a loop, where each iteration it reads one byte,
    or on the receiver end where every iteration it writes one byte and then does a flush() to the file system.
    either, or both of these would cause horrible performance.

    For example, on the output stream,
    Java Code:
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("aFile") ) ) );
    and similarly for the input reading part,
    Java Code:
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("aFile") ) ) );
    and then, where reading / writing to streams. such as reading from file, writing to network.. or when reading from network, writing to socket., instead of using the int read() method of the inputStream and write(int) method of outputstream, use the int read(byte[], int, int) with a buffer

    Java Code:
     byte[] buffer = new byte[bufferSize];
     int bytesRead = 0;
     while (  (bytesRead = in.read(buffer, 0, bufferSize)) != -1 ) {
        out.write(buffer, 0, bytesRead);
      }
    where it tries to read up to bufferSize at once, but in the event it reads less than the bufferSize, such as at the end of the file, it will bytesRead will contain the exact number of bytes actually read. So the write operation works on the same range of valid bytes read into the buffer. and the input stream will return -1 bytes read on end of stream so thats when we stop.

    Since you will find this type of operation needs to be done on the upload client (file to socket) and on the server (socket to file), and since everything is either an inputstream or an outputstream, it is convenient to package this buffered copy operation into a utility function, such as
    Java Code:
    public class StreamUtil {
     
      public static final int DEFAULT_BUFFER_SIZE = 1024;
      
      /**
       * Will block the calling thread and copy all from input stream until end of stream (-1 is returned).
       * @param in the stream to copy from.
       * @param out the stream to copy to.
       * @return the total bytes copied.
       * @throws IOException
       */
      public static int streamCopy(InputStream in, OutputStream out) throws IOException {
        if (in == null) {
          throw new IOException("streamCopy() invoked with null inputStream");
        }
        if (out == null) {
          throw new IOException("streamCopy() invoked with null outputStream");
        }
        
        int bytesRead = 0;
        int totalBytesRead = 0;
        int bufferSize = DEFAULT_BUFFER_SIZE;    
        byte[] buffer = new byte[bufferSize];
    
        while (  (bytesRead = in.read(buffer, 0, bufferSize)) != -1 ) {
          totalBytesRead += bytesRead;
          out.write(buffer, 0, bytesRead);
        }
        return totalBytesRead;
      }
    }
    so in the client,
    Java Code:
    DataInputStream fileIn = .... that DataInputStream using BUfferedInputStream thing
    Socket sOut = .. the output stream after we connect socket.
    
    int totalCopied = StreamUtil.streamCopy(fileIn, sOut);
    and on the server, the same idea.

    Java Code:
    InputStream sIn = ... the input stream from the connected socket.
    DataOutputStream fileOut = ... that DataOutputStream using the BufferedOutputStream thing.
    
    int totalCopied = StreamUtil.copyStream(sIn, fileOut);

  3. #3
    y10
    y10 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default sending the file but corrupted and some times zero bytes

    hello,...im trying 2 send file from server to the client but when it send it i open it its either corrupted or some times its send with zero bytes i tried alot to play on the array size on both client and server with no value .. so could u plz check my code .....plz replY and thankz

    client side:
    -------------------------


    public static void RcvdFile()throws IOException
    {
    try{
    int bytesRead;
    int i=0;
    byte [] arrayFile = new byte [500000];// size

    InputStream z = Clientsocket.getInputStream();

    FileOutputStream x = new FileOutputStream("received- "+filename);

    BufferedOutputStream Tofile = new BufferedOutputStream(x);

    System.out.println ("reciving...");

    bytesRead = z.read(arrayFile,0,arrayFile.length);
    i = bytesRead;
    do {

    bytesRead =z.read(arrayFile, i, (arrayFile.length-i));

    if(bytesRead >= 0)
    i+= bytesRead;
    }
    while(bytesRead > -1);
    Tofile.write(arrayFile, 0 , i);

    x.flush();

    Tofile.flush();

    // Tofile.close();

    Clientsocket.close();} }

    SERVER side:
    -----------------------
    rqsted file already defined as file needed.

    if(rqstdFile.exists())
    {
    outputdata.println("ACK");

    byte [] arrayfile =new byte[(int)rqstdFile.length()+9216];//size

    FileInputStream x = new FileInputStream(rqstdFile);//nw

    BufferedInputStream fromFile = new BufferedInputStream(x);

    fromFile.read(arrayfile,0,arrayfile.length); // reading

    OutputStream filesend = Tempsocket.getOutputStream();

    System.out.println("Sending...");

    filesend.write(arrayfile,0,arrayfile.length);//writing file

    //x.close();

    filesend.flush();

    //filesend.close();
    // Tempsocket.close();
    }

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    do.. while doesn't work unless you also check the bytes read > -1 inside the loop.
    also the client side is not writing after reading. the loop appears to be reading the input file and writing the last chunk.

    if I was to use my streamCopy method from previous post, this would look like:
    Client:
    Java Code:
    public static void RcvdFile()throws IOException
    {
    	InputStream z = Clientsocket.getInputStream();
    	
    	BufferedOutputStream Tofile = new BufferedOutputStream(new FileOutputStream("received- "+filename));
    	
    	System.out.println ("reciving...");
    	
    	StreamUtil.streamCopy(z, Tofile);
    	
    	Tofile.flush();
    	Tofile.close();
    	
    	Clientsocket.close();
    }
    Server:
    Java Code:
    if(rqstdFile.exists())
    {
      outputdata.println("ACK");
    
    	BufferedInputStream fromFile = new BufferedInputStream(new FileInputStream(rqstdFile));
    
      OutputStream filesend = Tempsocket.getOutputStream();
    
      System.out.println("Sending...");
    
    	StreamUtil.streamCopy(formFile, filesend);
    	
      fromFile.close();
      filesend.flush();
    }

  5. #5
    y10
    y10 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    thkz for the reply, but actually i used ur method and above ..the file sent with zero bytes .. still the problem is not solved,,any suggestions

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    not sure, it seems to work for me.
    Java Code:
    /**
     *Created on Nov 1, 2009
     */
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import org.junit.Test;
    import StreamUtil; // that streamUtil above
    
    /**
     * @author thein
     *
     */
    public class TestStreamFile {
    
      @Test
      public void server() throws Exception {
        ServerSocket serverSocket = new ServerSocket(6554);
        File file = new File("/tmp/file.csv");
        
        System.out.println("Server: waiting for a client to connect.");
        
        Socket connectedSocket = serverSocket.accept();
        System.out.println("Server: a client has connected.");
        OutputStream out = connectedSocket.getOutputStream();
        InputStream in = new BufferedInputStream(new FileInputStream(file) );
        
    
        int bytes = StreamUtil.streamCopy(in, out);
        
        in.close();
        out.flush();
        out.close();
        
        System.out.println("Server: finished streaming file "+ file + ", " + bytes + " bytes sent.");
        
        connectedSocket.close();
        
        serverSocket.close();
        System.out.println("Server: finished.");
      }
      
      @Test 
      public void client() throws Exception {
        Socket clientSocket = new Socket("localhost", 6554);
        File outputFile = new File("outfile.csv");
        System.out.println("Client: connected to server.");
        InputStream in = clientSocket.getInputStream();
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile) );
        
        int bytes = StreamUtil.streamCopy(in, out);
        
        System.out.println("Client: received " + outputFile + ", " + bytes + " bytes read.");
        
        in.close();
        out.flush();
        out.close();
          
        clientSocket.close();
        System.out.println("Client: finished");
      }
    }
    where the server sends one file and the client saves one file and exits. i just used a file i had in temp folder,

    Running the server junit test and then the client junit test in a different vm, within eclipse,

    gives the output:
    Java Code:
    Server: waiting for a client to connect.
    Server: a client has connected.
    Server: finished streaming file /tmp/file.csv, 3110 bytes sent.
    Server: finished.
    
    Client: connected to server.
    Client: received outfile.csv, 3110 bytes read.
    Client: finished
    and the file is streamed identically without corruption .

Similar Threads

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 07:15 PM
  2. Replies: 2
    Last Post: 09-10-2009, 03:29 PM
  3. Socket Communication on different networks
    By ersachinjain in forum Networking
    Replies: 4
    Last Post: 09-05-2009, 03:32 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  5. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 03:21 PM

Posting Permissions

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