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

  • Home
  • SEARCH
  • 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 8087445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:46:46+00:00 2026-06-05T18:46:46+00:00

I have a problem with a Sender and a Server UDP. The SenderUDP sends

  • 0

I have a problem with a Sender and a Server UDP. The SenderUDP sends an object and the ServerUDP must receive it. For the first object all works fine, but when I try to send another object the server throws a StreamCorruptedException.
Here the client side code:

public class SenderUDP implements Runnable {
private java.net.DatagramSocket clientsocket;
private ObjectOutputStream out;
private int port;
private InetAddress ip;
private Packet objToSend;
private ByteArrayOutputStream baos;


public SenderUDP(String ip, int port, Packet p) throws UnknownHostException{
    this.ip =InetAddress.getByName(ip);
    this.port = port;
    this.objToSend = p;
    this.out = null;
    this.clientsocket = null;
}

@Override
public void run(){
    try{
        clientsocket = new DatagramSocket ();
        System.out.println("Inside senderUDP");
        byte[] sendData;                    
        baos = new ByteArrayOutputStream(1024);
        out = new ObjectOutputStream(baos);
        out.writeObject(objToSend);
        sendData = baos.toByteArray();           
        DatagramPacket sendpacket = new DatagramPacket(sendData,sendData.length,ip,port);            
        clientsocket.send(sendpacket);                 
        System.out.println("Sended packet with UDP");
        out.flush();
        if(this.objToSend.getOP() == 1){
            byte[] buf = new byte[1024];                
            int read;
            ByteArrayOutputStream bas = new ByteArrayOutputStream((int)this.objToSend.getFile().length());                
            FileInputStream fis = new FileInputStream(objToSend.getFile());
            while((read = fis.read(buf)) != -1){
                bas.write(buf, 0, read);
            }
            DatagramPacket sendfile = new DatagramPacket(bas.toByteArray(), bas.toByteArray().length, ip, port);
            clientsocket.send(sendfile);
        }           
        out.close();             
    }
    catch(UnknownHostException uhe) {
        System.err.println(uhe.getMessage());
    }
    catch(IOException ioe) {
        System.err.println(ioe.getMessage());
    }
}

}

Here is the server side code:

class ServerUDP implements Runnable {
private DatagramSocket socket;
private int port;
private Controller controller;
private byte[] buffer;
private DatagramPacket packet;
private Packet p;
private ObjectInputStream ois;

public ServerUDP(int port, Controller controller){
    this.socket = null;
    this.port = port;
    this.controller = controller;
}

@Override
public void run() {
    try {
            socket = new DatagramSocket(port);
        } catch (SocketException ex) {
            Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
        }
    while(true){               
        buffer = new byte[1000000];
        packet = new DatagramPacket(buffer,buffer.length);
        System.out.println("Ascolto UDP!");
        try {                
            socket.receive(packet);
            System.out.println(packet);
            System.out.println("1");
        } catch (IOException ex) {
            Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Packet UDP Received!");
        try {
            ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
        } catch (IOException ex) {
            Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            p = (Packet) ois.readObject();
            System.out.println("Pacchetto/Evento arrivato con UDP!");
            System.out.println(p);
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(p.getOP() == 1){
            Thread t = new Thread(new FilesManager(socket,p,false, controller));
            t.start();
        }
        controller.enqueue(p);
        try {
            ois.close();
        } catch (IOException ex) {
            Logger.getLogger(ServerUDP.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

}

The StreamCorruptedException is thrown by

ois = new ObjectInputStream(new ByteArrayInputStream(buffer));

and

p = (Packet) ois.readObject();

Thanks!

  • 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-06-05T18:46:48+00:00Added an answer on June 5, 2026 at 6:46 pm

    Umm, you totally forgot to implement a protocol! You forgot to do transmit pacing, duplicate datagram detection, checksums, datagram reordering, retransmissions, acknowledgements, slow start, sliding windows, and pretty much everything else! You may not absolutely need all of these things, but you need most of them or you need to accomplish these basic functions some other way.

    You’ve done about 1% of the job of sending data using UDP. It won’t work until you finish the job.

    If you want minimal file transfer over UDP, then TFTP is what you want to study. Here’s a description of how a file transfer occurs over UDP:

    The initiating host A sends an RRQ (read request) or WRQ (write request) packet to host S at the well-known port number 69, containing the filename and transfer mode.

    S replies with an ACK (acknowledgement) packet to WRQ and directly with a DATA packet to RRQ. Packet is sent from a freshly allocated ephemeral port, and all future packets to host S should be to this port.

    The source host sends numbered DATA packets to the destination host, all but the last containing a full-sized block of data (512 bytes). The destination host replies with numbered ACK packets for all DATA packets.

    The final DATA packet must contain less than a full-sized block of data to signal that it is the last. If the size of the transferred file is an exact multiple of the block-size, the source sends a final DATA packet containing 0 bytes of data.

    Receiver responds to each DATA with associated numbered ACK. Sender responds to the first received ACK of a block with DATA of the next block.

    If an ACK is not eventually received, a retransmit timer resends DATA packet.

    Trivial File Transfer Protocol – Wikipedia

    This avoids the need to do transmit pacing, slow start, windows, or datagram reordering by only keeping one chunk of data “in flight” at a time. That makes it very slow, but also pretty simple. As described above, you can at best exchange 512 bytes per round trip time.

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

Sidebar

Related Questions

Description of problem: I have an updatepanel that works fine in all browsers in
I have unusual problem with AJAX toolkit ColorPickerExtender. Javascript code works fine it changes
i have problem with autorotate on iphone i set up in all classes -
I have a problem with an J2ME client app, that sends data to an
I have a problem that has been bugging me all day. In my code
I have problem with sending objects via TCPClient. At first, I serialize them into
Currently I have this problem. Client downloads from server successfully only the 1st time.
Ok, simple problem hopefully. I have started to make a simple server for a
Have such a problem We have claim awared asp.net site and adfs server configured
I have got a Client/Server Application that using Asynchronous Socket.My problem is i cant

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.