Results 1 to 2 of 2
- 06-20-2009, 06:52 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Inconsistent ObjectInput/OutputStreams through Sockets
Hi all... I've been trying to create a fairly basic set of classes that I can use for server/client connections. However, when I began testing, I found that the ObjectOutput/InputStreams in my classes did not work properly. I have no idea why, but every time I run the server and then clients, the first client fails to initialize the streams, the second one succeeds and receives the object that it is sent, and all other clients fail, along with the corresponding threads for the server. Its very strange. I did wrap my Sockets in classes for efficiency, but I can't see what it would do. I've attached a zip file with all the code related to the test/library, since I have no idea where to start. The code for the class that wraps the Socket is below. If you do take a look at the zip file, I believe the problem is somewhere in the ObjectSocket, Clinet, or ClientThread classes, as they are the only classes handling the sockets with problems. The only other class would be the SocketDistributor class, but that seems unlikely as I do get debug messages saying it printed.
Feel free to ask any questions you need too, I realize comments are inefficient in the code right now...Java Code:package contact; import java.net.*; import java.io.*; public class ObjectSocket<T> extends SocketWrapper<T>{ private ObjectInputStream input; private ObjectOutputStream output; public ObjectSocket(Socket sock, boolean server) throws IOException{ super(sock); System.out.println("Entering OSStreamInit"); if(server) initServerStreams(); else initClientStreams(); } private void initClientStreams() throws IOException{ input = new ObjectInputStream(socket.getInputStream()); output = new ObjectOutputStream(socket.getOutputStream()); } private void initServerStreams() throws IOException{ output = new ObjectOutputStream(socket.getOutputStream()); input = new ObjectInputStream(socket.getInputStream()); } @SuppressWarnings("unchecked")//TODO: somehow fix the suppresswarnings necessity public T read(){ T o; try { o = (T) input.readObject(); } catch (Exception e1) { return null; } return o; } public boolean write(T o) throws IOException{ output.writeObject(o); output.flush(); return true; } }
Consistent inconsistency... strange
Singing BoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-22-2009, 12:43 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I still have no idea what the problem was, but I rewrote the SocketDistributor class and it works. Probably some problems with my wait()/notifyAll() calls.
Please don't blame me if you try to use the code in the zip file and it doesn't work... it's largely untested.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Sockets NIO
By aamp in forum New To JavaReplies: 3Last Post: 01-15-2009, 10:56 AM -
ObjectInput/Output Streams
By Rhesus21 in forum NetworkingReplies: 3Last Post: 07-30-2008, 01:26 AM -
Inconsistent layout w/dynamic resize of components
By donb2000 in forum AWT / SwingReplies: 3Last Post: 07-26-2008, 02:40 PM -
Help with Sockets
By Eric in forum NetworkingReplies: 3Last Post: 12-01-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks