PROGRAMOWANIE W ŚRODOWISKU SIECIOWYM I


Serializacja obiektów

 

 

Code Sample 1: SaveDate.java

import java.io.*;

import java.util.Date;

 

public class SaveDate {

 

  public static void main(String argv[]) throws Exception {

    FileOutputStream fos = new FileOutputStream("date.out");

    ObjectOutputStream oos = new ObjectOutputStream(fos);

    Date date = new Date();

    oos.writeObject(date);

    oos.flush();

    oos.close();

    fos.close();

  }

}


 Code Sample 2: ReadDate.java

 

import java.io.*;

import java.util.Date;

 

public class ReadDate {

 

  public static void main(String argv[]) throws Exception {

    FileInputStream fis = new FileInputStream("date.out");

    ObjectInputStream ois = new ObjectInputStream(fis);

    Date date = (Date) ois.readObject();

    System.out.println("The date is: "+date);

    ois.close();

    fis.close();

  }

}



Code Sample 3: UserInfo.java

import java.io.*;

import java.util.*;

 

public class UserInfo implements Serializable {

   String name = null;

 

   public UserInfo(String name) {

      this.name = name;

   }

 

   public void printInfo() {

      System.out.println("The name is: "+name);

   }

}


Code Sample 4: SaveInfo.java

import java.io.*;

import java.util.Date;

 

public class SaveInfo {

 

  public static void main(String argv[]) throws Exception {

    FileOutputStream fos = new FileOutputStream("name.out");

    ObjectOutputStream oos = new ObjectOutputStream(fos);

    // create two objects

    UserInfo user1 = new UserInfo("Java Duke");

    UserInfo user2 = new UserInfo("Java Blue");

    // write the objects to the output stream

    oos.writeObject(user1);

    oos.writeObject(user2);

    oos.flush();

    oos.close();

    fos.close();

  }

}


Code Sample 5: ReadInfo.java

import java.io.*;

import java.util.Date;

 

public class ReadInfo {

 

  public static void main(String argv[]) throws Exception {

    FileInputStream fis = new FileInputStream("name.out");

    ObjectInputStream ois = new ObjectInputStream(fis);

    // read the objects from the input stream (the file name.out)

    UserInfo user1 = (UserInfo) ois.readObject();

    UserInfo user2 = (UserInfo) ois.readObject();

    // invoke a method on the constructed object

    user1.printInfo();

    user2.printInfo();

    ois.close();

    fis.close();

  }

}



 

 

Code Sample 6: DateServer.java

import java.io.*;

import java.net.*;

import java.util.*;

 

public class DateServer extends Thread {

 

   private ServerSocket dateServer;

 

 

   public static void main(String argv[]) throws Exception {

     new DateServer();

   }

 

   public DateServer() throws Exception {

     dateServer = new ServerSocket(3000);

     System.out.println("Server listening on port 3000.");

     this.start();

   }

 

   public void run() {

     while(true) {

       try {

        System.out.println("Waiting for connections.");

        Socket client = dateServer.accept();

        System.out.println("Accepted a connection from: "+

client.getInetAddress());

        Connect c = new Connect(client);

       } catch(Exception e) {}

     }

   }

}

 

class Connect extends Thread {

   private Socket client = null;

   private ObjectInputStream ois = null;

   private ObjectOutputStream oos = null;

   

   public Connect() {}

 

   public Connect(Socket clientSocket) {

     client = clientSocket;

     try {

      ois = new ObjectInputStream(client.getInputStream());

      oos = new ObjectOutputStream(client.getOutputStream());

     } catch(Exception e1) {

         try {

            client.close();

         }catch(Exception e) {

           System.out.println(e.getMessage());

         }

         return;

     }

     this.start();

   }

 

 

   public void run() {

      try {

         oos.writeObject(new Date());

         oos.flush();

         // close streams and connections

         ois.close();

         oos.close();

         client.close();

      } catch(Exception e) {}      

   }

}

 

Code Sample 7: DateClient.java

import java.io.*;

import java.net.*;

import java.util.*;

 

public class DateClient {

   public static void main(String argv[]) {

      ObjectOutputStream oos = null;

      ObjectInputStream ois = null;

      Socket socket = null;

      Date date = null;

      try {

        // open a socket connection

        socket = new Socket("IP", 3000);

        // open I/O streams for objects

        oos = new ObjectOutputStream(socket.getOutputStream());

        ois = new ObjectInputStream(socket.getInputStream());

        // read an object from the server

        date = (Date) ois.readObject();

        System.out.print("The date is: " + date);

        oos.close();

        ois.close();

      } catch(Exception e) {

        System.out.println(e.getMessage());

      }

   }

}


