Java – use socket to send ArrayList from server to client through TCP?

I'm trying to send an object from a server socket to a client socket over TCP I can't find the problem

This is the error I encountered on the client:

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)

Server side code:

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();
        }
    }

}

Client code:

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();
        }   
    }   
}

Solution

Changing from set to add solves the problem

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

PS. according to others' suggestions, this is not a good idea, but only for learning

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>