java programming in network (UDP)

Posted on 2004-10-21
Last Modified: 2010-03-31
DatagramSocket socket = new DatagramSocket(2001);
DatagramPacket packet = new DatagramPacket(buf, buf.length,add,80);

can both of these port number be different?
Question by:sraveend
    LVL 92

    Assisted Solution

    yes (and they would connect to different ports)
    LVL 12

    Assisted Solution

    DatagramSocket socket = new DatagramSocket(2001);
    Constructs a datagram socket and binds it to the specified port on the local host machine.

    DatagramPacket packet = new DatagramPacket(buf, buf.length,add,80);
    Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

    >can both of these port number be different?
    Sure! If the host is the same and the port are the same, be carefull!

    Bye, Giant.
    LVL 2

    Expert Comment

    Yes and at receiving end all U got to do is

    Create a datagram socket bound to port 80 using
    DatagramSocket receivingSocket = new DatagramSocket(80);

    Then create a buffer of some length bigger than the lenth of the datagramPacket U r sending

    byte[] buffer = new byte[2000];

    Create a datagramPacket with this buffer using
    DatagramPacket dp = new DatagramPacket(buffer,buffer.length);

    then receive a packet using from DatagramSocket bound to port 80 using

    This populate the datagramPacket created above with the data sent to port 80

    Now to extract the data use

    byte[] dataSent = dp.getData();

    dataSent array contains the data sent to this port.

    Author Comment

    am confused,why do we've to use port number in datagramsocket as well as in datagrampacket.In TCP/IP  we use only once port number in sockets.
    LVL 2

    Accepted Solution

    Port number in DatagramSocket is the port on Ur machine using which U will be sending packets to... and Port number in DatagramPacket is the port number on the destination machine.

    In your case port number on the sending machine is 2001 so packets sent from this socket will have a source port set to 2001

    and Ur datagram packets are received at port 80 on the destination machine.

    Here U can use the same datagram socket at 2001 and send packets to destination machines at various ports like 500, 1000(port mentioned in datagramPacket) what ever....

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    sameEnds challenge 25 57
    nested interface 5 30
    Convert ArrayList of custom objects to JSON 4 44
    firstChar challenge 13 57
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now