Socket Programming
public int getPort()
gives information on which port the Socket is connected to on the remote host
public int getLocalPort()
gives the port number for the local end of the connection
java.net.InetAddress class
a class used in most of the networking classes such as Socket, ServerSocket, URL, DatagramPacket and more
what is a socket
a socket is a connection between two hosts
IPv6
composed of eight blocks of four hexadecimal digits separated by colons
IPv4
composed of four unsigned bytes, each byte ranges from 0 to 255, also called dotted quad format
sockets seven basic functions
connect to a remote machine, send data, retrieve data, close a connection, blind a port, listen for incoming data, accept connection from remote machines on the bound port
public Socket(String host, int port)
creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host
3 static methods of java.net.InetAddress
getByName(), getAllByName() and getLocalHost()
public InetAddress getInetAddress()
gives information about the remote host the Socket is connected to
what does java.net.Socket do
performs client-side TCP operations
how java programs normally use client sockets
program creates a new socket with the constructor, socket attempts to connect to the remote host, once the connection is established the local and the remote hosts can exchange data, one or both sides closes the connection after the data transmission is complete
why is a socket bound to a port number
so that the TCP layer can identify the application that data is destined to be sent to
public InetAddress getLocalAddress()
tells which network interface a socket is bound to