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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:09:01+00:00 2026-05-24T00:09:01+00:00

I’m trying to transfer an ArrayList from a client to a server using the

  • 0

I’m trying to transfer an ArrayList from a client to a server using the UDP protocol.

The transfer starts at the “max” if statement.
Same in the server side

This is the client:

public class UdpClient {
protected DatagramPacket sendPacket;
protected DatagramPacket receivePacket;

public static void main(String args[]) throws IOException,
        ClassNotFoundException {
    UdpClient upd = new UdpClient();
    ArrayList<Integer> arr = new ArrayList<Integer>();
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
            System.in));
    DatagramSocket clientSocket = new DatagramSocket();
    byte[] sendData = new byte[1024];
    byte[] receiveData = new byte[1024];
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    ObjectOutput oo = new ObjectOutputStream(bStream);
    ByteArrayInputStream baos;
    ObjectInputStream oos;
    System.out
            .println("Commands: Time, Date, Weather, Sum-number, Max-number, Exit");
    while (true) {
        String fromUsr = inFromUser.readLine();
        if (fromUsr.equals("bye")) {
            break;
        } else if (fromUsr.equals("weather")) {
            sendData = fromUsr.getBytes();
            upd.sendPacket(sendData, clientSocket);
            System.out
                    .println("Please select a ctiy: Lund, Malmo, Stockholm");
            String weather = inFromUser.readLine();
            sendData = weather.getBytes();
            upd.sendPacket(sendData, clientSocket);
            upd.receivePacket(clientSocket, receiveData);
        } else if (fromUsr.equals("max")) {
            sendData = fromUsr.getBytes();
            upd.sendPacket(sendData, clientSocket);
            String max = inFromUser.readLine().trim();
            upd.nums(max, arr);
            oo.writeObject(arr);
            byte[] buf = new byte[bStream.toByteArray().length];
            buf = bStream.toByteArray();
            upd.sendPacket(buf, clientSocket);
            System.out.println(arr);
        } else {
            // send data that of the user
            sendData = fromUsr.getBytes();
            upd.sendPacket(sendData, clientSocket);
            upd.receivePacket(clientSocket, receiveData);
        }
        String fromServer = new String(upd.getData());
        System.out.println("Message from server:\n" + fromServer);
    }
}

private void sendPacket(byte[] sendData, DatagramSocket clientSocket)
        throws IOException {
    InetAddress IPAddress = InetAddress.getByName("ericman-PC");
    sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress,
            9876);
    clientSocket.send(sendPacket);

}

private void receivePacket(DatagramSocket serverSocket, byte[] receiveData)
        throws IOException {
    byte[] rData = new byte[1024];
    receiveData = rData;
    receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
}

This is the Server

    public class UdpServer {
protected DatagramPacket receivePacket;
protected DatagramPacket sendPacket;

public static void main(String args[]) throws IOException,
        ClassNotFoundException {
    UdpServer upd = new UdpServer();
    DatagramSocket serverSocket = new DatagramSocket(9876);
    DateFormat currentDate = new SimpleDateFormat("yyyy/MM/dd");
    DateFormat currentTime = new SimpleDateFormat("HH:mm:ss");
    Date date = new Date();
    ByteArrayInputStream baos;
    ObjectInputStream oos;
    byte[] sendData = new byte[1024];
    byte[] receiveData = new byte[1024];

    while (true) {
        System.out.println("****************************************"
                + "\nServer is connected");
        upd.receivePacket(serverSocket, receiveData);
        String str = new String(upd.getData()).trim();
        System.out.println("Message received is:" + " " + str);
        if (str.equals("time")) {
            str = currentTime.format(date);
            sendData = str.getBytes();
            upd.sendPacket(serverSocket, sendData);
        } else if (str.equals("date")) {
            str = currentDate.format(date);
            sendData = str.getBytes();
            upd.sendPacket(serverSocket, sendData);
        } else if (str.equals("weather")) {
            upd.receivePacket(serverSocket, receiveData);
            str = upd.weather(str = new String(upd.getData()).trim());
            sendData = str.getBytes();
            upd.sendPacket(serverSocket, sendData);

        } else if (str.equals("max")) {
            byte[] buf = new byte[1024];
            System.out.println("waitng for object to come");
            upd.receivePacket(serverSocket, buf);
            baos = new ByteArrayInputStream(buf);
            oos = new ObjectInputStream(baos);
            Object o = oos.readObject();
            System.out.println(o);
        } else {
            str = "Unknown command, please try again..";
            sendData = str.getBytes();
            upd.sendPacket(serverSocket, sendData);
        }

    }
}

This is the error i get in the Server side

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 00000000
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at UdpServer.main(UdpServer.java:54)

Line 54 that the error indicates is this line on the server

baos = new ByteArrayInputStream(buf);
            oos = new ObjectInputStream(baos);

If you could help me out of why this error is happening? thanx!

  • 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-24T00:09:02+00:00Added an answer on May 24, 2026 at 12:09 am

    Your code creates alot of arrays it immediately discards. I would use a debugger to step through the code so you understand what it is doing.

    private void receivePacket(DatagramSocket serverSocket, byte[] receiveData)
            throws IOException {
        // create a new array
        byte[] rData = new byte[1024];
        // throw away the orignal array so the new array will be update.
        receiveData = rData;
        // copy into the new array, not the old one.
        receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
    }
    

    In this case, the original receiveData will not me modified so it will contain lots of 0 bytes.


            byte[] buf = new byte[bStream.toByteArray().length];
            buf = bStream.toByteArray();
    

    This creates three arrays when all you need is one. The first array bStream.toByteArray() is created just so you can determine what length it would be. You create a second array which is the same length but empty and put it in buf Finally you discard the second array and replace it with a copy of the first array.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.