Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Last week, we had the author of
TDD for a Shopping Website LiveProject
. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See
for the agenda and registration link
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Sockets and Internet Protocols
send files over the socket
Bobes Calin
Greenhorn
Posts: 22
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a litle problem (i think) on my client server application. The aplication sends one file and not all that i give it to it.
here is the code for the server....
public class FileServer { private ServerSocket server = null; private Socket client = null; private DataInputStream in = null; private DataOutputStream out = null; private FileOutputStream fos = null; public FileServer() { listenSocket(); receiveFileFromClient(in); } public void listenSocket() { try { server = new ServerSocket(4321); System.out.println("Waiting for a client..."); } catch (IOException e) { System.out.println("Could not listen on port 4321"); e.printStackTrace(); } try { client = server.accept(); System.out.println("The server and client are connected!"); in = new DataInputStream(new BufferedInputStream(client.getInputStream())); out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream())); } catch (IOException e) { System.out.println("Accept failed: 4321"); System.exit(-1); } } public void receiveFileFromClient(DataInputStream in) { try { byte[] buf=new byte[512]; int len; int totsize=0; String action = in.readUTF().trim(); System.out.println("action "+action); long fileLong = new Long(in.readLong()); //System.out.println("long value: "+fileLong.toString()); String fname=in.readUTF(); System.out.println("server : file name: "+fname); String prefix=fname.substring(0,fname.lastIndexOf('.')).trim(); String suffix=fname.substring(fname.lastIndexOf('.'),fname.length()).trim(); String file = prefix + suffix; //long length = in.readLong(); fos=new FileOutputStream(file); byte[] fileBuff = new byte[(int)fileLong]; while((len = in.read(fileBuff)) > 0) { totsize=totsize+len; fos.write(fileBuff,0,len); } fos.flush(); DecimalFormat df= new DecimalFormat("#0.00 MB"); System.out.println("bytes read: " + df.format(totsize/1024/1024d)); } catch (Exception e) { e.printStackTrace(); } } protected void finalize() { //Clean up try { fos.close(); out.close(); in.close(); server.close(); } catch (IOException e) { System.out.println("Could not close."); System.exit(-1); } } public static void main(String[] args) { new FileServer(); } }
and for the client....
public class FileClient { private Socket socket = null; private DataInputStream in = null; private DataOutputStream out = null; private FileInputStream fis = null; public FileClient() { connectToServer(); sendFileToServer(out); } public void connectToServer() { try { socket = new Socket("127.0.0.1", 4321); in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); } catch (UnknownHostException e) { System.out.println("Unknown host: 127.0.0.1"); e.printStackTrace(); } catch (IOException e) { System.out.println("No I/O"); e.printStackTrace(); } } public void sendFileToServer(DataOutputStream out) { try { File[] files = new File[]{new File("/home/calin/Desktop/files/temp.flv"), new File("/home/calin/Desktop/files/temp.txt"), new File("/home/calin/Desktop/files/A.txt"), new File("/home/calin/Desktop/files/B.txt")}; for (int i = 0; i < files.length; i++) { byte[] buf=new byte[512]; int len; System.out.println("client : file name: " + files[i].getName()); String action = in.readUTF().trim(); System.out.println("action "+action); Long fileLong = new Long(files[i].length()); out.writeUTF("get"); out.writeLong(fileLong.longValue()); out.flush(); // send the filename out.writeUTF(files[i].getName()); out.writeLong(files[i].length()); fis=new FileInputStream(files[i]); while((len=fis.read(buf))!=-1) { out.write(buf,0,len); } out.flush(); } } catch (IOException e) { e.printStackTrace(); } } protected void finalize() { //Clean up try { fis.close(); out.close(); in.close(); socket.close(); } catch (IOException e) { System.out.println("Could not close."); System.exit(-1); } } public static void main(String[] args) { new FileClient(); } }
Can anybody show me how to send all the files? Thank you!
Calin
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Looks like your client should send all the files, but your server will only read one. "receiveFileFromClient()" should be called in a loop, right?
[Jess in Action]
[AskingGoodQuestions]
Bobes Calin
Greenhorn
Posts: 22
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
yes the server reads only the fisrt file. How can i do the loop? on what criteria?
Thank you!
Calin
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Maybe the client could send the number of files to expect first, and then you could just use a "for" loop.
[Jess in Action]
[AskingGoodQuestions]
Bobes Calin
Greenhorn
Posts: 22
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I tried to send the number of files that are in the File[] array
in the client sendFileToServer()
public void sendFileToServer(DataOutputStream out) { try { File[] files = new File[]{new File("/home/calin/Desktop/files/temp.flv"), new File("/home/calin/Desktop/files/temp.txt"), new File("/home/calin/Desktop/files/A.txt"), new File("/home/calin/Desktop/files/B.txt")}; int fileNumber = files.length; for (int i = 0; i < files.length; i++) { byte[] buf=new byte[512]; int len; System.out.println("client : file name: " + files[i].getName()); // send the number of files to the server out.writeInt(fileNumber); out.flush(); out.writeUTF("get"); out.writeLong(files[i].length()); out.flush(); // send the filename out.writeUTF(files[i].getName()); // send the file length out.writeLong(files[i].length()); fis=new FileInputStream(files[i]); while((len=fis.read(buf))!=-1) { out.write(buf,0,len); } out.flush(); } } catch (IOException e) { e.printStackTrace(); } }
and the server receiveFileFromClient()
public void receiveFileFromClient(DataInputStream in) { try { for(int i = 0; i < in.readInt(); i++) { byte[] buf=new byte[512]; int len; int totsize=0; String action = in.readUTF().trim(); System.out.println("action "+action); long fileLong = new Long(in.readLong()); String fname=in.readUTF(); System.out.println("server : file name: "+fname); String prefix=fname.substring(0,fname.lastIndexOf('.')).trim(); String suffix=fname.substring(fname.lastIndexOf('.'),fname.length()).trim(); String file = prefix + suffix; fos=new FileOutputStream(file); byte[] fileBuff = new byte[(int)fileLong]; while((len = in.read(fileBuff)) > 0) { totsize=totsize+len; fos.write(fileBuff,0,len); } fos.flush(); DecimalFormat df= new DecimalFormat("#0.00 MB"); System.out.println("bytes read: " + df.format(totsize/1024/1024d)); } } catch (Exception e) { e.printStackTrace(); } }
but hte same thing, only the first file is send.
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You want to read the file count from the socket only once, and store it in a variable, and then use that variable in the for loop. If you do this:
for(int i = 0; i < in.readInt(); i++)
then the count gets changed to some random value each time through the loop, and that isn't good!
[Jess in Action]
[AskingGoodQuestions]
Bobes Calin
Greenhorn
Posts: 22
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
yes you are right with the count.
// send the number of files to the server int fileNumber = files.length; out.writeInt(fileNumber); out.flush();
i put it out from the loop and initialized with a with an int value on the server
int files = in.readInt(); for(int i = 0; i < files; i++) ...
but the same thing only one file is copied
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
When to use flush() in Java?
No main class
problem with java.net.SocketException at client site
Transferring file name then file data over socket
Java net BindException:Address already in use: JVM_Bind
More...