Code Sample 8: SerializedObject.java

import java.io.*;

import java.util.*;

 

public class SerializedObject implements Serializable {

   private int array[] = null;

 

   public SerializedObject() {

   }

 

   public void setArray(int array[]) {

     this.array = array;

   }

 

   public int[] getArray() {

     return array;

   }

}


Code Sample 9: ArrayClient.java

import java.io.*;

import java.net.*;

 

public class ArrayClient {

   public static void main(String argv[]) {

      ObjectOutputStream oos = null;

      ObjectInputStream ois = null;

      // two arrays

      int dataset1[] = {3, 3, 3, 3, 3, 3, 3};

      int dataset2[] = {5, 5, 5, 5, 5, 5, 5};

      try {

        // open a socket connection

        Socket socket = new Socket("YourMachineNameORipAddress", 4000);

        // open I/O streams for objects

        oos = new ObjectOutputStream(socket.getOutputStream());

        ois = new ObjectInputStream(socket.getInputStream());

        // create two serialized objects

        SerializedObject so1 = new SerializedObject();

        SerializedObject so2 = new SerializedObject();

        SerializedObject result = null;

        int outArray[] = new int[7];

        so1.setArray(dataset1);

        so2.setArray(dataset2);

        // write the objects to the server

        oos.writeObject(so1);

        oos.writeObject(so2);

        oos.flush();

        // read an object from the server

        result = (SerializedObject) ois.readObject();

        outArray = result.getArray();

        System.out.print("The new array is: ");

        // after unpacking the array, iterate through it

        for(int i=0;i<outArray.length;i++) {

          System.out.print(outArray[i] + "   ");

        }

        oos.close();

        ois.close();

      } catch(Exception e) {

        System.out.println(e.getMessage());

      }

   }

}



Code Sample 10: ArrayMultiplier

import java.io.*;

import java.net.*;

 

public class ArrayMultiplier extends Thread {

 

   private ServerSocket arrayServer;

 

 

   public static void main(String argv[]) throws Exception {

     new ArrayMultiplier();

   }

 

   public ArrayMultiplier() throws Exception {

     arrayServer = new ServerSocket(4000);

     System.out.println("Server listening on port 4000.");

     this.start();

   }

 

   public void run() {

     while(true) {

       try {

        System.out.println("Waiting for connections.");

        Socket client = arrayServer.accept();

        System.out.println("Accepted a connection from: "+

client.getInetAddress());

        Connect c = new Connect(client);

       } catch(Exception e) {}

     }

   }

}

 

class Connect extends Thread {

   private Socket client = null;

   private ObjectInputStream ois = null;

   private ObjectOutputStream oos = null;

   

   public Connect() {}

 

   public Connect(Socket clientSocket) {

     client = clientSocket;

     try {

      ois = new ObjectInputStream(client.getInputStream());

      oos = new ObjectOutputStream(client.getOutputStream());

     } catch(Exception e1) {

         try {

            client.close();

         }catch(Exception e) {

           System.out.println(e.getMessage());

         }

         return;

     }

     this.start();

   }

 

   public void run() {

      SerializedObject x = null;

      SerializedObject y = null;

      int dataset1[] = new int[7];

      int dataset2[] = new int[7];

      int result[] = new int[7];

      try {

         x = (SerializedObject) ois.readObject();

         y = (SerializedObject) ois.readObject();

         dataset1 = x.getArray();

         dataset2 = y.getArray();

         // create an array by multiplying two arrays

         for(int i=0;i<dataset1.length;i++) {

           result[i] = dataset1[i] * dataset2[i];

         }

         // ship the object to the client

         SerializedObject output = new SerializedObject();

         output.setArray(result);

         oos.writeObject(output);

         oos.flush();

         // close connections

         ois.close();

         oos.close();

         client.close();

      } catch(Exception e) {}      

   }

}



Materiał:

 

http://java.sun.com/developer/technicalArticles/ALT/sockets/

 

 

ZADANIE

 

1) Posługując się programem klient-serwer z pierwszego ćwiczenia, zaimplementować przesyłanie poleceń poprzez obiekt klasy Command, która powinna zostać serializowana tak jak jest podane w przykladzie  6 i 7. Klasa Command powinna implementować interfejs Serializabe (przykład 8).

 

Przykladowe pola klasy Command to: ID, NAME, CMD, DATA (nie implemntować pola klasy File).

 

2) Zaimplementować nowe polecenie file, zawierające nazwę pliku,który ma zostac przesłany z serwera do klienta. Plik powinien być przesyłany w polu DATA (typ String).

 

3) Zaimplemntować dodatkowo polecneie DIR, przesyłające nazwykatalogów i pików z katalogu roboczego.