when I try join with multiple clients to my server, I get the error "java.io.StreamCorruptedException: invalid type code: AC" at line 60 in ClientThread class.
I'm trying to broadcast whatever object each client is sending, to all the clients. In this case I'm just sending an object with 1 variable "x" with the value of 5. I want to make it so that when a client joins, it sends the object to all clients connected to the server. Right now it works when I just join with 1 client, it connects and the value gets sent back to the client.
Whenever I join with a second client, it will give me the error on first client that joined. I've done some research and saw that it's due to having multiple outputstreams or something like that, but I couldn't find out how to apply a solution to my problem :(
I'm really lost at this point and have no idea where else to go but try get help here.
I really appreciate it if anyone can help me as it's really frustrating not being able to move on with my code because of this not working.
Here's the classes:
Server:
public class Server {
protected ServerSocket serverSocket = null;
protected boolean listening = true;
protected List<Socket> clientSockets;
public Server(int port) throws IOException {
serverSocket = new ServerSocket(4444);
clientSockets = new ArrayList<>();
while (listening) {
Socket clientSocket = serverSocket.accept();
clientSockets.add(clientSocket);
Thread clientThread = new ServerThread(clientSocket, this);
clientThread.start();
}
serverSocket.close();
}
public void broadcast(ObjectPackage outputObject) {
for (Socket socket : clientSockets) {
ObjectOutputStream out;
try {
out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(outputObject);
out.flush();
} catch (IOException e) {
// We failed at writing to the socket...
//e.printStackTrace();
//removeSocket(socket);
}
}
}
public void removeSocket(Socket socket) {
clientSockets.remove(socket);
System.out.println("socket removed");
}
public static void main(String[] args) throws IOException {
new Server(4444);
}
}
ServerThread:
public class ServerThread extends Thread{
private Socket socket = null;
private Server server;
public ServerThread(Socket socket, Server server) throws IOException {
this.socket = socket;
this.server = server;
}
public void run() {
try {
ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
ObjectPackage objectOut,objectIn;
while ((objectIn = (ObjectPackage) objIn.readObject()) != null) {
objectOut = objectIn;
System.out.println("Object received: " + objectOut.x);
server.broadcast(objectOut);
}
}
catch (EOFException e){
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
ClientThread
public class ClientThread {
private String host;
private int port;
private ObjectPackage pack;
private Socket socket;
public ClientThread(final String host, final int port) {
this.host = host;
this.port = port;
}
public void connect() {
try {
socket = new Socket(host, port);
} catch (IOException e) {
e.printStackTrace();
}
System.out.format("[Client] Connected to server %s:%d!\n", host, port);
final Thread outThread = new Thread() {
@Override
public void run() {
System.out.println("Started...");
ObjectOutputStream obj;
try {
obj = new ObjectOutputStream(socket.getOutputStream());
pack = new ObjectPackage(5);
obj.writeObject(pack);
} catch (IOException e) {
e.printStackTrace();
}
}
};
outThread.start();
final Thread inThread = new Thread() {
@Override
public void run() {
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectPackage object;
while (true) {
object = (ObjectPackage) in.readObject();//line 60
System.out.println(object.x);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
inThread.start();
}
public void disconnect() {
try {
socket.close();
System.out.println("socked closed");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I have 2 other classes but it's just the serialized object I'm sending and the main class starting the client.