8

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();
        }   
    }   
}
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Bernard
  • 4,240
  • 18
  • 55
  • 88
  • Just an advise: Don't send Objects you make over the internet. Only send primitive types. String, Byte, Integer, Float, Double, Char and then convert on either side to the format you want. – OmniOwl Oct 15 '12 at 12:15
  • sent multiple strings and then recreate the arraylist, that's better – Gianmarco Oct 15 '12 at 12:17
  • @ Gianmarco: That's a good idea but still I want to learn how to send an object and also it's part of assignment specification! – Bernard Oct 15 '12 at 12:20
  • @Bernard The general approach is that you don't send Objects, but just raw data. Then you recreate the data on the end it is received. – OmniOwl Oct 15 '12 at 12:23
  • I'd recommend breaking the problem up. Can you successfully serialize and deserialize the ArrayList? No sockets, no client; just write it to the file system and read it back. Once you can do that, then worry about sockets. – duffymo Oct 15 '12 at 12:25
  • @Vipar Do you mean I must change the server side where I'm sending the object? Do I still need ObjectOutputStream? – Bernard Oct 15 '12 at 12:26
  • You already have an answer to this question, but learn to work with raw data rather than Objects of data. It is much safer to send over "the wire" (expression for the internet) than Objects of data is. Also a lot easier to work with, since you won't have to make your own Input/Output streams specifically tailored for your objects. – OmniOwl Oct 15 '12 at 12:30
  • @duffymo Do you mean I write the file inside the server side to one file like text file and read it back? What's the meaning of "write it to the file system and read it back"? – Bernard Oct 15 '12 at 12:31
  • @Viper Thanks for the hint. I think you mean it's better to use DataInputStream/DataOutputStream Instead of ObjectInputStream. but as I said part of assignment specification is to use : ObjectInputStream/ObjectOutputStream. – Bernard Oct 15 '12 at 12:34
  • Don't write the objects to the socket. Write them to disk and read them back. – duffymo Oct 15 '12 at 12:43

1 Answers1

8

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

shyam
  • 9,134
  • 4
  • 29
  • 44
  • After 1 hour of posting this message I found that the problem was set method after changing it to what you said the program works 100% fine. – Bernard Oct 15 '12 at 13:16
  • Just wondering, shouldn't your server code have thrown an IndexOutOfBoundsException since you were calling set for non-existent elements? – Klitos Kyriacou Oct 15 '12 at 13:23
  • @KlitosKyriacou : Unfortunately no. It did not thrown any exception so wasted my 5 hours of time! – Bernard Oct 15 '12 at 13:28
  • 1
    That is because you did not look at the exception thrown when you run `ServerSide` as, KlitosKyriacou pointed out `IndexOutOfBoundsException` is indeed thrown which is how I found the solution – shyam Oct 15 '12 at 13:31
  • @shyam : Well, Now I just double check the program and I realise that there was an IndexOutBoundException exception! which I did not see it before. Because the client side error used to prompted first and I didn't look at the server side :(.... Anyway thanks for your help. ;) – Bernard Oct 15 '12 at 13:39