Results 1 to 10 of 10
- 07-31-2011, 05:46 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
How to Send Multiple File from Server to Client
Hi..
i am just a newbie here,
i want send multiple file (ex.100 files) from server to client using loop.
but the client side just get only 1 file and the connection direct to close.
this is make me confused.
plis help me :
Server Code :
Client Code :Java Code:import java.net.*; import java.io.*; public class Server { public static void main (String [] args ) throws IOException { DataInputStream in; DataOutputStream out; int i =1; ServerSocket servsock = new ServerSocket(13267); while (true) { System.out.println("Waiting..."); i++; Socket sock = servsock.accept(); System.out.println("Accepted connection : " + sock); File myFile = new File ("C:/Test/namafile-"+i+".txt"); byte [] mybytearray = new byte [(int)myFile.length()]; FileInputStream fis = new FileInputStream(myFile); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); OutputStream os = sock.getOutputStream(); System.out.println("Sending..."); os.write(mybytearray,0,mybytearray.length); os.flush(); sock.close(); } } }
Java Code:import java.net.*; import java.io.*; public class Client{ public static void main(String[] args) throws UnknownHostException, IOException{ int i=0; while(true){ int filesize=6022386; DataOutputStream out = null; DataInputStream in =null; long start = System.currentTimeMillis(); int bytesRead; int current = 0; Socket sock = new Socket("127.0.0.1",13267); System.out.println("Connecting..."); try{ i++; byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("C:/Hasil/"+i+"-"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); long end = System.currentTimeMillis(); System.out.println(end-start); bos.close(); } catch(EOFException e){ e.printStackTrace(); } sock.close(); } } }
thanks for moderator..please help me to get client code can receive multiple file from server
the client code can receive first file, and then exception show
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at java.io.BufferedOutputStream.write(BufferedOutputS tream.java:111)
at javasocket.Client.ClientSet(Client.java:55)
at javasocket.Client.main(Client.java:72)
Java Result: 1Last edited by reza1990; 08-01-2011 at 04:22 PM.
- 07-31-2011, 06:26 PM #2
Please edit your code and wrap it in code tags to preserve its formatting. Use the #icon above the input box.
If you want anyone to compile and test your code, you will need to make a small simpler program that compiles and executes to demonstrate your problem. Your code is tied to DB stuff.Last edited by Norm; 07-31-2011 at 06:40 PM.
- 08-01-2011, 03:51 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
please help me... just tell me what should i do..
- 08-01-2011, 03:55 PM #4
You need to remove ALL of the DB code and make the program as simple as possible.If you want anyone to compile and test your code, you will need to make a small simpler program that compiles and executes to demonstrate your problem.
For example use an array as the source of the filenames to be sent.
- 08-01-2011, 04:25 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
- 08-01-2011, 04:30 PM #6
Your need to debug your code.
Add some printlns to show the values of the variables as they change.
For example:
what is the value of current and bytesRead in the do{} while loop?
How many bytes are read by each read() method call?
Why do you always write the full array instead of the number of bytes read into the array?
Add a println to show the name of each file you are writing to.
Have you looked in the file(s) that were written to see what they contain?
- 08-01-2011, 04:52 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
This is debug from my code..
Byte Read : 4
namafile-1.txt : Current :5 BytesRead :-1 MyByteArray: [B@19821f
Byte Read : -1
namafile-2.txt : Current :-1 BytesRead :-1 MyByteArray: [B@190d11
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
- 08-01-2011, 04:55 PM #8
Yes a -1 would cause ArrayIndexOutOfBoundsException.
You need to fix the logic to test for the -1 and not use it as an index.
Have you looked in the file(s) that were written to see what they contain?
- 08-01-2011, 05:51 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
Norm, can you give me an advice, what should i do and change to my client code. So the Exception didn't show anymore.
- 08-01-2011, 05:55 PM #10
For example, test the index value and do NOT use it if invalid:
Have you looked at the line where the exception occurs?Java Code:if(arrayIndex a valid value) { // use the index in the array } // end if
Where did the index get a value of -1? Why does it get that value? Read the API doc for the read() method.
What should you do when it has a value of -1?
Similar Threads
-
send multiple ints of data from the server to the client..
By lkcz in forum New To JavaReplies: 8Last Post: 09-24-2010, 03:34 AM -
send file via client - server model
By spasavvas in forum NetworkingReplies: 15Last Post: 08-13-2010, 11:45 AM -
send bytes from client to server
By 0xHexaDecimal in forum NetworkingReplies: 41Last Post: 06-04-2010, 02:17 PM -
how to send mp3 file from server to client
By Jigga008 in forum NetworkingReplies: 0Last Post: 12-01-2009, 12:02 PM -
how to send a file from server to client and client saves the file?
By KoolCancer in forum New To JavaReplies: 3Last Post: 07-29-2009, 04:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks