By default udp sockets are made using datagramsocket

This preview shows page 6 - 8 out of 8 pages.

By default, UDP sockets are made using DatagramSocket . The idea behind datagram sockets is that the packets contain the information about Table 3: Datagram Socket methods java.net.DatagramSocket DatagramSocket() Creates a datagram sockets and binds it to any free UDP port in the system. DatagramSocket(int port) Creates a datagram socket and binds to the port . void receive(DatagramPacket p) Receives a packet. void send(DatagramPacket p) Sends a packet. 4.1 Example of Use As an example, we try to send a datagram packet to a given socket. The datagram includes a test string. The receiver should look like this: import java.io.*; import java.net.*; public class UDPReceiver { /** * Prints the usage and exits. */ public static void usage(){ 6
Image of page 6

Subscribe to view the full document.

System.out.println("Usage: java UDPReceiver port_number\n this program reads a Datagram received through a UDP socket bound to specified port."); System.exit(0); } public static void main(String[] args) { OutputStream out=null; try{ String text="test"; byte[] b = new byte[100]; DatagramPacket dp=new DatagramPacket(b,100); // checks the arguments if (args.length!=1) throw new Exception("Bad number of arguments."); // creates the socket DatagramSocket s=new DatagramSocket(Integer.parseInt(args[0])); s.receive(dp); System.out.println(new String(dp.getData())); } catch (Exception E){ usage(); } } } The sender could look like this: import java.io.*; import java.net.*; public class UDPTest { /** * Prints the usage and exits. */ public static void usage(){ System.out.println("Usage: java SocketInteractor local_port host remote_port\n this System.exit(0); } public static void main(String[] args) { OutputStream out=null; try{ String text="test"; // checks the arguments if (args.length!=3) throw new Exception("Bad number of arguments."); // creates the socket DatagramSocket s=new DatagramSocket(Integer.parseInt(args[0])); s.connect(InetAddress.getByName(args[1]),Integer.parseInt(args[2])); s.send(new DatagramPacket(text.getBytes(),text.length())); } catch (Exception E){ usage(); } } 7
Image of page 7
} 5 Exercise 1. Read the APIs for MulticastSocket and try to use it. As indicated, it is a DatagramSocket . 2. Use the example and code a minimalistic Web server. 3. Use the example and code a minimalisstic FTP client. 4. Try to program a UDP based webserver. What do you think of the ap-proach? What could be the interest?
Image of page 8

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern