Hello,

I have a problem, sending objects from android to a server. Since
there seems to be no RMI I use serialised objects.

The problem is that only the first few messages reach the server.
After that I get this:

java.io.StreamCorruptedException: invalid type code: AC

Why does this error happen? What does it mean? I only send
serializable objects to the stream who are from a jar that exists both
in the client and in the server. so both sides should know these
objects...

Here's how my program currently works:
When you start the android application it prompts the user for his
login data and the server IP. Then it creates a socket to that IP and
keeps the socket running for the whole session. Then it sends some
simple serializable objects through the socket. It seems to work on
the client side.
The server works like this: It has a thread that accepts incoming
connection. For each incoming connection it creates a new custom
object in its own thread that handles a socket for that connection.
This socket should be kept open until the client sends a LogOut-
message (which never happens since the server crashes before that)

Before I integrated the code into android I had the client part of the
connection running in a normal java application. It worked without any
problems. So my speculation is that I need to do something special to
make it work in android?

Here is some source code:


==SERVER==
The stuff that gets send through the socket all implement my
NetworkMessage interface, so all can be casted to that. The "server"
variable in that source code contains the main program, which reacts
to the messages. Currently, the server just calls the "toString"
method of these NetworkMessages, which should always work.
This code runs in its own thread:

        NetworkMessage nm;

                        nm = (NetworkMessage) objin.readObject();
                        while (nm != null){//!str.equals("")) {
                                System.out.println("SocketOnServer: There's a 
message");
                                server.receiveMessage(this, nm);
                                nm = (NetworkMessage) objin.readObject(); // 
CRASHES HERE


                        }




==CLIENT: log in part==

This is the login function. For testing purposes it currently just
sends some nonsense messages. It does not cause any exceptions.

public boolean logIn(String host, String username, String password,
GeoPoint currentLocation) {
                System.out.println("Now trying to connect to server...");

                boolean success = false;

                int port = 5554;//8189;
                int timeout = 5000;

                // get a socket
                Socket client = SocketOnClient.openSocket(host, port, 
timeout,this);
                if (client == null) {
                        System.out.println("The socket could not be opened.");
                } else {
                        try {

                                System.out.println("We got socket. Now try to 
create the object
streams...");
                                // open communications channels
                                //BufferedReader in = new BufferedReader(new 
InputStreamReader(
                                //              client.getInputStream()));
                                //PrintWriter out = new 
PrintWriter(client.getOutputStream(),
                                //              true);

                                objout = new 
ObjectOutputStream(client.getOutputStream());
                                System.out.println("objout was created. Now 
trying to send an
object");
                                objout.writeObject(new 
NetworkMessagePlainText("Halli Hallo!"));
                                System.out.println("Well, at least the simple 
plain text worked.
Lets try something more sophisticated...");

                                System.out.println("Now send all kinds of 
stuff");

                                objout.writeObject(new NetworkMessageError());

                                objout.writeObject(new NetworkMessageEvent(new 
EventDebug()));
                                objout.writeObject(new 
NetworkMessagePlainText("last message!"));
                                        //objout.writeObject(new 
NetworkMessagePlainText("das ist ne
testmessage"));

                                try{
                                        Thread.sleep(100);
                                } catch (Exception e){};

                                NetworkMessageLogIn sendme = new 
NetworkMessageLogIn
(username,password,currentLocation);
                                System.out.println("now we have the message. 
Lets write it to the
stream");
                                objout.writeObject(sendme);
                                System.out.println("we sent the object. lets 
flush");
                                System.out.println("horray!");


                                try{
                                        Thread.sleep(100);
                                } catch (Exception e){};

                                System.out.println("Now create objin stream");

                                objout.flush();
                                // TODO This takes too long:
                                //objin = new 
ObjectInputStream(client.getInputStream());
                                //System.out.println("objin was created");


                                System.out.println("Sending object complete!");

                                success = true;
                                //      System.out.println("you just sent a new 
EventDebug()");
                                //System.out.println("SERVER 
ANSWERS:"+in.readLine());
                                        //input = textin.readLine();// 
getInput();
                                //}
                                //objout.writeObject(null);// quit server socket
                        } catch (IOException e) {
                                System.out.println("Socket creation failed at 
some point, see
this:");//
                                e.printStackTrace();

                        }
                }
                System.out.println("LogInEnds. But did it succeed?");
                return success;
        }

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to