Other internet
communication

Dr Andy Evans

[Fullscreen]

Introduction

  • Communication is via a port, using a protocol. Several protocols may be involved at once. Most use the following over the internet...
  • Internet Protocol (IP)
    Used to split data into small chunks called "packets" Addresses them to the right machine.
  • Transport Control Protocol (TCP)
    Guarantees packets get to their destination.
    Controls the route taken and lets computers confirm receipt.
    Adds packets back together in the right order.
  • HyperText Transfer Protocol (HTTP)

java.net.InetAddress

  • We saw the URL class, and how it encapsulates the location of things on a network.
  • There is a second address class that deals with finding machine IP address-specific information.
    java.net.InetAddress
  • This has no constructors. You create one using the class's static methods.

InetAddress creation

  • InetAddress ina = InetAddress.getLocalHost();
    Returns an InetAddress object representing the machine the program is running on.
  • InetAddress ina = InetAddress.getByName(hostNameString);
    Returns an InetAddress corresponding to the host named.
  • InetAddress[] ina = InetAddress.getAllByName(siteNameString);
    Returns an array of InetAddress objects containing all the servers associated with a given site name.

InetAddress methods

  • IP addresses are numeric: http://129.11.93.2 will get the old School webserver.
  • However, many machines hold a registry which contains the numbers and what they'd prefer to be called. This scheme is called the Domain Name Service (DNS)
    http://www.geog.leeds.ac.uk
  • Methods exist for finding a numerical internet address (IP address) from the domain name, and vice versa.
  • These methods might have to do the DNS look-up on another computer on the network - this may take some time.

Sockets

  • Once you have your address, you can then connect to it. To do this, we use "sockets".
  • These are TCP/IP based stream connections to a computer port. You need a client and a server.
  • java.net.Socket
    Sends requests to communicate to a port using TCP or equivalent.
  • java.net.ServerSocket
    Lets you build your own server.
    Sits and listens to a port in case something calls it.

Sockets

  • Socket(InetAddress ipAdd, int port)
    ServerSocket(int port)
  • TCP/IP reserves the first 1024 ports, otherwise you can use most numbers up to 49151.
  • A ServerSocket is set to listen with its accept() method which returns a Socket when activated.
  • Once you have your sockets connected, you can use their getInputStream(), getOutputStream() and close() methods to talk using streams.

Sockets example

  • On the server...
    
    ServerSocket ss = new ServerSocket(9999);
    Socket s = ss.accept();
    BufferedInputStream in =  
       new BufferedInputStream(s.getInputStream());
    
    
  • Start the program.
  • Once a connection has been accepted, use the socket to communicate, not the ServerSocket.
  • On the client...
    
    Socket s = new Socket(iNetAddress, 9999);
    BufferedOutputStream b  = 
       new BufferedOutputStream(s.getOutputStream());
    b.write(aByteArray);
    s.close();
    
    

Ports and Firewalls

  • Always check what other programs, if any, use the port you are on.
  • Some networks have "Firewalls". These are security devices that sit on Ports and stop some connecting. Check for them.

Review

  • Most networks use TCP/IP to communicate.
  • HTTP gets and sends specific data-type requests on TCP/IP networks.
  • We can encapsulate specific machine addresses with an InetAddress object, and specific resources with a URL object.
  • Using a URL/URLConnection we can get basic resource types from a http / web server.
  • For more complex data and object transmission, we can set up sockets.
  • We can use ServerSocket objects to make our own servers.