By using java how to listout sharing files in LAN...?

I want to know the all the sharing files when systems are connected in LAN......by using java program. Any one give me suggestion how it is possible.....?

Replies

  • ramdane
    ramdane
    please, give java code program, which list out us result, all sharing files in lan
  • Morningdot Hablu
    Morningdot Hablu
    May be there are some classes and interfaces by which you can do your work in easy way.
    But if you come to basics you can do it using rmi and socket programming.Just open your file streams on the network(from all the computers) using io package and store it in another where you want to display the files on the lan.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    We can use a Client - Server script... Try this... Not mine, got it in the net...

    Server Class (Server.java)
    package transfer;
    
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server extends Thread {
        public static final int PORT = 3333;
        public static final int BUFFER_SIZE = 100;
    
        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(PORT);
    
                while (true) {
                    Socket s = serverSocket.accept();
                    saveFile(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void saveFile(Socket socket) throws Exception {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            FileOutputStream fos = null;
            byte [] buffer = new byte[BUFFER_SIZE];
    
            // 1. Read file name.
            Object o = ois.readObject();
    
            if (o instanceof String) {
                fos = new FileOutputStream(o.toString());
            } else {
                throwException("Something is wrong");
            }
    
            // 2. Read file to the end.
            Integer bytesRead = 0;
    
            do {
                o = ois.readObject();
    
                if (!(o instanceof Integer)) {
                    throwException("Something is wrong");
                }
    
                bytesRead = (Integer)o;
    
                o = ois.readObject();
    
                if (!(o instanceof byte[])) {
                    throwException("Something is wrong");
                }
    
                buffer = (byte[])o;
    
                // 3. Write data to output file.
                fos.write(buffer, 0, bytesRead);
            } while (bytesRead == BUFFER_SIZE);
    
            fos.close();
    
            ois.close();
            oos.close();
        }
    
        public static void throwException(String message) throws Exception {
            throw new Exception(message);
        }
    
        public static void main(String[] args) {
            new Server().start();
        }
    }
    Client Class (Client.java)
    package transfer;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.util.Arrays;
    
    public class Client {
        public static void main(String[] args) throws Exception {
            String fileName = null;
    
            try {
                fileName = args[0];
            } catch (Exception e) {
                System.out.println("Pass file name as command line argument");
            }
    
            File file = new File(fileName);
            Socket socket = new Socket("localhost", Server.PORT);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    
            oos.writeObject(file.getName());
    
            FileInputStream fis = new FileInputStream(file);
            byte [] buffer = new byte[Server.BUFFER_SIZE];
            Integer bytesRead = 0;
    
            while ((bytesRead = fis.read(buffer)) > 0) {
                oos.writeObject(bytesRead);
                oos.writeObject(Arrays.copyOf(buffer, buffer.length));
            }
    
            oos.close();
            ois.close();
        }
    }

You are reading an archived discussion.

Related Posts

i am an electronics engg. as my percentage is less(54%), i didnt get campus placement. pls suggest some certification courses which will help me to get a job in electronics...
Hi All,I'm doing 3rd computer science in SIET. I would like to do In Plant Training in chennai. Can anyone suggest some good companies that offer IPT?... Thanx in advance
An engineering student from Mumbai who likes anonymity …i njoy chatting i m really intrested to know how things work….how they were made what was the need……making new things is...
Hey Godfather I am currently working on a project controlling industrial hazards using GSM intimation and feedback..... has someone worked on a smilar project...... thanks in advance bro.... i have...
RoboEarth is a Crazy Engineering Idea about creating a HUGE network with HUGE database for Robots. It will be place where robots can share data and learn from each other...