DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Datagram problem

    I am creating a program that runs on two computers, one runs a sender program, the other a receiver.

    I have coded it so that the sender sends a datagram to the other computer. This works fine.

    My problem is I now need to send a message back to the sender program. I have extracted the ip address, and the port, all i need to do is change the message in the datagram, and then send it back.

    My code is as follows:

    Sender
    ----------------------------------------------------------------------
    import java.net.*; // for network
    import java.util.*; // for utilities
    import java.io.*; // for streams
    import Command.*;

    public class ex11sender
    {

    public static void main(String[] argv)
    {
    try
    {
    InetAddress addr = InetAddress.getByName("IP.IP.IP.IP");
    //InetAddress addr = InetAddress.getLocalHost();
    int sendPortNumber = 2000;
    int portNumber = 5000;

    Command data = new Command("Alex","Gregory",10);

    DatagramSocket socket = new DatagramSocket(sendPortNumber);

    // now create and send packet
    ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
    ObjectOutputStream out = new ObjectOutputStream(bos);

    out.writeObject(data);
    byte[] buffer = bos.toByteArray();
    DatagramPacket packet =
    new DatagramPacket(buffer,buffer.length,addr,portNumber);



    System.out.println("ready to send on " +
    socket.getLocalAddress().toString() +
    " port: " + socket.getLocalPort());


    socket.send(packet);

    System.out.println("Packet data sent");

    }
    catch (UnknownHostException uh)
    {
    System.out.println("ERROR: " + uh);
    System.exit(0);
    }
    catch (IOException ioe)
    {
    System.out.println("ERROR: " + ioe);
    }
    }
    }

    ----------------------------------------------------------------------
    Receiver
    ----------------------------------------------------------------------
    import java.net.*; // for network
    import java.util.*; // for utilities
    import java.io.*; // for streams
    import Command.*;

    public class ex11receiver
    {

    public static void main(String[] argv)
    {
    try
    {
    int portNumber = 5000;
    int port;
    InetAddress address;


    // assume that machine and port to send to are in arg vector
    if (argv.length == 1)
    {
    portNumber = Integer.parseInt(argv[1]);
    }

    // now create and send packet

    byte[] buffer = new byte[1024];
    DatagramPacket packet =
    new DatagramPacket(buffer,buffer.length);

    DatagramSocket socket = new DatagramSocket(portNumber);
    socket.setSoTimeout(0);
    System.out.println("ready to receive on " +
    socket.getLocalAddress().toString() +
    " port: " + socket.getLocalPort());

    socket.receive(packet);
    address = packet.getAddress();
    port = packet.getPort();
    socket.send(packet);

    ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());
    ObjectInputStream in = new ObjectInputStream(bin);

    // now read object from stream

    Command data = (Command)in.readObject();

    System.out.println("Received " + data.toString());
    System.out.println("Port is " + port);
    System.out.println("Address is " + address);

    }
    catch (ClassNotFoundException nfe)
    {
    System.out.println("Failed to read data");
    }
    catch (IOException ioe)
    {
    System.out.println("ERROR: " + ioe);
    }
    }
    }
    Alex Gregory

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    yes.. you are correct!

    did you know, your post does not contain a single question mark?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links