Results 1 to 2 of 2
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default 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.

    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;
    	}
    }
    Feel free to ask any questions you need too, I realize comments are inefficient in the code right now...

    Consistent inconsistency... strange
    Singing Boyo
    Attached Files Attached Files
    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!

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. Sockets NIO
    By aamp in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 11:56 AM
  2. ObjectInput/Output Streams
    By Rhesus21 in forum Networking
    Replies: 3
    Last Post: 07-30-2008, 02:26 AM
  3. Inconsistent layout w/dynamic resize of components
    By donb2000 in forum AWT / Swing
    Replies: 3
    Last Post: 07-26-2008, 03:40 PM
  4. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •