Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:42:59+00:00 2026-05-25T10:42:59+00:00

hi i am working on a client server programme with containing some special functions

  • 0

hi i am working on a client server programme with containing some special functions like sending private message, show online list etc. so i know i must use serialization and first i managed it but after a while a screwed up 🙂 nowadays i spending time for learn to serialize. i will share only meaningfull parts for prevent comlpexity. and i wanna learn where i making wrong. so thanks for your help anyway. here is a part of server code;

    public class Server {

        private ServerSocket ss;
        private Socket socket;
        private Map<Socket,DataOutputStream> list = new HashMap<Socket,DataOutputStream>();
        private LinkedList<Person> client_list = new LinkedList<Person>();
        private String socketName;
        private Object lockObj = new Object();

        public Server(int port_number) throws IOException{

            create_Server(port_number);
        }

        public static void main(String[] args) throws IOException {

            int port_number=23;

            new Server(port_number);
        }

        private void create_Server(int port_number) throws IOException{

            ss = new ServerSocket(port_number);

            System.out.println("Server is ready!");

            while(true){

                socket=ss.accept();

                System.out.println(socket.getLocalAddress().getHostName() + " was connected!");

                send_con_mes();

                list.put(socket,new DataOutputStream(socket.getOutputStream()) );

                ServerThread st = new ServerThread(socket,this);

                Person per = new Person(socket.getInetAddress().toString());

                client_list.add(per);

                st.start();


            }

        }

            public LinkedList<Person> send_list(){  

                   return client_list;
        }

so i am creating the server and waiting for response for any socket. moreover i used list for save the socket and its output and clien_list saves Object person (person is a serializable object ).

here is serverthread part

public class ServerThread extends Thread {

    private Socket s;
    private Server srv;
    private String socketName;
    private StringTokenizer str;
    private String message = "";
    private LinkedList<Person> client_list;
    private ObjectOutputStream oos;

    private static int i=0;

    public ServerThread(Socket s,Server srv){
        this.s = s;
        this.srv = srv;

        try {
            oos = new ObjectOutputStream(s.getOutputStream());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void setList(){

        client_list = srv.send_list();

    }

    private LinkedList<Person> getList(){

        return client_list;
    }

    @Override
    public void run() {

        String msg;
        String token;
        DataInputStream dis;

        try {
            dis = new DataInputStream(s.getInputStream());

            while(true){

                msg = dis.readUTF();
                srv.send_to_All(msg, s);

                setList();

                oos.writeObject(getList());
                oos.flush();

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                srv.remove_Connection(s);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

so i send client_list over on a ObjectOutputStream oos to clients.

finally here is client part which takes the list and deserialize person object and read the information…

public class Client extends javax.swing.JFrame implements Runnable {

    private DataOutputStream dos;
    private DataInputStream dis;
    private Socket s;
    private String Client_name;
    private String Ip_addr;
    private Font font = new Font("Arial", Font.PLAIN, 13);
    private int click_num_b=0;
    private int click_num_i=0;
    private LinkedList<Person> client_list;

    private FileOutputStream fos;
    private PrintStream pts;
    private ObjectInputStream socketIn;

/** Creates new form Client */
public Client() {
    initComponents();
    Screen.setEditable(false);

    Text_Field.setFont(font);
    Screen.setFont(font);
    start_Chat();

}

    @Override
    public void run() {

        try {

            while(true){

                    read_list();

                String message = dis.readUTF();
                Screen.append(message + "\n");

            }

        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    private void read_list() throws IOException{

        socketIn = new ObjectInputStream(s.getInputStream());

        try {

            client_list = (LinkedList<Person>) socketIn.readObject();

            for (Iterator<Person> itr = client_list.iterator(); itr.hasNext();) {

                Person per = itr.next();

                pts.println(per.getnickName() );

            }

            socketIn.close();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void start_Chat() {
        try {

            Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : ");
            s = new Socket(Ip_addr, 23);

            Client_name = JOptionPane.showInputDialog("Enter your Nickname : ");

            dis = new DataInputStream(s.getInputStream());         
            dos = new DataOutputStream(s.getOutputStream());

            fos = new FileOutputStream("personList.txt");
            pts = new PrintStream(fos);

            new Thread(Client.this).start();

in here private ObjectInputStream socketIn; takes the serializable object and writes on a file. here is some errors which i am facing with

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at ServerThread.run(ServerThread.java:58)


SEVERE: null
java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Client.read_list(Client.java:81)
    at Client.run(Client.java:61)
    at java.lang.Thread.run(Unknown Source)

so i appreciated if you can help me to handles this issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:43:00+00:00Added an answer on May 25, 2026 at 10:43 am

    Only ever send over your own objects. For example

    interface ServerToClientPacket { 
        void performAction(Client c); 
    }
    
    class MyMessage implements ServerToClientPacket {
        String message;
        MyMessage(String message) { this.message = message; }
        void performAction(Client c) {
            JOptionPane.showMessageDialog(message);
        }
    }
    
    class PersonList implements ServerToClientPacket {
        LinkedList<Person> people;
        // constructor here
        void performAction(Client c) {
            for(Person person : people) {
                c.pts.println(person);
            }
        }
    }
    

    Each will implement their own performAction on the client with the data that got serialized. When you define the behavior, instead of putting it in the client, put it in the message. Then your client becomes nothing more than a display mechanism for the various messages and behaviors passed to it from the socket.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice/beginner programmer having problems getting some simple client/server C code working. My
I have client server application using distributed object. some time my code is working
I am working on a client-server application that uses boost::serialization library for it's serialization
I'm working on a client-server Android application and trying to figure out how to
I'm working on a client/server library for a legacy RPC implementation and was running
I am working on a client server system, and am running into issues where
I am working on a .net rich client server application, the client call the
I am working on an application where client and server share an object model,
I've got a working SOAP::Lite client. It works against an established server, but so
I'm working on this project where the client has a virtual server setup. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.