ObjectOutputStream - NotSerializableException???
Вот здесь есть похожее http://cloud-cuckoo.com.ua/content/java-serializatsiya-rabota-s-objectoutputstream-objectinputstream
Кто сталкивался подскажите пож!!!
Пробую сделать многопоточный сервер. Пока все получается, но мне нужно передать на клиенты объект через который они будут общаться.
При попытке передачи такого объекта, я получаю NotSerializableException - что нужно сериализировать, зачем? Спасибо!
Код:
this.oOutputStream.writeObject(sendOutputStreamObject);
Клиент посылает название сервера главному серверу.
Код:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class CHClient{
private String serverName;
private final int PORT = 8888;
private ObjectInputStream oInputStream;
private ObjectOutputStream oOutputStream;
public CHClient(String serverName) throws IOException {
this.serverName = serverName;
Object[] inputStreamObject = null;
String oInputStreamParam1;
Object[] outputStreamObject = { this.serverName };
InetAddress addr = InetAddress.getByName(null);
System.out.println("addr = " + addr);
Socket socket = new Socket(addr, PORT);
try {
System.out.println("socket = " + socket);
this.oOutputStream = new ObjectOutputStream(socket.getOutputStream());
this.oOutputStream.writeObject(outputStreamObject);
this.oInputStream = new ObjectInputStream(socket.getInputStream());
inputStreamObject = (Object[]) this.oInputStream.readObject();
oInputStreamParam1 = inputStreamObject[0].toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("closing...");
socket.close();
}
}
}
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class CHClient{
private String serverName;
private final int PORT = 8888;
private ObjectInputStream oInputStream;
private ObjectOutputStream oOutputStream;
public CHClient(String serverName) throws IOException {
this.serverName = serverName;
Object[] inputStreamObject = null;
String oInputStreamParam1;
Object[] outputStreamObject = { this.serverName };
InetAddress addr = InetAddress.getByName(null);
System.out.println("addr = " + addr);
Socket socket = new Socket(addr, PORT);
try {
System.out.println("socket = " + socket);
this.oOutputStream = new ObjectOutputStream(socket.getOutputStream());
this.oOutputStream.writeObject(outputStreamObject);
this.oInputStream = new ObjectInputStream(socket.getInputStream());
inputStreamObject = (Object[]) this.oInputStream.readObject();
oInputStreamParam1 = inputStreamObject[0].toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("closing...");
socket.close();
}
}
}
Сервер смотрит если сервера с таким именем еще нет - создает и возвращает объект(должен возвращать), Все работает, но как только суну в Стрим объект типа ChessMultiServerThread - вылетает NotSerializableException!
Код:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ChessServer {
private static final int PORT = 8888;
private Socket socket;
private ServerSocket serverSocket = null;
private ArrayList<String> servers = new ArrayList<String>();
private boolean isServerExist;
private ObjectInputStream oInputStream;
private ObjectOutputStream oOutputStream;
public static void main(String[] args) throws IOException {
new ChessServer();
}
public ChessServer() {
Object[] inputStreamObject;
Object outputStreamServerNameObj = null;
Object outputStreamChMultiServObj = null;
String oInputStreamParam1;
System.out.println("Server Started");
try {
serverSocket = new ServerSocket(PORT);
do {
this.socket = serverSocket.accept();
this.oInputStream = new ObjectInputStream(socket.getInputStream());
inputStreamObject = (Object[]) this.oInputStream.readObject();
this.oOutputStream = new ObjectOutputStream(socket.getOutputStream());
oInputStreamParam1 = inputStreamObject[0].toString();
isServerExist = isServerExist(oInputStreamParam1);
if (!isServerExist) {
ChessMultiServerThread serverOne = new ChessMultiServerThread(socket);
outputStreamServerNameObj = new String("newServer");
outputStreamChMultiServObj = serverOne;
} else {
outputStreamServerNameObj = new String("serverExist");
}
Object[] sendOutputStreamObject = { outputStreamServerNameObj, outputStreamChMultiServObj };
this.oOutputStream.writeObject(sendOutputStreamObject);
} while (true);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
this.serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private boolean isServerExist(String serverName) {
boolean isExist = this.servers.contains(serverName);
System.out.println("isExist: " + isExist);
System.out.println("serverName: " + serverName);
if (isExist) {
return true;
} else {
this.servers.add(serverName);
return false;
}
}
}
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ChessServer {
private static final int PORT = 8888;
private Socket socket;
private ServerSocket serverSocket = null;
private ArrayList<String> servers = new ArrayList<String>();
private boolean isServerExist;
private ObjectInputStream oInputStream;
private ObjectOutputStream oOutputStream;
public static void main(String[] args) throws IOException {
new ChessServer();
}
public ChessServer() {
Object[] inputStreamObject;
Object outputStreamServerNameObj = null;
Object outputStreamChMultiServObj = null;
String oInputStreamParam1;
System.out.println("Server Started");
try {
serverSocket = new ServerSocket(PORT);
do {
this.socket = serverSocket.accept();
this.oInputStream = new ObjectInputStream(socket.getInputStream());
inputStreamObject = (Object[]) this.oInputStream.readObject();
this.oOutputStream = new ObjectOutputStream(socket.getOutputStream());
oInputStreamParam1 = inputStreamObject[0].toString();
isServerExist = isServerExist(oInputStreamParam1);
if (!isServerExist) {
ChessMultiServerThread serverOne = new ChessMultiServerThread(socket);
outputStreamServerNameObj = new String("newServer");
outputStreamChMultiServObj = serverOne;
} else {
outputStreamServerNameObj = new String("serverExist");
}
Object[] sendOutputStreamObject = { outputStreamServerNameObj, outputStreamChMultiServObj };
this.oOutputStream.writeObject(sendOutputStreamObject);
} while (true);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
this.serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private boolean isServerExist(String serverName) {
boolean isExist = this.servers.contains(serverName);
System.out.println("isExist: " + isExist);
System.out.println("serverName: " + serverName);
if (isExist) {
return true;
} else {
this.servers.add(serverName);
return false;
}
}
}
Сервер для коммуникации между клиентами.(Только набросок - логика еще не зашита)
Код:
import java.io.*;
import java.net.Socket;
public class ChessMultiServerThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChessMultiServerThread(Socket s) throws IOException {
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
start();
}
@Override
public void run() {
try {
// while (true) {
String str = in.readLine();
// System.out.println("Echoing: " + str);
out.println(str);
// }
} catch (IOException e) {
System.err.println("IO Exception");
} finally {
try {
socket.close();
} catch (IOException e) {
System.err.println("Socket not closed");
}
}
}
}
import java.net.Socket;
public class ChessMultiServerThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ChessMultiServerThread(Socket s) throws IOException {
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
start();
}
@Override
public void run() {
try {
// while (true) {
String str = in.readLine();
// System.out.println("Echoing: " + str);
out.println(str);
// }
} catch (IOException e) {
System.err.println("IO Exception");
} finally {
try {
socket.close();
} catch (IOException e) {
System.err.println("Socket not closed");
}
}
}
}