Uczę się programowani sieciowegio i znaklazłem ciekawy tuturial do Syna na temat alternatywy dla RMi,czyli serializowanie obiektów,i przesyłanie je między socketamiu przez strumien obiektow.Wszystko byloby ok,gdyby nie fakt,że kod ze strony Suna nie działa u mmnie.tznkompiluje się serwer widzi klienta i klapa,nie moge odczytac obiektów ze strumienia.Może ktoś pomóc?
Oto kodY
Klient:
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());
}
}
}
Serwer:
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) {}
}
}
Zserializowana klasa
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;
}
}
Kody żywce z tuta Suna z e strony<url>V</url>
Z gory dziekuje za pomoc!