Results 1 to 2 of 2
  1. #1
    invidos is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default server/client problems

    Hi

    I've been making a simple game for school, and I've gotten stuck at the network part of it.
    The game returns a few different exceptions.

    java.io.OptionalDataException
    java.io.StreamCorruptedException: invalid type code: F0

    The type code isn't allways the same either.
    Problem is that neither I or my teacher knows where the problem is, so I thought that I might aswell post the queastion here

    Java Code:
    	public void update(PlayerData data)
    	{
    		try
    		{
    			outStream.writeObject(data);
    			outStream.flush();
    			Object input = inStream.readObject();
    			if(input instanceof NetworkedEntity)
    			{
    				NetworkedEntity netEntity = (NetworkedEntity)input;
    				Point position = netEntity.entityPosition;
    				Entity entity = null;
    				if(netEntity.entityID == 21)
    				{
    					entity = new Wall(position, GUI);
    				}
    				if(entity != null)
    				{
    					GUI.controller.addEntity(entity);
    				}
    			}
    			else if(input instanceof PlayerData)
    			{
    				PlayerData enemyData = (PlayerData)(input);
    				if(!enemyData.getName().equals(GUI.playerName))
    				{
    					enemyFarmer.setPosition(enemyData.getPosition());
    				}
    			}
    		}
    		catch(Exception e)
    		{
    			System.out.println("Problem updating to the server:" + e);
    			e.printStackTrace();
    		}
    	}
    That is the code which the exceptions are coming from.
    Both NetworkedEntity and PlayerData are serializable.
    And the printStackTrace() points the problem to the readline("Object input = inStream.readObject") and outStream is an ObjectOutputStream and inStream is an ObjectInputStream

    Java Code:
    	public void addEntity(Entity entity, int id)
    	{
    		NetworkedEntity netEntity = new NetworkedEntity(entity.getPosition(), id, true);
    		try
    		{
    			outStream.writeObject(netEntity);
    			outStream.flush();
    			GUI.controller.addEntity(entity);
    		}
    		catch(Exception e)
    		{
    			System.out.println("Problem syncing new Entities:" + e);
    		}
    	}
    Not sure that it matters but those 2 functions are the only ones writing/reading.
    The program can run for a while without any problem, but after you have called for the addEntity function the game gives some sort of Exception.
    Any help would be very usefull!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: server/client problems

    The ObjectOutputStream writes a header block before it writes anything else and it writes it only once; the ObjectInputStream reads that block and it only expects it once; the implications are that if you open an output stream you should also open an input stream and don't open another output stream connected to the same old input stream because another header is writen while the input stream doesn't expect one anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  2. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  4. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:07 AM

Posting Permissions

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