DOC PREVIEW
WMU CS 5550 - JAVA UDP Sockets

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JAVA UDP SocketsJAVA - Internet Addresses java.net.InetAddress class You get an address by using static methods: Create InetAddress object representing the local machineInetAddress myAddress = InetAddress.getLocalHost();  Create InetAddress object representing some remote machineInetAddress ad = InetAddress.getByName(hostname);JAVA - Printing Internet Addresses You get information from an InetAddressby using methods:ad.getHostName();ad.getHostAddress();JAVA - The InetAddress Class Handles Internet addresses both as host names and as IP addresses Static Method getByName returns the IP address of a specified host name as an InetAddress object Methods for address/name conversion:public static InetAddress getByName(String host) throws UnknownHostExceptionpublic static InetAddress[] getAllByName(String host) throws UnknownHostExceptionpublic static InetAddress getLocalHost() throws UnknownHostExceptionpublic boolean isMulticastAddress()public String getHostName()public byte[] getAddress()public String getHostAddress()public int hashCode()public boolean equals(Object obj)public String toString()import java.net.*;import java.io.*;public class IPFinder{public static void main(String[] args) throws IOException{String host;BufferedReader input =new BufferedReader(new InputStreamReader(System.in));System.out.print("\n\nEnter host name: ");host = input.readLine();try {InetAddress address = InetAddress.getByName(host);System.out.println("IP address: " + address.toString());}catch (UnknownHostException e){System.out.println("Could not find " + host);}}}Retrieving the address of the local machineimport java.net.*;public class MyLocalIPAddress{public static void main(String[] args) {try {InetAddress address = InetAddress.getLocalHost();System.out.println (address);}catch (UnknownHostException e) {System.out.println("Could not find local address!");}}}The UDP classes 2 classes:  java.net.DatagramSocket class is a connection to a port that does the sending and receiving. ADatagramSocket can send to multiple, different addresses.The address to which data goes is stored in the packet, not in the socket.public DatagramSocket() throws SocketExceptionpublic DatagramSocket(int port) throws SocketExceptionpublic DatagramSocket(int port, InetAddress laddr) throws SocketException java.net.DatagramPacket class is a wrapper for an array of bytes from which data will be sent or into which data will be received. It also contains the address and port to which the packet will be sent.public DatagramPacket(byte[] data, int length)public DatagramPacket(byte[] data, int length, InetAddress host, intport)Datagram SocketsSERVER:1. Create a DatagramSocket objectDatagramSocket dgramSocket = new DatagramSocket(1234);2. Create a buffer for incoming datagramsbyte[] buffer = new byte[256];3. Create a DatagramPacket object for the incoming datagramDatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);4. Accept an incoming datagramdgramSocket.receive(inPacket)Datagram SocketsSERVER:5. Accept the sender’s address and port from the packetInetAddress clientAddress = inPacket.getAddress();int clientPort = inPacket.getPort();6. Retrieve the data from the bufferstring message = new String(inPacket.getData(), 0, inPacket.getLength());7. Create the response datagramDatagramPacket outPacket = new DatagramPacket(response.getBytes(), response.length(),clientAddress, clientPort);8. Send the response datagramdgramSocket.send(outPacket)9. Close the DatagramSocket: dgram.close();Datagram Sockets CLIENT:1. Create a DatagramSocket objectDatagramSocket dgramSocket = new DatagramSocket;2. Create the outgoing datagramDatagramPacket outPacket = new DatagramPacket(message.getBytes(),message.length(),host, port);3. Send the datagram messagedgramSocket.send(outPacket)4. Create a buffer for incoming datagramsbyte[] buffer = new byte[256];Datagram SocketsCLIENT:5. Create a DatagramPacket object for the incoming datagramDatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);6. Accept an incoming datagramdgramSocket.receive(inPacket)7. Retrieve the data from the bufferstring response = new String(inPacket.getData(), 0, inPacket.getLength());8. Close the DatagramSocket: dgram.close();Sending UDP packets When you receive a packet, the IP and port number of the sender are set in the DatagramPacket. You can use the same packet to reply, by overwriting the data, using the method: packet.setData(newbuffer);Non-blocking I/O receiving UDP packets You can set a time-out in milliseconds to determine how long a read operation blocks, before throwing an exception. socket.setSoTimeout(duration); If the duration given in milliseconds is exceeded, an exception is thrown: java.io.InterruptedExceptionReferences Cisco Networking Academy Program (CCNA), Cisco Press. CSCI-5273 : Computer Networks, Dirk Grunwald, University of Colorado-Boulder CSCI-4220: Network Programming, Dave Hollinger, Rensselaer Polytechnic Institute. TCP/IP Illustrated, Volume 1, Stevens. Java Network Programming and Distributed Computing, Reilly & Reilly. Computer Networks: A Systems Approach, Peterson & Davie. http://www.firewall.cx


View Full Document

WMU CS 5550 - JAVA UDP Sockets

Download JAVA UDP Sockets
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JAVA UDP Sockets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JAVA UDP Sockets 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?