Sending files in socket problem///

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 17
Reputation: stewie griffin is an unknown quantity at this point 
Solved Threads: 0
stewie griffin stewie griffin is offline Offline
Newbie Poster

Sending files in socket problem///

 
0
  #1
Jun 9th, 2009
hi,
im working on java on some client server project...
in the project the client and the server can send files, but im failng to do so, the resiver get only part of the file...
here is my code
sender:
  1. public void uploadByteFile(String fileNameToGet, File fileDst)
  2. {
  3.  
  4. try
  5. {
  6.  
  7. FileReader fileReader = new FileReader(new File("c:\\7.mpg"));
  8.  
  9. BufferedReader bufferedFileReader = new BufferedReader(fileReader);
  10. // PrintWriter streamWriter = new PrintWriter(client.getOutputStream());
  11. DataOutputStream streamWriter = new DataOutputStream(client.getOutputStream());
  12.  
  13. String line = null;
  14. PrintWriter outTextStream = new PrintWriter(client
  15. .getOutputStream(), true);
  16. BufferedReader in = new BufferedReader(new InputStreamReader(client
  17. .getInputStream()));
  18.  
  19. outTextStream.println("start?");
  20.  
  21. if (in.readLine().matches("start"))
  22. {
  23. streamWriter.flush();
  24. // streamWriter.flush();
  25. while ((line = bufferedFileReader.readLine()) != null)
  26. {
  27. streamWriter.writeBytes(line);
  28. // System.out.println(line);
  29. }
  30. out.flush();
  31. }
  32.  
  33. //in.close();
  34. }
  35. catch (IOException e)
  36. {
  37. System.out.println("Error reading from file: " + fileNameToGet);
  38. }
  39. finally
  40. {
  41. if (out != null)
  42. {
  43. try
  44. {
  45. out.close();
  46. }
  47. catch (IOException e)
  48. {
  49. System.out.println("Error wile trying to close a file: "
  50. + fileDst.getName());
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }

resiver::
  1.  
  2. FileOutputStream out = null;
  3.  
  4. try
  5. {
  6. PrintWriter outTextStream = new PrintWriter(socket
  7. .getOutputStream(), true);
  8. BufferedReader inTextStream = new BufferedReader(
  9. new InputStreamReader(socket.getInputStream()));
  10.  
  11. out = new FileOutputStream("c:\\91.mpg");
  12.  
  13. int received = 0;
  14. byte[] buff = new byte[1024];
  15. int sum = 0;
  16.  
  17. inTextStream.readLine();
  18. outTextStream.println(("start"));
  19. DataInputStream in = new DataInputStream(socket.getInputStream());
  20.  
  21. while ((received = in.read(buff)) > 0)
  22. {
  23. out.write(buff, 0, received);
  24. System.out.println(sum += buff.length);
  25. }
  26. out.flush();
  27. out.close();
  28. in.close();
  29.  
  30. in.close();
  31. // out = new FileOutputStream("");
  32. out.close();
  33. out.flush();
  34. out = null;
  35.  
  36. }
  37. catch (IOException e)
  38. {
  39. System.out.println("Error reading from file: ");
  40. }
  41. finally
  42. {
  43. if (out != null)
  44. {
  45. try
  46. {
  47. out.close();
  48. }
  49. catch (IOException e)
  50. {
  51. System.out.println("Error wile trying to close a file: ");
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. }


help....
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 17
Reputation: stewie griffin is an unknown quantity at this point 
Solved Threads: 0
stewie griffin stewie griffin is offline Offline
Newbie Poster

Re: Sending files in socket problem///

 
1
  #2
Jun 9th, 2009
After long worm hours (the air conditioner is broken) I found the problem the sender and the reviser was using different type of streams I fixed it .. .and the code is attached
enjoy


sender
  1. try
  2. {
  3.  
  4.  
  5. FileInputStream bufferedFileReader = new FileInputStream(
  6. "c:\\7.mpg");
  7.  
  8. DataOutputStream streamWriter = new DataOutputStream(client
  9. .getOutputStream());
  10.  
  11. String line = null;
  12. int line1 = 0;
  13. PrintWriter outTextStream = new PrintWriter(client
  14. .getOutputStream(), true);
  15. BufferedReader in = new BufferedReader(new InputStreamReader(client
  16. .getInputStream()));
  17.  
  18. outTextStream.println("start?");
  19.  
  20. if (in.readLine().matches("start"))
  21. {
  22. streamWriter.flush();
  23.  
  24. byte[] buff = new byte[1024];
  25. while ((line1 = bufferedFileReader.read(buff, 0, 1024)) > -1)
  26. {
  27. streamWriter.write(buff);
  28. System.out.println(buff);
  29.  
  30. }
  31. out.flush();
  32. }
  33.  
  34. // in.close();
  35. }
  36. catch (IOException e)
  37. {
  38. System.out.println("Error reading from file: " + fileNameToGet);
  39. }
  40. finally
  41. {
  42. if (out != null)
  43. {
  44. try
  45. {
  46. out.close();
  47. }
  48. catch (IOException e)
  49. {
  50. System.out.println("Error wile trying to close a file: "
  51. + fileDst.getName());
  52. e.printStackTrace();
  53. }
  54. }
  55. }
resiver:
  1. try
  2. {
  3. PrintWriter outTextStream = new PrintWriter(socket
  4. .getOutputStream(), true);
  5. BufferedReader inTextStream = new BufferedReader(
  6. new InputStreamReader(socket.getInputStream()));
  7.  
  8. out = new FileOutputStream("c:\\91.mpg");
  9.  
  10. int received = 0;
  11. byte[] buff = new byte[1024];
  12. int sum = 0;
  13.  
  14. inTextStream.readLine();
  15. outTextStream.println(("start"));
  16. DataInputStream in = new DataInputStream(socket.getInputStream());
  17.  
  18. while ((received = in.read(buff)) > -1)
  19. {
  20.  
  21. out.write(buff, 0, received);
  22. // System.out.println(sum += received);
  23. }
  24. out.flush();
  25. out.close();
  26. in.close();
  27.  
  28.  
  29. }
  30. catch (IOException e)
  31. {
  32. System.out.println("Error reading from file: ");
  33. }
  34. finally
  35. {
  36. if (out != null)
  37. {
  38. try
  39. {
  40. out.close();
  41. }
  42. catch (IOException e)
  43. {
  44. System.out.println("Error wile trying to close a file: ");
  45. e.printStackTrace();
  46. }
  47. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC