//package comm; import java.io.*; import java.net.*; import java.util.*; public class Sender { public int desPort; //destination port public int selfPort; //send Port public Sender(int selfPort)//,int desPort) { //this.desPort=desPort; this.selfPort=selfPort; } public void sendTo(Object o,int desPort) { try { InetAddress address = InetAddress.getByName("localhost"); //System.out.println(address); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(selfPort); ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(byteStream)); os.flush(); os.writeObject(o); os.flush(); //retrieves byte array byte[] sendBuf = byteStream.toByteArray(); DatagramSocket dSock=new DatagramSocket(selfPort); DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, address, desPort); int byteCount = packet.getLength(); dSock.send(packet); os.close(); dSock.close(); } catch (UnknownHostException e) { System.err.println("Exception: " + e); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* public static void main(String args[]) { Sender s1=new Sender(Integer.parseInt(args[0]),Integer.parseInt(args[1])); Vector v=new Vector(); v.add("Mithun"); v.add("Jekkin"); v.add("ihong"); v.add("jolly"); s1.sendTo(v); }*/ }