Hey all -

I have created an ArrayList as follows:

ArrayList<Object> arList = new ArrayList<Object>();

The arraylist contains the following - every time it will have the same data ... either filled or considered "";

String - transaction type
String - name
String - ssn
String - address
int - code

How can i take this arrayList and seriliaze it, and pass it from a 'client' through sockets, to a 'server'?

I have read about this online all over the place - but as for now i have come up with something like:

ObjectOutputStream oos = new ObjectOutputStream();

But i get a precompile error here saying: "The constructor ObjectOutputStream() is not visible"

And i have no clue what that means...

I assume i create an output object, then do outObj.writeOut(arList):

Then on the server side use an input object and use:

io.readObject();

But i am still pretty clueless, if someone could open my eyes as to how this would be plausible - i would be very very appreciative!

Recommended Answers

All 5 Replies

Current code looks like this now:
Client Side

System.out.println("All things filled - will call server with info");
			
sendInfoAr.add("ADD");
sendInfoAr.add(nameVal);
sendInfoAr.add(ssnVal);
sendInfoAr.add(addressVal);
sendInfoAr.add(codeVal);
		    
try{
System.out.println("Should now create and send serialized thing");
FileOutputStream fileOut = new FileOutputStream("something.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(sendInfoAr);
out.close();
fileOut.close();
		        
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Server Side:

ArrayList<Object> inputDataAr = new ArrayList<Object>();
userInfo readInUser = new userInfo();
 try {
System.out.println("Creating File/Object input stream...");
          
FileInputStream fileIn = new FileInputStream("something.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);

System.out.println("Loading Hashtable Object...");
inputDataAr = (ArrayList)in.readObject();

System.out.println("Closing all input streams...\n");
 in.close();
fileIn.close();
          
} catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch(FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }

System.out.println("Should print name: " + readInUser.name);

When i run my client, and enter values and store them into my arrayList, that part seems to work, but the server gives me this error:


Server listening on port 9704
Creating File/Object input stream...
java.io.FileNotFoundException: something.ser (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at Conversation.run(server.java:166)
Should print name:

Line 166 in server.java is this line of code:

FileInputStream fileIn = new FileInputStream("something.ser");

Anyone have any idea why the something.ser couldn't be found? Im confused by this - as im 99% sure my client does send this to the server!

Thanks for any help

Is this file "something.ser" already created before you try to read that using fileInputStream at the server's side?

Alright - i have changed this thing up some. Instead of using an ArrayList ... i have implemented my own class to store the data, and i will attempt to serialize an object of that class - sending that to the server instead. This should give me better access etc to my values.

Here is what i have done:
server and client both have:

public class UserInfo implements Serializable {
String name, ssn, address, type, response, code;
public UserInfo() {
 name = ssn = address = type = code = response = "";
}
}

On the client side i create an instance of this class, then similar to previous attempts, i get the information from my GUI and store in the objects values. Here:

UserInfo addUser = new UserInfo();
...
...
addUser.name = nameVal;
addUser.address = addressVal;
addUser.ssn = ssnVal;
addUser.code = codeVal;

I then try to send this to the server as a serialized object:

System.out.println("Should now create and send serialized thing");
oos = new ObjectOutputStream(sock.getOutputStream());	    	
oos.writeObject(addUser); 		
oos.flush();   
oos.reset();
 }
catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} // end try

I get the following error on the oos.WriteObject() line:
java.io.NotSerializableException:

Why would this be - initially i thought it was because i had 'code' as an int, and i read somewhere that ints donesn't implement serializable?

Any ideas/tips etc would be appreciated, thank you

Okay - moving on - hopefully i can get another reply with this more simple question:

I have this code:

public void run()
  {  
// try connect to mysql database
//System.out.println("" + addUser.name + " " + addUser.ssn); 
try {
			
	ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
	UserInfo addUser = null;// = new UserInfo();
	addUser = (UserInfo) ois.readObject();	
	System.out.println(addUser);
	ois.close();
			
if (addUser.type.equals("ADD"))
	{
	System.out.println("in here");
	addUser = addCustomer(addUser);
	}
	} catch (ClassNotFoundException e) {
	e.printStackTrace();
	} catch (IOException e2) {
	e2.printStackTrace();
	}
		
//oos.writeObject(addUser); //send updated info back to server
System.out.println("Closing all input streams...\n");
} 
// end run

This is in my server.java

In my client i have this:

try 
{
Socket sock = new Socket("turing.cs.niu.edu", 9704);
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream()); 

nameVal = tfName.getText();
ssnVal = tfSSN.getText();
addressVal = tfAddress.getText();
codeVal = tfCode.getText();
if (nameVal.equals(""))
{
tfName.setText("error - empty");
nameError.setText("EMPTY FIELD");
    System.out.println("empty name field");
}
if (ssnVal.equals(""))
tfSSN.setText("error - empty");
if (addressVal.equals(""))
tfAddress.setText("error - empty");
if (codeVal.equals(""))
tfCode.setText("error - empty");
else if (!nameVal.equals("") && !ssnVal.equals("") && addressVal.equals("") && !codeVal.equals("")) {
   System.out.println("All things filled - will call server with info");
   addUser.name = nameVal;
   addUser.address = addressVal;
   addUser.ssn = ssnVal;
   addUser.code = Integer.parseInt  (codeVal);
   addUser.type = "ADD";
   oos.writeObject(addUser); 
   oos.flush();
   oos.close();
   } // end else if
   } // end try
   catch (UnknownHostException uhe)
   {
    System.out.println("Unknown Host");
    }
    catch (IOException ioe)
    {
     System.out.println("IO error: " + ioe.getMessage());
     }
}

I get the following error:
java.io.StreamCorruptedException: invalid stream header: 7372000F

On this line of the server code:

ObjectInputStream ois = new ObjectInputStream(client.getInputStream());

Anyone have any ideas as to why this would happen?

thanks in advance

You often get "stream corrupted" during debugging when a previous test crashes part way thru and the socket/stream is left open and part-written or part-read. It's worth checking that there are no instances of the JVM still running from failed tests before starting the next test.
ps: You shouldn't have to write your own serialisation code for a class that just has serialisable members - I would go back to that version and find out why it didn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.