Advanced Socket Programming Politeknik Elektronika Negeri Surabaya PENSITS
Advanced Socket Programming Politeknik Elektronika Negeri Surabaya PENS-ITS 1
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 2
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 3
Object Serialization • Ada dua class utama yang digunakan untuk membaca dan menulis objek untuk stream yaitu Object. Output. Stream dan Object. Input. Stream. • Object. Output. Stream menyediakan fungsi write. Object untuk menulis sebuah objek ke output stream • Object. Input. Stream menyediakan fungsi read. Object untuk membaca objek dari input stream. • Penting !!! Bahwa objek yang digunakan dengan fungsi ini harus diserialisasikan. Artinya, class harus mengimplementasikan interface Serializable. PENS-ITS 4
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 5
Serializing Existing Classes • Membaca serialized object dan mencetak informasi object tsb. PENS-ITS 6
Serializing Existing Classes • Untuk mengetahui apakah sebuah class adalah serializable, gunakan tool serialver yang terdapat di JDK. • Menggunakan GUI PENS-ITS 7
Serializing Existing Classes • shows that the Socket class is not serializable. PENS-ITS 8
Serializing Custom Classes • how to serialize a custom class. In this example, we create a custom class, User. Info PENS-ITS 9
Serializing Custom Classes • The next step is to create a class that creates a instance of the User. Info 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 write. Object method can be called any number of times to write any number of objects to the output stream. PENS-ITS 10
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 write. Object, the read. Object method can be called any number of times to read any number of objects from the input stream. PENS-ITS 11
Serializing Custom Classes • To try out this example, compile the source files: User. Info. java, Save. Info. java, and Read. Info. java. Run Save. Info, then Read. Info, and you would see some output similar to this: PENS-ITS 12
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 13
Transporting an existing object • Here we develop a multi-threaded Date. Server 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. D 4 PENS-ITS 14
PENS-ITS 15
Transporting an existing object • Note: the Date. Server is a multi-threaded server that is implemented by inheriting from the Thread class. Another approach to developing multithreaded servers is to implement the Runnable interface instead (inheritance vs. composition). PENS-ITS 16
Transporting an existing object • The client, Date. Client, does not have to send any messages to the Date. Server 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 17
Transporting an existing object PENS-ITS 18
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 19
Transporting Custom Objects PENS-ITS 20
Transporting Custom Objects • The next step is to develop the client. In this example, the client creates two instances of Serialized. Object and writes them to the output stream (to the server), as shown from the source code in Code Sample 9. PENS-ITS 21
Transporting Custom Objects PENS-ITS 22
Transporting Custom Objects • Now we need to develop the server, Array. Multiplier. 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 Array. Multiplier is shown in Code Sample 10. PENS-ITS 23
Transporting Custom Objects D 4 PENS-ITS 24
Transporting Custom Objects PENS-ITS 25
PENS-ITS 26
Transporting Custom Objects • To run this example, modify the Array. Client source specifying the machine name or IP address where the Array. Multiplier 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 Serialized. Object 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 Serialized. Object 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 Array. Multiplier and Array. Client successfully, you should get the output: • The new array is: 15 15 PENS-ITS 27
- Slides: 27