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 1114645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:02:10+00:00 2026-05-17T03:02:10+00:00

Okay, so I’m writing a test program to send a vector of objects from

  • 0

Okay, so I’m writing a test program to send a vector of objects from server to client.

-Server-

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

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

        Vector<Obj> obj = new Vector<Obj>();
        obj.add(new Obj(5));
        obj.add(new Obj(4));
        obj.add(new Obj(7));
        obj.add(new Obj(8));
        obj.add(new Obj(2));

        ServerSocket serverSocket = new ServerSocket(4444);
        boolean listening = true;

        while(listening){
            new ClientThread(serverSocket.accept(), obj).start();
        }

        serverSocket.close();
        System.exit(0);
    }
}

class ClientThread extends Thread implements Runnable{
    Socket acceptedSocket;
    Vector<Obj> obj;

    public ClientThread(Socket acceptedSocket, Vector<Obj> obj){
        super("ClientThread");
        this.acceptedSocket = acceptedSocket;
        this.obj = obj;
    }

    public void run(){
        try{
            Socket clientSocket = acceptedSocket;
            clientSocket.setTcpNoDelay(true);
            System.out.println("Accepted. Now creating I/O.\n");
            DataInputStream in = new DataInputStream(clientSocket.getInputStream());
            ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
            System.out.println("player connected. \n");
            out.writeObject(obj);
            out.flush();

            while(true){
                out.writeObject(obj);
                out.flush();

               sleep(15);
            }

        }

        catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }


    }
}

class Obj implements Serializable{
    int i;

    public Obj(int i){
        this.i = i;
    }
}

-Client-

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

import javax.swing.JOptionPane;


public class TestClient {

    static Vector<Obj> obj;
    static ClientThread ct;

    public static void main(String[] args){
        obj = new Vector<Obj>();
        connect();

        for(int i = 0; i < obj.size(); i++){
            System.out.println("Obj: " + obj.get(i).i);
        }

        System.exit(0);
    }

    public static void connect(){
        String ip = JOptionPane.showInputDialog("Input server IP.");
        ct = new ClientThread(ip, obj);
        ct.start();
        ct.setPriority(Thread.MAX_PRIORITY);
    }
}

class Obj implements Serializable{
    int i;

    public Obj(int i){
        this.i = i;
    }
}

class ClientThread extends Thread implements Runnable{
    Socket socket;
    Vector<Obj> obj;
    DataOutputStream out;
    ObjectInputStream in;
    boolean loop = true;

    @SuppressWarnings("unchecked")
    public ClientThread(String ip, Vector<Obj> obj){
        super("ClientThread");

        try{
            socket = new Socket(ip, 4444);
            socket.setTcpNoDelay(true);
            out = new DataOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
            this.obj = obj; 
            this.obj = (Vector<Obj>) in.readObject();
        }

        catch(Exception e){
            e.printStackTrace();
        }
    }

    public void run(){
        try{
            while(loop){
                try{
                    if(!socket.isClosed() && socket.isConnected()){         
                        obj = (Vector<Obj>) in.readObject();
                        this.sleep(15);
                    }

                    else
                        loop = false;

                }
                catch(Exception e){
                        e.printStackTrace();
                        socket.close();
                }  
            }



        }

        catch(Exception e){
            e.printStackTrace();
        }
    }
}

Port 4444 has been opened.

Getting this error:

Accepted. Now creating I/O.

player connected. 

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at ClientThread.run(TestServer.java:49)
  • 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-17T03:02:11+00:00Added an answer on May 17, 2026 at 3:02 am

    You got this error because client terminates with
    System.exit(0);
    call in line 21.

    I see that it’s just a test but it is good form to write careful code in tests too.
    -use right visibility modificators
    -close streams at finally block
    -use ArrayList instead of Vector if you don’t need thread safety during operations with it
    -etc

    This code contains many potential bugs that is why real (quite simple) problem wasn’t noticed at first site.

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

Sidebar

Related Questions

Okay, so I'm working on a java server for an Apps backend, it must
Okay, I have a WinForms C# program I am writing in Visual Studio 2010
Okay so I made this program to help me out with my homework and
Okay so here is my database: I have a page on my .net form
Okay so I have a simple HTML Input fragment: <input id=txtFirstName type=text class=txtBox runat=server
Okay, I have hundreds of .net controls with text attributes that needs to be
Okay, so I've tried to use sort to vector of items so the size
Okay, so I'm running a small test webserver on my private network. I've got
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay so I have two import pieces of code involved in this. This first

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.