UDP send object - Java Network

Java examples for Network:UDP

Description

UDP send object

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class Main {
    public static void send(String ip, int port, Serializable obj) {

        try {//from   w  ww .ja  v a 2  s.c  om
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);

            byte[] buf = baos.toByteArray();
            System.out.println(buf.length);

            DatagramPacket dp = new DatagramPacket(buf, buf.length,
                    new InetSocketAddress(ip, port));
            DatagramSocket ds = new DatagramSocket(null);
            ds.send(dp);
            ds.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Related Tutorials