java networking

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


Imports Needed:*

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:


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 "" then " ||" would print out.


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+= ".";

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


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.


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 "" 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 (


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

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


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