In Code Sample: How to transport your own custom objects ?
An Introduction to Socket Programming
(Continued from previous question...)
In Code Sample: How to transport your own custom objects ?
In this example, 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.
Start by making the class, whose objects will be
transportable over sockets, serializable by implementing
the Serializable interface as shown in Code Sample
Code Sample : 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;
}
}
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.
Code Sample: 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());
}
}
}
Now we need to develop the server, ArrayMultiplier .
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.
Code Sample : 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) {}
}
}
To run this example, modify the ArrayClient source
specifying the machine name or IP address where the ArrayMultiplier
server will run.
If you run the ArrayMultiplier and ArrayClient
successfully, you should get the output:
The new array is: 15 15 15 15 15 15 15
(Continued on next question...)
Other Interview Questions
|