I want to send an ArrayList<Integer> from client and and then read it from server using socket program and after readinf it at server, I want to calculate the sum of the elements of the ArrayList and then return the average to client.
I have written the code as follows

import java.net.*;
import java.io.*;
import java.util.ArrayList;
class testobject implements Serializable {
ArrayList al = new ArrayList();
public  testobject(ArrayList al){
this.al = al;
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class SimpleClient {
public static void main(String args[]){
try{
Socket s = new Socket("localhost",2002);
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
ArrayList al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
al.add(new Integer(5));
testobject to = new testobject(al);
oos.writeObject(to);
//oos.writeObject(new String("another object from the client"));
oos.close();
os.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
class SimpleServer  {
public static void main(String args[]) {
int port = 2002;
try {
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
//testobject to = (ArrayList)(testobject)ois.readObject();
//if (to!=null){System.out.println(to.id);}
ArrayList al = (ArrayList)ois.readObject();
System.out.println(al);
is.close();
s.close();
ss.close();
}catch(Exception e){System.out.println(e);}
}
}

The problem is that in server program, the object is getting read but is not being typecasted to arraylist. How to achieve this?

waiting for a solution..............

//solution
oos.writeObject(to.al);

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.