Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Who knows networking with Java

  1. Feb 16, 2005 #1
    Does anyone know a way how to transfer a file from one computer to the other with Java language? Should I serialize it as an object?
  2. jcsd
  3. Feb 18, 2005 #2
    Are u working on lan or internet?
    If its on lan , it is easy and no i dont think u need to serialize it. Use ObjectInputStream and ObjectOutputStream which does the job for u.

    -- AI
  4. Feb 18, 2005 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Any Java object must be serialized in order to leave the virtual machine in which it resides. But, as Tenali mentions, it's not something you invoke explicitly.
  5. Feb 18, 2005 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    So you want to copy a file from your disk, across the network to another computer, and put the file on its disk?

    You'll need to use a ServerSocket on one computer to create a server which can be connected to by the other computer. The other computer will use a normal Socket.

    You will have to use FileInputStream and FileOutputStream to read and write the files from the disk.

    This is a potentially complicated program, because you'll have to develop some kind of a protocol in which the computers can tell each other the names of the files, etc. If you want to be able to transfer files in both directions, you'll have to work out a protocol in which one computer can tell the other what to do, etc.

    You might want to consider implementing a subset of the File Transfer Protocol, http://www.w3.org/Protocols/rfc959/. If you do this, you'll be able to test and use your program with a large variety of FTP clients already available for free.

    - Warren
  6. Feb 19, 2005 #5
    No Warren I don't intend to build something as complicated as the FTP. I just thought I could transfer file objects with the ObjectOutputStream and ObjectInputStream from one machine to the other. Regarding the reading and writing of files I believe I can use the built in FileChooserDialog.
  7. Feb 19, 2005 #6
    Err he didnt mean that.
    For example when u have transferred a file to the other side, say for example the client would like to store it with the same name as on the server side , then u would have to pass the filename to the other side (we had to do this when we had implemented a small teaching tool at our college).

    -- AI
  8. Feb 19, 2005 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    This is not correct. You will need to involve actual networking classes, like Socket and ServerSocket, to accomplish moving files across a network.

    - Warren
  9. Feb 23, 2005 #8
    If it is on the LAN, and you can mount network drives, they its as easy as writing the file to the directory.

    If it is over the Internet however, you will need to user network sockets, and you will need a simple protocol. It shouldn't be too bad, however.
  10. Feb 23, 2005 #9
    What I mean is that I can define a much simpler protocol than the FTP, whithout the 'get', 'put', 'list', etc. etc. commands. Anyway I get your points.
  11. Feb 23, 2005 #10
    Actually another alternative is to use RMI, but that would probably more complicated than its worth.
  12. Feb 24, 2005 #11
    No, RMI doesn't work for file transfer.
  13. Feb 24, 2005 #12
  14. Feb 25, 2005 #13
    Servlets require a webserver(or a webserver-lite like Tomcat), which is probably more than he needs.

    Why couldn't you invoke a remote method on the host that will write out to a local file? Im not saying its the easiest way or anything(you have to set up an RMI registry on the host for one), but I don't see any reason why its not possible..
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Who knows networking with Java
  1. Java q (Replies: 3)

  2. Java problem. (Replies: 4)