-1

I have the send working but the server side wont receive the object sent in the data input stream. The class is serialized and works when sending but I'm not sure why it is not being received by the server.

Here is my code:

Server.ois = new ObjectInputStream(client.getInputStream());
while(Server.ois.available() != 0) {
    try {
        TriangleSend ts = (TriangleSend)Server.ois.readObject();
        send(ts);
        Server.ois.close();
    } catch (Exception e) {
        continue;
    }
}

That is all in a while loop and a try. It also generates the exception when received.

The send (TriangleSend) method is:

public static void send(TriangleSend coords) {
    for (Socket client : Server.clients) {
        try {
            if (client != null) {
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
                    oos.writeObject(coords);
                    oos.flush();
                } catch (Exception e) {}
            }
        } catch (NullPointerException e) {
            continue;
        }
    }
}

EDIT: Source Code And An Exception: http://pastebin.com/rYzqduer

heres the exception:

java.io.StreamCorruptedException: invalid stream header: 73720027
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at com.gopro2027.lwjgl.server.ServerThread$1$1.run(ServerThread.java:88)
    at java.lang.Thread.run(Thread.java:745)
Could not send data Timeout: 0
gopro_2027
  • 103
  • 1
  • 9
Tyler Songer
  • 70
  • 1
  • 9
  • 2
    "*it also generates the exception when recieved*" Anytime an exception gets thrown, please post the stack trace for that exception – Vince Nov 13 '14 at 00:07

2 Answers2

0

Tyler - wrap your client's output stream with a BufferedOutputStream before constructing the ObjectOutputStream - conversely do a BufferedInputStream before constructing the ObjectInputStream on the server side. That should help with the read/write process.

Server Side:

Server.ois = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
while(Server.ois.available() != 0) {
    try {
        TriangleSend ts = (TriangleSend)Server.ois.readObject();
        send(ts);
        Server.ois.close();
    } catch (Exception e) {
        e.printStackTrace();
        continue;
    }
}

Send Triangle:

public static void send(TriangleSend coords) {
    for (Socket client : Server.clients) {
        if (client != null) {
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(client.getOutputStream()));
                oos.writeObject(coords);
                oos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

removed edit

gopro_2027
  • 103
  • 1
  • 9
Dave G
  • 9,639
  • 36
  • 41
  • i changed the input and output streams to BufferedOutputStream bis = new BufferedOutputStream(client.getOutputStream()); ObjectOutputStream oos = new ObjectOutputStream(bis); and it changed the amount of times it generates the error but didnt fix it – Tyler Songer Nov 13 '14 at 00:58
  • 1
    Can you post a stack trace? – Dave G Nov 13 '14 at 00:59
  • it doesnt generate an exception... it makes an exception but i cant find where or if its just not generating to stack trace – Tyler Songer Nov 13 '14 at 01:04
  • 1
    You're swallowing the exception ... I'll repost code to get that to come out – Dave G Nov 13 '14 at 01:15
  • it still doesnt work... had the same effect as i had it when i changed the code to what you posted before you posted it. would you like to see all of the code? – Tyler Songer Nov 13 '14 at 02:02
  • 1
    @TylerSonger "Doesn work" is not a problem description. You haven't asked an answerable question yet. Post the stack trace, as everyone where has asked you to do. It should have been in your question all along. – user207421 Nov 13 '14 at 02:06
  • 1
    @TylerSonger with the above code that I provided you should be seeing stack traces all over the place. I, as well as two others, have requested you post output of your code. You have not done that. – Dave G Nov 13 '14 at 02:12
  • sorry i am at school right now. i will post the error as soon as i get home. and all of the code on pastebin – Tyler Songer Nov 13 '14 at 14:01
  • @TylerSonger The exception must be posted ***in the question.*** you've been told that before. – user207421 Nov 13 '14 at 21:26
0
while(Server.ois.available() != 0) {

available() is not a valid test for end of stream. See the Javadoc. You should change the loop to while (true) and break when you catch EOFException.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • sorry i forgot to mention. i had that and then that surrounded in a while loop and that in a new thread. i already had that in there :( i can post the full code on pastebin if you need me too. – Tyler Songer Nov 13 '14 at 01:10
  • What we need is the mysterious exception, *and* your real code. – user207421 Nov 13 '14 at 02:00