Results 1 to 2 of 2
  1. #1
    sanderstad is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default java.io.EOFException when sending object through socket

    Hi all,

    I've been struggeling with this issue for some time now and I just don't know why it fails.
    I have created a small program that send an object through a socket to a server where the server receives the object and prints a attribute
    value of the object.

    I tested this in a little program and that works fine but this was only meant for one connection and than everthing closes.

    The architecture is that of a server->agent where the server send a object to the agent. The agent has to run as long as it is need to and
    the server will open a socket to the server.

    When I start the agent and than the server it will give a "java.io.EOFException". The full errror can be seen below:
    Java Code:
    java.io.EOFException
    	at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    	at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    	at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    	at java.io.ObjectInputStream.<init>(Unknown Source)
    	at Agent.runAgent(Agent.java:98)
    	at Agent.<init>(Agent.java:50)
    	at Agent.main(Agent.java:170)
    java.io.EOFException
    	at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    	at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    	at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    	at java.io.ObjectInputStream.<init>(Unknown Source)
    	at Agent.runAgent(Agent.java:98)
    	at Agent.<init>(Agent.java:50)
    	at Agent.main(Agent.java:170)
    Can anybody tell me why my this is happening because I'm all out of ideas at the moment.

    I've tried to include both the Eclipse projects in ZIP format but the JaneServer file wouldn't upload.
    The JaneAgent project is included: JaneAgent_20111103.zip

    The code of the JaneServer can be seen below:
    JaneServer
    Java Code:
    import com.jennifersoft.jane.agent.common.*;
    
    public class JaneServer {
    
    	public static void main(String args[]) {
    		
    		Server server = null;
    		
    		String configFile = "";
    		if (args.length > 0) {
    				configFile = args[0];
    		}else{
    			String workDirectory = System.getProperty("user.dir");
    			configFile = workDirectory + "\\resources\\Server.properties";
    		}
    		
    		configFile = configFile.replaceAll("\\\\", "\\\\\\\\");
    
    		ConfigurationHandler agentConfig = new ConfigurationHandler(configFile);
    
    		if (agentConfig.fileExist(configFile)) {
    			server = new Server(agentConfig);
    		} else {
    			System.out.println("Configuration file couldn't be located!");
    		}
    		
    		String[] agents = server.getAgents();
    		
    		if(agents != null){
    			for(int i = 0; i < agents.length; i++){
    				System.out.println(agents[i]);
    			}
    		}else{
    			System.out.println("Geen verbonden agents");
    		}	
    	}
    
    }
    Server
    Java Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.BindException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    
    import com.jennifersoft.jane.agent.common.ConfigurationHandler;
    import com.jennifersoft.jane.agent.common.DatabaseHandler;
    import com.jennifersoft.jane.agent.common.PerformanceProfile;
    import com.jennifersoft.jane.agent.common.ProfileHandler;
    
    public class Server {
    
    	private ConfigurationHandler configHandler = null;
    	private DatabaseHandler dbHandler = null;
    	private ProfileHandler ppHandler = null;
    	
    	private int port = 0;
    	private String ppLoc;
    	private String dbLoc;
    	private String dbUsername;
    	private String dbPassword;
    	private ArrayList<String> agents;
    
    	private Socket socket = null;
    	
    	private static final int DEFAULT_PORT = 11111;
    	private static final String DEFAULT_PPLOC = "C:\\Temp\\PerformanceProfiles.xml";
    	
    	/**
    	 * Constructor for the Server class.
    	 * Accepts a configuration handler class
    	 * @param cf
    	 */
    	public Server(ConfigurationHandler cf) {
    		// Initialize the variables
    		configHandler = cf;
    		agents = new ArrayList<String>();
    		
    		// Get the configuration values
    		getConfigurationValues();
    		
    		// Initialize the database handler
    		dbHandler = new DatabaseHandler(dbLoc, dbUsername, dbPassword);
    		
    		// Initialize the performance profile handler
    		ppHandler = new ProfileHandler(ppLoc);
    		
    		// Refresh the local agents variable
    		refreshConnectedAgents();
    	}
    	
    	/**
    	 * Method to locally refresh the connected agents
    	 */
    	public void refreshConnectedAgents() {
    		// Get the connected agents from the database and set them
    		// to the local variable
    		ArrayList<String> temp = dbHandler.getConnectedAgents();
    		
    		if(temp.size() >= 1){
    			for(int i = 0; i < temp.size(); i++){
    				try {
    					// try to get a connection to the Agent
    					socket = new Socket(temp.get(i), port);
    					// Add the agent to the arraylist when successful
    					if(!agents.contains(temp.get(i))){
    						agents.add(temp.get(i));
    					}
    					socket.close();
    				} catch (UnknownHostException e) {
    					// TODO Auto-generated catch block
    					//e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					//e.printStackTrace();
    				}
    			}
    		}
    	}
    	
    	/**
    	 * Method for retrieving the configuration values from the configuration file
    	 */
    	private void getConfigurationValues() {
    		// Check the setting of the port from the configuration file.
    		// If no setting is made, the port is the default port.
    		if (configHandler.getConfigValue("PORT").equals("")
    				|| configHandler.getConfigValue("PORT").equals("0")) {
    			port = DEFAULT_PORT;
    		} else {
    			port = Integer.parseInt(configHandler.getConfigValue("PORT"));
    		}
    
    		// Check the setting of the performance profile location
    		// from the configuration file.
    		if (configHandler.getConfigValue("PROFILE_LOCATION").equals("")) {
    			ppLoc = DEFAULT_PPLOC;
    		} else {
    			ppLoc = configHandler.getConfigValue("PROFILE_LOCATION");
    		}
    
    		// Get the Derby database location from the configuration file
    		if (!configHandler.getConfigValue("DERBYDB_LOCATION").equals("")) {
    			dbLoc = configHandler.getConfigValue("DERBYDB_LOCATION");
    		} else {
    			System.out.println("No Derby Database location given!");
    		}
    
    		// Get the Derby Database username from the configuration file
    		if (!configHandler.getConfigValue("DERBYDB_USERNAME").equals("")) {
    			dbUsername = configHandler.getConfigValue("DERBYDB_USERNAME");
    		} else {
    			System.out.println("No Derby Database username given!");
    		}
    
    		// Get the Derby database password from the configuration file.
    		if (!configHandler.getConfigValue("DERBYDB_PASSWORD").equals("")) {
    			dbPassword = configHandler.getConfigValue("DERBYDB_PASSWORD");
    		} else {
    			System.out.println("No Derby Database password given!");
    		}
    	}
    
    
    	/**
    	 * Method to get the connected agents in array format
    	 * @return
    	 */
    	public String[] getAgents() {
    		// Refresh the connected agents
    		refreshConnectedAgents();
    		// Convert the arraylist to an array
    		String[] temp = new String[agents.size()];
    		temp = agents.toArray(temp);
    		
    		if(temp == null || temp.length < 1){
    			return null;
    		}else{
    			// 	Return the array
    			return temp;
    		}
    	}
    	
    	/**
    	 * Method for sending the performance profile to the Jane Agent
    	 * @param agent
    	 * @param pp
    	 */
    	public void sendProfile(String agent, String pp){
    		// Get the performance profile from the XML file
    		PerformanceProfile profile = ppHandler.getPerformanceProfile(pp);
    		System.out.println("Sending profile: " + profile.getName());
    		try {
    			// Create a connection to the Jane Agent
    			socket = new Socket(agent, port);
    			// Instantiate the input and output variables for the sockets
    			OutputStream os = socket.getOutputStream();
    			// Create new output stream to send the data in bytes
    			ObjectOutputStream oos = new ObjectOutputStream(os);
    			// Write the object
    			oos.writeObject(profile);
    			//oos.writeObject(new String("another object from the client"));
    			
    			// Flush and close the object output stream
    			//oos.flush();
    			oos.close();
    			// Flush and close the output stream
    			//os.flush();
    			os.close();
    			// Close the socket
    			socket.close();
    		} catch (UnknownHostException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		} catch (IOException ioe) {
    			System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
    			//ioe.printStackTrace();
    		}  	
    		
    	}
    }

    The Agent is started by running the Agent class. The server is started by running the JaneServer class.
    The software uses a Apache Derby database which I can't include but maybe someone can help me.

    Thank you in advance.

  2. #2
    sanderstad is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default Re: java.io.EOFException when sending object through socket

    Can anybody help me with this. I'm reallt stuck here and I don't know what I'm doing wrong.

    Thanax in advance

Similar Threads

  1. Object Input Stream EOFException
    By FlyNn in forum New To Java
    Replies: 1
    Last Post: 12-18-2010, 12:33 PM
  2. Sending object through socket
    By Alexandrinne in forum New To Java
    Replies: 0
    Last Post: 11-15-2010, 07:03 AM
  3. sending object through network/socket
    By skandalouz in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:34 AM
  4. Replies: 2
    Last Post: 09-10-2009, 02:29 PM
  5. Replies: 0
    Last Post: 04-05-2009, 03:59 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
  •