java networking

Topics: IP address, IPv4, Internet Protocol Pages: 5 (792 words) Published: November 16, 2013
The Basics of Java Networking
by Daniel Bloom

JAVA NETWORKING TUTORIAL

Imports Needed:

Java.io.*
Java.util.*
Java.net.*

Objects Used:

InetAddress - creates an IP connection to a specified host
Inet4Address - Creates an IPv4 connection to a specified host Inet6Address - Creates an IPv6 connection to a specified host SocketAddress - provides an immutable object used by sockets for binding, connecting, or as returned values. (Abstract class (used with InetAddress)) Socket - Opens a TCP socket to a specific IP address

ServerSocket - Creates a server for Sockets to connect to (Only needed for client/server networing) DatagramPacket - Creates a UDP packet which connects through the DatagramSocket DatagramSocket - creates a UDP socket to send DatagramPackets

Examples/explenations of all:

InetAddress:

public static void inetmethod() throws UnknownHostException{ InetAddress address =InetAddress.getByName(ADDRESS_AS_STRING_GOES_HERE); System.out.println(address.getHostAddress() +" || "+ address.getHostName());     }

The above doe will print out the IP address of anything given and it's host name. For example, if ADDRESS_AS_STRING_GOES_HERE was substituted with "www.google.com" then "173.194.46.20 || http://www.google.com" would print out.

Inet4Address:

import java.net.*;
import java.util.*;

public class GetAddress {

    public static void main(String[] args) throws Exception {         String result = "";
        InetAddress i = Inet4Address.getByName("localhost");         //returns the raw IP address of object.
        byte b[] = i.getAddress();
         System.out.print("IP address is: ");
        for (int j = 0; j < b.length; j++) {
            result+= b[j];
            result+= ".";
        }
        System.out.print(result);
    }
}

The above code will print out the IPv4 address of any given host. For example, the above code, will print out "IP address is: 127.0.0.1". However, if we substitute "localhost" with "www.google.com" the output will end up as "IP address is: 173.194.46.20"

Inet6Address:

public static void Inetsix throws UnknownHostException {
        InetAddress ipv6 = Inet6Address.getByName(ADDRESS_AS_STRING_GOES_HERE);         System.out.println(ipv6.getHostAddress());
        
    }

The above code would print out the IPv6 address of any given host. For example, if you replace ADDRESS_AS_STRING_GOES_HERE with a host that has an IPv6 address, it would print out said IPv6 address. However, due to the fact that I don't know of any IPv6 address using sites/hosts, I can not show you example output.

SocketAddress:

public static void socketaddressmethod(){
        SocketAddress address = new InetSocketAddress(HOST_AS_STRING, PORT_AS_INT);     }

The above code will set up a new connection to a given host on a given port. For example, if HOST_AS_STRING was replaced with "www.google.com" and PORT_AS_INT was replaced with 80, a new connection would be set up(accessible)(connections are made with a socket) with google on port 80 (http://www.google.com:80).

Socket:

public static void SocketExample(){
        SocketAddress address = new InetSocketAddress(HOST_AS_STRING, PORT_AS_INT);         Socket sock = new Socket();
        try {
            sock.connect(address);
            System.out.println("Connection made");         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

The above code will make a connection to any given host/port. For example, if HOST_AS_STRING was replaced with "www.google.com" and PORT_AS_INT was replaced with 80 a new connection would be made with google on port 80 (http://www.google.com:80) and "Connection made" would be printed to the screen.

ServerSocket:

public static void serversocketexample(){
        try {
            ServerSocket server = new ServerSocket();             while(true){...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • JAVA Essay
  • Java Essay
  • Java Essay
  • Java Project Essay
  • Essay on Java Cis Assignment 1
  • Compare and Contrast Java and Javascript Essay
  • Java Applets Essay
  • Essay on Computer Java

Become a StudyMode Member

Sign Up - It's Free