Results 1 to 3 of 3
  1. #1
    rameshraj is offline Member
    Join Date
    Dec 2007
    Posts
    42
    Rep Power
    0

    Thumbs up Sending files over sockets!

    While sending files over sockets,it is ok for small files like text and java.When some larger files like .pdf,.wmv..mp3 etc are sent then the file received is corrupted on the receiving end and the file size is also greater than the sent one.Also the receiving end is waiting infinitely to write.
    Actually what might have been happening?Can anyone give me some idea?

    The receiving end has the receiving code as shown below:

    InputStream in = clientSocket.getInputStream();
    byte[] buf = new byte[Integer.parseInt(headers[2])];//allocate the size as //the number of bytes available on the stream
    //to get the filename and file extensions
    String []fileExtension=headers[1].split("\\.");//split on basis of dot to get the

    //file extension
    //the last content of the filedetails array will be the extension

    String f2=headers[1];//+"."+fileExtension[fileExtension.length-1];
    //or
    f2="E:/E-resources/java/Transfer."+fileExtension[fileExtension.length-1];
    //delete already existing file with same name
    if(new File(f2).exists())
    {
    //new File(f2).delete();
    File file=new File(f2);
    System.out.println("File Deleted\t"+file.delete());
    }
    System.out.println("Writing as a file\t"+f2);
    FileOutputStream fos = new FileOutputStream(new File(f2));
    //int size=in.available();

    while(in.read(buf)>=0)
    {
    fos.write(buf);
    System.out.println("Input streams available are\t"+in.read(buf));

    }
    fos.close();//close the fileoutputStream finally
    in.close();

  2. #2
    M77's Avatar
    M77
    M77 is offline Member
    Join Date
    May 2008
    Location
    LV
    Posts
    43
    Rep Power
    0

    Default

    U will need at least 3 classes: Server that listens port for connection and creates ServerConnections when clients sends files. And Client. Here is the core of these clasees:

    import java.net.*;
    import java.io.*;

    public class Server implements Runnable {
    public static void main(String[] args) {
    new Thread(new ServerLogger(out)).start();
    }

    private Server() {
    conCount=0;
    }

    public void run() {
    try {
    ServerSocket acceptSocket=new ServerSocket(Config.port);
    while (true) {
    try {
    Socket sock=acceptSocket.accept();
    new ServerConnection(this, sock);
    } catch(Exception ex) {
    ex.printStackTrace();
    }
    }
    } catch(Exception e) {
    if (!e.toString().startsWith("java.net.SocketExceptio n")) {
    e.printStackTrace();
    }
    }
    }
    }


    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class ServerConnection implements Runnable {
    private BufferedReader in;

    public ServerConnection(Socket sock) {
    try {
    this.serverLogger=serverLogger;
    sock.setSoTimeout(30000);
    in=new BufferedReader(new InputStreamReader(sock.getInputStream()));
    new Thread(this).start();
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public void run() {
    while (true) {
    try {
    String s=in.readLine();
    if (s != null) {
    System.out.println(s);
    } else {
    return;
    }
    } catch(Exception e) {
    if ((!e.toString().startsWith("java.net.SocketExcepti on")) &&
    (!(e instanceof SocketTimeoutException))) {
    e.printStackTrace();
    } else {
    return;
    }
    }
    }
    }
    }

    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class Client implements {
    private PrintWriter out;

    public static void main(String[] args) {
    new Logger();
    }

    public Client() {
    try {
    Socket s=new Socket(Config.serversite, Config.serverport);
    out=new PrintWriter(s.getOutputStream(), true);
    //write file to out here
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    }

  3. #3
    vglass is offline Member
    Join Date
    Jul 2007
    Posts
    14
    Rep Power
    0

    Default

    In your code you have a comment that says:

    "the number of bytes available on the stream"

    How exactly are you determining the number of bytes available? The available() method is known to be unreliable, so this may be part of the reason your data is getting corrupted.

    Also, you may want to avoid using Reader/Writer classes. If I remember correctly these are meant for character data and can corrupt file transfers.

Similar Threads

  1. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:05 PM
  2. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 09:10 AM
  3. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 06:12 AM
  4. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 10:01 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
  •