Common networking errors

Posted: - Modified: | teaching

Due 2004.02.23 11:59:59 PM: Source code and detailed notes on

– changes made
– problems encountered
– solutions tried/found
– outstanding problems
– plans for next step

I don’t expect everyone to have a fully-working game by tomorrow, so
most of your partial points will come from the notes. “It doesn’t
work.” isn’t enough; I need to know why it doesn’t work, if it used to
work before, what you’ve tried doing to get it to work, why you think
it doesn’t work, etc.

Along the way to your first milestone, you may have encountered the
following problems:

ObjectInputStream seems to hang when you create it.

That happens when both sides of the connection try to create object
input streams at the same time, but the other side hasn’t opened an
object output stream yet. Fix: create the object output stream first
before you create the input stream.

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

Your program lags terribly.

How are you doing your networking and repainting code? If you’re doing
animation, repaint() should be in one thread and your networking code
should be in another. You shouldn’t do them in the same thread because
then repaint() will only happen whenever something is received from
the socket connection. Also, make sure you have delays somewhere!
while (true) { repaint(); } means your CPU will spend
most of its time repainting. Add a Thread.sleep in that loop so that
your computer has time to do other things.

You can’t write certain objects over the stream.

Make sure the object is serializable and all of its attributes are
either transient or serializable as well. You really shouldn’t be
sending your entire application over the network – what is the other
side going to do with all those textfields? Mark many of your
attributes transient or create a small, simple object that
encapsulates the data you need to send.

You’ve sent an object over the network connection and you’re calling methods on it on the other side, but the one on the first computer isn’t changing!

That’s the way it works. If you send an object from computer A to
computer B, computer B isn’t working on the original object but on a
copy of it. Any changes on A won’t be automatically sent to B and vice
versa. The correct thing to do is examine the object and perform
different actions locally.

If you really need to get something like this working and you have
plenty of spare time, read up on RMI (Remote Method Invocation). It’s
out of the scope of CS21B, though.

For tonight, I want lots and lots of notes.

TOMORROW: Data structures (stacks, queues and linked lists).

You can comment with Disqus or you can e-mail me at sacha@sachachua.com.