NetworkingSerializat..

advertisement
Networking Serialization
CSCI 201L
Jeffrey Miller, Ph.D.
HTTP://WWW-SCF.USC.EDU/~CSCI201
USC CSCI 201L
Outline
▪ Network Serialization
▪ Program
USC CSCI 201L
2/10
Serialization
▪ Recall that serialization allows us to write objects to
an output stream and read objects from an input
stream
▪ We have already seen serialization with file I/O
▪ This is shown on the following slides
Network Serialization
USC CSCI 201L
3/10
Serialization with File I/O
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.Serializable;
public class Employee implements Serializable {
public static final long serialVersionUID = 1;
private String fname, lname;
private transient String password;
public Employee(String fname, String lname, String password) {
this.fname = fname;
this.lname = lname;
this.password = password;
}
public void printEmployee() {
System.out.println(fname + " " + lname + ": " + password);
}
}
Network Serialization
USC CSCI 201L
4/10
Serialization with File I/O
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import
import
import
import
import
import
java.io.FileInputStream;
java.io.FileNotFoundException;
java.io.FileOutputStream;
java.io.IOException;
java.io.ObjectInputStream;
java.io.ObjectOutputStream;
public class EmployeeMain {
public static void main(String [] args) {
Employee emp = new Employee("donald", "trump", "billionaire");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out.txt"));
oos.writeObject(emp);
oos.flush();
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out.txt"));
Employee emp1 = (Employee)ois.readObject();
ois.close();
emp.printEmployee();
emp1.printEmployee();
} catch (FileNotFoundException fnfe) {
System.out.println("fnfe: " + fnfe.getMessage());
} catch (IOException ioe) {
System.out.println("ioe: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
}
}
}
Network Serialization
USC CSCI 201L
5/10
Network Serialization
▪ With network communication, we are also able to
transmit objects back and forth between two
different programs over a Socket
▪ This works almost exactly the same as serialization
with file I/O, but instead of a FileInputStream and
FileOutputStream, we will use the input stream and
output stream associated with the Socket
Network Serialization
USC CSCI 201L
6/10
Serialization with Server Networking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*; // shortened for space
import java.net.*; // shortened for space
public class NetworkSerializationServer {
private ServerSocket ss = null;
private Socket s = null;
public NetworkSerializationServer() {
try {
ss = new ServerSocket(6789);
s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Employee emp1 = (Employee)ois.readObject();
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(emp1);
oos.flush();
oos.close();
ois.close();
} catch (IOException ioe) {
System.out.println("ioe: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
if (s != null && ss !- null) {
try {
s.close();
ss.close();
} catch (IOException ioe) {
System.out.println("IOE closing socket: " + ioe.getMessage());
}
}
}
}
public static void main(String [] args) {
new NetworkSerializationServer();
}
}
Network Serialization
USC CSCI 201L
7/10
Serialization with Client Networking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.*; // shortened for space
import java.net.Socket;
public class NetworkSerializationClient {
private Socket s = null;
public NetworkSerializationClient() {
Employee emp = new Employee("donald", "trump", "billionaire");
try {
s = new Socket("localhost", 6789);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(emp);
oos.flush();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Employee emp1 = (Employee)ois.readObject();
oos.close();
ois.close();
System.out.println("Employee sent");
emp.printEmployee();
System.out.println("Employee received");
emp1.printEmployee();
} catch (IOException ioe) {
System.out.println("ioe: " + ioe.getMessage());
} catch (ClassNotFoundException cnfe) {
System.out.println("cnfe: " + cnfe.getMessage());
} finally {
if (s != null) {
try {
s.close();
} catch (IOException ioe) {
System.out.println("IOE closing socket: " + ioe.getMessage());
}
}
}
}
public static void main(String [] args) {
new NetworkSerializationClient();
}
}
Network Serialization
USC CSCI 201L
8/10
Outline
▪ Network Serialization
▪ Program
USC CSCI 201L
9/10
Program
▪ Modify the chat client and chat server program to pass
serialized objects instead of strings. The serialized object
should contain the IP address, port, and String that is being
sent. The output should then contain the IP and port.
C:>java ChatClient localhost 6789
hello
192.168.1.2:56450: how are you?
fine, and you?
192.168.1.2:56450: good, thanks
C:>java ChatClient localhost 6789
192.168.1.3:56451: hello
how are you?
192.168.1.3:56451: fine, and you?
good, thanks
Program
USC CSCI 201L
10/10
Download