1 / 27

Advanced Socket Programming

Advanced Socket Programming. Yuliana Setiowati Politeknik Elektronika Negeri Surabaya. Topik. Memberikan gambaran singkat tentang object serialization Menunjukkan cara kerja dengan object serialization Menggambarkan cara kerja dengan obyek yang sudah ada dan objek yang dibuat sendiri

melliot
Download Presentation

Advanced Socket Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Socket Programming Yuliana Setiowati Politeknik Elektronika Negeri Surabaya PENS-ITS

  2. Topik • Memberikan gambaran singkat tentang object serialization • Menunjukkan cara kerja dengan object serialization • Menggambarkan cara kerja dengan obyek yang sudah ada dan objek yang dibuat sendiri • Menunjukkan bagaimana untuk mengirim objek melalui socket • Memberikan contoh multi-threaded server PENS-ITS

  3. Object Serialization • Object Serialization adalah suatu mekanisme dalam program untuk menyimpan state object ke sebuah file dan selanjutnya membaca kembali object tersebut untuk mengubah state object tsb atau mengirim object melalui jaringan menggunakan socket. • Class yang diserialisasi dapat dilakukan dengan membuat class yang mengimplementasikan interface java.io.Serializable. • Interface ini tidak mempunyai fungsi yang harus diimplementasikan, hanya sebagai interface penanda saja (a maker interface). • Interface ini digunakan untuk memberi informasi ke Java virtual machine (JVM) bahwa object ini diserialisasi. PENS-ITS

  4. Object Serialization • Ada dua class utama yang digunakan untuk membaca dan menulis objek untuk stream yaitu ObjectOutputStream dan ObjectInputStream. • ObjectOutputStream menyediakan fungsi writeObject untuk menulis sebuah objek ke output stream • ObjectInputStream menyediakan fungsi readObject untuk membaca objek dari input stream. • Penting !!! Bahwa objek yang digunakan dengan fungsi ini harus diserialisasikan. Artinya, class harus mengimplementasikan interface Serializable. PENS-ITS

  5. Serializing Existing Classes • Sekarang kita lihat bagaimana cara membaca / menulis obyek, atau object dari class yang tersedia, yang sudah mengimplementasikan interface Serializable. • Class Date dapat diserialisasikan, artinya class ini telah mengimplementasikan interface Serializable. PENS-ITS

  6. Serializing Existing Classes • Membaca serialized object dan mencetak informasi object tsb. PENS-ITS

  7. Serializing Existing Classes • Untuk mengetahui apakah sebuah class adalah serializable, gunakan tool serialver yang terdapat di JDK. • Menggunakan GUI PENS-ITS

  8. Serializing Existing Classes • shows that the Socket class is not serializable. PENS-ITS

  9. Serializing Custom Classes • how to serialize a custom class. In this example, we create a custom class, UserInfo PENS-ITS

  10. Serializing Custom Classes • The next step is to create a class that creates a instance of the UserInfo class and writes the object to an output stream as shown in Code Sample 4. The output stream in this example is a file called "name.out". The important thing to note from Code Sample 4 is that the writeObject method can be called any number of times to write any number of objects to the output stream. PENS-ITS

  11. Serializing Custom Classes • Finally, we write a class that reads the objects that have been saved, and invokes a method as shown in Code Sample 5. Again, as with writeObject, the readObject method can be called any number of times to read any number of objects from the input stream. PENS-ITS

  12. Serializing Custom Classes • To try out this example, compile the source files: UserInfo.java, SaveInfo.java, and ReadInfo.java. Run SaveInfo, then ReadInfo, and you would see some output similar to this: PENS-ITS

  13. Transporting Objects over Sockets • Now that we have seen how to write and read objects to/from I/O streams in a single process, let's see how to transport objects over sockets. First, we will see how to transport existing object (such as the Date object), then we will see how to transport custom objects. PENS-ITS

  14. Transporting an existing object • Here we develop a multi-threaded DateServer that listens on port 3000 and waits for requests from clients. Whenever there is a request, the server replies by sending a Date object (over sockets) to the client as shown in Code Sample 6. D4 PENS-ITS

  15. PENS-ITS

  16. Transporting an existing object • Note: the DateServer is a multi-threaded server that is implemented by inheriting from the Thread class. Another approach to developing multi-threaded servers is to implement the Runnable interface instead (inheritance vs. composition). PENS-ITS

  17. Transporting an existing object • The client, DateClient, does not have to send any messages to the DateServer once a connection has been established. It simply receives a Date object that represents the current day and time of the remote machine. The client receives the object and prints the date as shown in Code Sample 7. PENS-ITS

  18. Transporting an existing object PENS-ITS

  19. Transporting Custom Objects • In the previous example, we have worked with existing objects. What if you want to transport your own custom objects. Is the process different? • In this example, we write an array multiplier server. The client sends two objects, each representing an array; the server receives the objects, unpack them by invoking a method and multiplies the arrays together and sends the output array (as an object) to the client. The client unpacks the array by invoking a method and prints the new array. • We start by making the class, whose objects will be transportable over sockets, serializable by implementing the Serializable interface as shown in Code Sample 8. PENS-ITS

  20. Transporting Custom Objects PENS-ITS

  21. Transporting Custom Objects • The next step is to develop the client. In this example, the client creates two instances of SerializedObject and writes them to the output stream (to the server), as shown from the source code in Code Sample 9. PENS-ITS

  22. Transporting Custom Objects PENS-ITS

  23. Transporting Custom Objects • Now we need to develop the server, ArrayMultiplier. This server is similar to Code Sample 6. The only difference is in the processing. In this example, the server receives two objects, unpacks them and then multiplies the arrays together and finally sends the output as an object to the client. The ArrayMultiplier is shown in Code Sample 10. PENS-ITS

  24. Transporting Custom Objects D4 PENS-ITS

  25. Transporting Custom Objects PENS-ITS

  26. PENS-ITS

  27. Transporting Custom Objects • To run this example, modify the ArrayClient source specifying the machine name or IP address where the ArrayMultiplier server will run. Note that if you wish to run the server and client of this particular example on two different machines then both machines must have a copy of the SerializedObject class. This breaks information hiding and force tight coupling. A solution to this problem would be to write an interface that extends the Serializable interface and then have the SerializedObject class in Code Sample 8 implement the new interface. Using this technique, you only need to provide copies of the interface to the client and server, but not implementation. • If you run the ArrayMultiplier and ArrayClient successfully, you should get the output: • The new array is: 15 15 15 15 15 15 15 PENS-ITS

More Related