Sending an ArrayList<String> from the server side to the client side over TCP using socket?

Bernard picture Bernard · Oct 15, 2012 · Viewed 31.5k times · Source

I'm trying to send one object from the server side socket to the client side socket over TCP. I can't find out where is the problem.

Here is the error I'm getting on the Client side:

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
    at ClientSide.main(ClientSide.java:16)

Code for Server side:

import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class ServerSide {

    public static void main(String[] args) {
        try
        {
            ServerSocket myServerSocket = new ServerSocket(9999);
            Socket skt = myServerSocket.accept();   
            ArrayList<String> my =  new ArrayList<String>();
            my.set(0,"Bernard");
            my.set(1, "Grey");
            try 
            {
                ObjectOutputStream objectOutput = new ObjectOutputStream(skt.getOutputStream());
                objectOutput.writeObject(my);               
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            } 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

}

Code for the Client Side:

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;


public class ClientSide {

    public static void main(String[] args)
    {
        try {       
            Socket socket = new Socket("10.1.1.2",9999);
            ArrayList<String> titleList = new ArrayList<String>();
            try {
                ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream()); //Error Line!
                try {
                    Object object = objectInput.readObject();
                    titleList =  (ArrayList<String>) object;
                    System.out.println(titleList.get(1));
                } catch (ClassNotFoundException e) {
                    System.out.println("The title list has not come from the server");
                    e.printStackTrace();
                }
            } catch (IOException e) {
                System.out.println("The socket for reading the object has problem");
                e.printStackTrace();
            }           
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }   
}

Answer

shyam picture shyam · Oct 15, 2012

Changing from set to add does the trick

ArrayList<String> my =  new ArrayList<String>();
my.add("Bernard");
my.add("Grey");

ps. as advised by the others this is not a good idea but, use only for learning