Results 1 to 17 of 17
  1. #1
    blueprats is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default Access remote machine files

    Hello,
    I am trying some java io programs, even I have done some of them and they run very well. I was thinking about accessing the files on remote machine, but unfortunately I do not have an idea to do it. Which classes I have to use to access remote machine file. I was trying using the Socket class but I could not do it (there are four computers connected to each other through LAN - no internet connection) Now, Socket requires an port number, for communication between two computers (without internet connection) which is the port number to use? If you have any ideas then please do post back. Hoping for a reply soon.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah, I've done a bunch of this.
    One way to do it is to make a 'client' and a 'server' app. The server runs on the machine that you want to retrieve files from, and the client is running anywhere else.

    You use the machine's actual IP address for communication (so if its a lan, the client might be 192.168.1.100 and the server might be 192.168.1.150).

    As far as port number, the number doesn't matter as long as the client and server both use the same port. However, you should use something greater than 1024 just to be safe, since most well known port numbers are between 1 and 1023.

    On the sever, you create a ServerSocket that listens on a port. On the client, you create an ordinary Socket that takes the ip of the server and the port number the server is listening to. Once a Socket connection is established, both sides can create buffered reader/writers to send bytes/strings back and forth, or ObjectOutputStreams/ObjectInputStreams to send object back and forth. The latter is my favorite method.

    I have tons of sample code, but peek into the API first. The client and server combined can be written in about 100 lines or less.

  3. #3
    blueprats is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Hey,
    Thanks for the reply, I have done client server programs in java, what I am asking is how to access or view files which are on remote machine. I do not want to create a file which will run on remote machine. I just want a single file which will be running on my computer and will access all the files and directories of the remote computer. Just like you access other computers through "My Network Places" in Windows. Is this possible, which classes should I use for it.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    yeah, well, it is possible to read a remote file from the local machine with the method I described - but if you are trying to avoid writing the file locally, you have two scenarios:
    1) The remote file is small and you can hold the entire file in memory on the local machine. Copy the remote file into memory and then do what you need to do.

    2) The remote file is huge and does not fit in local memory. This is a nasty one. You basically need to implement some form of streaming which allows seeking on the remote machine. Doable, but tricky.

    What exactly are you trying to access? What type of file and what are you doing with it? Remember that on a computer system with a network browser, you are actually connecting to a remote file system and there is a complex underlying driver that allows your OS to think its working with a local file. Are you attempting to implement all that in java? Because I don't recommend it.

    However, if for example you have a DVD database on the remote computer that you would like to access, I would attempt either copying it locally (very simple) and then sending it back if needed (to save changes) or attempt the all-in-memory idea I mention in 1).

    I do not want to create a file which will run on remote machine. I just want a single file which will be running on my computer and will access all the files and directories of the remote computer...
    You're using the word 'file' here a lot. Don't be vague with your terminology - did you mean to say "I don't want a server side program running on the remote computer, I just want to browse its contents from my client."? Because in that case, you're opening a can of worms.

    There are ways to do that, but it would mean writing a full fledge standards-compliant client for some networking protocol running on the remote machine (like FTP, SMB, SSH, etc...). This is MUCH harder to do and would give most grad students a run for the money. I have written an FTP client from scratch which would connect to remote FTP servers, (it was a chore!).

    Windows networking uses a client/server system where every machine is a client and can also be a server (hence file sharing). Windows uses the SMB protocol for networking, so to interact with it, you would need to implement a SMB client.

    Keep it simple, write a server side app that can send files back to the client via TCP.

  5. #5
    blueprats is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Hey,
    I get the point, I understand that the client has to response to the server, in the way the server is defined. I was just asking that can I view the files on the remote machine which is connected in LAN. I use the ServerSocket / Socket for doing that, but the problem is the port number for me, I have the ip address, but which is the port number I have to include to view the files of the remote machine. In addition, if you know some protocols which are used to access or view the remote machine, then please let me know. By the way thanks for the wonderful information you provided.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by blueprats View Post
    Hey,
    Thanks for the reply, I have done client server programs in java, what I am asking is how to access or view files which are on remote machine. I do not want to create a file which will run on remote machine. I just want a single file which will be running on my computer and will access all the files and directories of the remote computer. Just like you access other computers through "My Network Places" in Windows. Is this possible, which classes should I use for it.
    If I'm not mistaken you can simply use the UNC (Universal Naming Convention) name and treat it as a File from there on. All those Windows machines know how to use that.

    kind regards,

    Jos

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Am I missing something here or are you missing something? As far as I know, you cannot connect to another machine on a LAN without some form of networking protocol. If you are using windows file sharing as an example, then my SMB argument stands. The port number will be the port number of whatever service you are trying to connect to.
    FTP is 21
    SSH is 22
    Telnet is 23
    Windows File Sharing is 445

    You cannot simply connect to a computer on a LAN and view its files if it is not running some kind of service like what I mentioned above. If it is running a service, you use the port number of that service. But you cannot simply open a socket to an ftp server or windows file sharing computer and expect it to work. These services follow a protocol which involves a lot more than just opening a TCP socket, hence why I said this is all WAY easier if you make a server that find files for you in java, put in on said comupter, run it and then let your remote machines (machines on the LAN) connect to it and ask for files to view.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Which brings me to another much simpler idea. Why not just mount the remote volume locally on your client machine and then just use file objects? That eliminates all networking in java.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by quad64bit View Post
    Which brings me to another much simpler idea. Why not just mount the remote volume locally on your client machine and then just use file objects? That eliminates all networking in java.
    Also see reply #6; those MS Windows machines have those services running if you 'share' your directories.

    kind regards,

    Jos

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    MS Windows machines have those services running if you 'share' your directories.
    Yes I know - so I pose this question to you (Jos) - If I have a windows box with file sharing enabled and shared folders set up, and I create a java socket using the UNC as a url to the file and the proper port number (I'm guessing 445?) and then get the input stream from that socket, it will allow me to read that file directly?

    In other words, windows file sharing (without logging in) will listen to a TCP socket request, establish a connection (3 way handshake, etc) and then serve data to that port without any special setup at all? This is what your post is saying to me, but I find that hard to believe; since this would allow me to log into ANY windows file share anywhere with 5 lines of code and proceed to dump the entire shared directory.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Basically, you're saying this would actually work? :
    Java Code:
    public class FileShare {
        public FileShare(){
            try {
                Socket socket = new Socket("\\\\ComputerName\\SharedFolder\\Resource", 445);
                InputStream in = socket.getInputStream();
                while(true){
                    int _byte = in.read();
                    if(_byte == -1)
                        break;
                    System.out.println(_byte);
                }
                
            } catch (UnknownHostException ex) {
                Logger.getLogger(FileShare.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FileShare.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by quad64bit View Post
    Basically, you're saying this would actually work? :
    Java Code:
    public class FileShare {
        public FileShare(){
            try {
                Socket socket = new Socket("\\\\ComputerName\\SharedFolder\\Resource", 445);
                InputStream in = socket.getInputStream();
                // snip ...
    That isn't even needed (and it won't work either ;-) on an MS Windows machine you can simply do this:

    Java Code:
    File file= new File("\\\\<machine>\\pathname");
    InputStream in= new FileInputStream(file);
    kind regards,

    Jos

  13. #13
    blueprats is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default

    Hey guys,
    Thanks for your reply, I will try this today. And yes, I know a protocol is required to communicate between two machines. Thanks again of your help.

  14. #14
    afreaz is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    hi guys i hav the same problem am totally new to java..
    all i want to do is access lan connected systems n there files after connecting to it via lan n display it through a browse button

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by afreaz View Post
    hi guys i hav the same problem am totally new to java..
    all i want to do is access lan connected systems n there files after connecting to it via lan n display it through a browse button
    Did you actually read this thread?

    kind regards,

    Jos

  16. #16
    afreaz is offline Member
    Join Date
    Feb 2010
    Posts
    19
    Rep Power
    0

    Default

    ya i read the thread n i actually hav the same problem..
    so asked..
    n i also serched for jfilechooser remote access method which i could not understand

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by afreaz View Post
    ya i read the thread n i actually hav the same problem..
    so asked..
    n i also serched for jfilechooser remote access method which i could not understand
    You should start your own thread if you have a question rather than hijack someone else's. In your new thread, you should note this thread here and also anything specifically about the recommendations above that you don't understand, or have tried and failed (with your code). I'm locking this thread.

Similar Threads

  1. Replies: 10
    Last Post: 02-28-2012, 09:44 PM
  2. Java program to ping remote machine using IP address..?
    By prabhurangan in forum New To Java
    Replies: 19
    Last Post: 11-22-2011, 04:58 PM
  3. Transfer a process to remote machine
    By sushil in forum Networking
    Replies: 6
    Last Post: 12-27-2009, 03:28 AM
  4. How do i share one process or service from remote machine in p2p
    By vishal.babar07@gmail.com in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 07:18 AM
  5. how to load a file in remote machine
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •