Results 1 to 3 of 3
  1. #1
    Nite is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default Problems sending file throught TCP sockets

    I would like to transfer a file throught a tcp socket, here there is what the sender program does :

    Java Code:
    try{
     
    	File localFile = new File("shared/"+fileName);
    	DataOutputStream oos = new DataOutputStream(socket.getOutputStream());	
    	DataInputStream fis = new DataInputStream(new FileInputStream(localFile));
     
    	while(fis.available() > 0){
    		oos.writeByte(fis.readByte());
    	}
     
    	}
    	catch(Exception e){}
    }
    and here what the receiver program does:

    Java Code:
    	try{
     
    	File downloadFile = new File("incoming/"+fileName);
    	downloadFile.createNewFile();
     
    	ois = new DataInputStream(connectionSocket.getInputStream());
    	fos = new DataOutputStream(new FileOutputStream(downloadFile));
     
    	while(ois.available() > 0){
     
    		fos.writeByte(ois.readByte());
    	}
     
    	}
    	catch(Exception e){}
    }

    Where i m wrong? it doesnt work :( , it just create the new file in the incoming folder, but its size remains 0 byte :(

    help a newbye please :D
    Last edited by levent; 08-04-2007 at 05:44 PM. Reason: Code placed inside [code] tag.

  2. #2
    levent Guest

    Default

    You can try putting some System.out.println to test if the program enters your loops!

    Also make sure that the socket connection is working. In these code segments, i did not see any mistakes. I think you should try to find the error by checking which steps are performed as expected and which are not. You can find the error in that way..

  3. #3
    Nite is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    the program didnt enter into the while loops, that was because of the available method, probably the two process didnt sync... anyway i solved in this way :

    int length;
    byte[] buffer = new byte[4096];
    while((length = fis.read(buffer)) != -1){

    out.write(buffer, 0, length);
    }

    and now it works, thanks anyway

Similar Threads

  1. Sending Mail Using Sockets
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:05 PM
  2. Problems with file dir
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 12:08 AM
  3. Servlet sending the file (setting header)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 08:14 PM
  4. sending image file from JSP to Servlet
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 01-10-2008, 02:34 PM
  5. problems with the name of file
    By bbq in forum Java Servlet
    Replies: 0
    Last Post: 06-28-2007, 04:47 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
  •