java.io.StreamCorruptedException: invalid type code: AC- Error found while running the client code from the server-client java program
You are double opening an ObjectOutputStream on the same socket in the Server class which is causing special headers to be sent twice.
1: openSockets
last line outputToClient = new ObjectOutputStream(clientSocket.getOutputStream());
2: writeToSocket
first line outputToClient = new ObjectOutputStream(clientSocket.getOutputStream());
Remove whichever one you want and it should work just fine.
As for the nullpointer you are now receiving I have a feeling that video is null in the client. In the code you have posted that would make sense because in the server you never create a video array and as such the client is just receiving null.
How to append to an ObjectInputStream without getting java.io.StreamCorruptedException: invalid type code: AC?
(I am appending to the file during the writing phase)
And that is the problem. You can't append to an ObjectOutputStream. That will definitly corrupt the stream and you get StreamCorruptedException.
But I already left a solution to this problem on SO: an AppendableObjectOutputStream
EDIT
From the writer I see that you write one cheque object and flush and close the stream. From the reader, I clearly see, that you're trying to read more than one cheque object. And you can read the first one but not the rest. So to me it is perfectly clear, that you reopen the Stream and append more and more cheque objects. Which is not allowed.
You have to write all cheque objects in 'one session'. Or use the AppendableObjectOutputStream instead of the standard ObjectOutputStream.
Exception StreamCorruptedException: invalid type code: AC
According to this post you cannot append to a ObjectOutputStream, which you are trying to do by opening the underlying FileOutputStream in append mode. There is a solution mentioned on that post such that you create an AppendableObjectOutputStream , or you could just open the FileOutputStream without appending.
CountDownLatch vs. Semaphore
CountDownLatch
is frequently used for the exact opposite of your example. Generally, you would have many threads blocking on await()
that would all start simultaneously when the countown reached zero.
final CountDownLatch countdown = new CountDownLatch(1);
for (int i = 0; i < 10; ++ i) {
Thread racecar = new Thread() {
public void run() {
countdown.await(); //all threads waiting
System.out.println("Vroom!");
}
};
racecar.start();
}
System.out.println("Go");
countdown.countDown(); //all threads start now!
You could also use this as an MPI-style "barrier" that causes all threads to wait for other threads to catch up to a certain point before proceeding.
final CountDownLatch countdown = new CountDownLatch(num_thread);
for (int i = 0; i < num_thread; ++ i) {
Thread t= new Thread() {
public void run() {
doSomething();
countdown.countDown();
System.out.printf("Waiting on %d other threads.",countdown.getCount());
countdown.await(); //waits until everyone reaches this point
finish();
}
};
t.start();
}
That all said, the CountDownLatch
can safely be used in the manner you've shown in your example.
Related Topics
What's the Difference Between JavaScript and Java
How to Compare Strings in Java
What Does a "Cannot Find Symbol" or "Cannot Resolve Symbol" Error Mean
How to Avoid Java Code in Jsp Files, Using Jsp 2
What Is Reflection and Why Is It Useful
Manipulating an Access Database from Java Without Odbc
Including All the Jars in a Directory Within the Java Classpath
What Does "Could Not Find or Load Main Class" Mean
Java String to Date Conversion
How to Use Java.Net.Urlconnection to Fire and Handle Http Requests
Converting Iso 8601-Compliant String to Java.Util.Date
How to Parse Invalid (Bad/Not Well-Formed) Xml
How to Make an Android Device Vibrate? With Different Frequency
Will System.Currenttimemillis Always Return a Value ≫= Previous Calls
Can't Start Eclipse - Java Was Started But Returned Exit Code=13