Java Networking
Java Networking
Java Networking
Socket
A socket is an endpoint between two way communication.
Important methods
Method Description
1) public InputStream getInputStream() returns the InputStream
attached with this socket.
2) public OutputStream getOutputStream() returns the OutputStream
attached with this socket.
3) public synchronized void close() closes this socket
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is
used to establish communication with the clients.
Important methods
Method Description
1) public Socket accept() returns the socket and establish a
connection between server and
client.
2) public synchronized void close() closes the server socket.
Creating Server:
Creating Client:
To create the client application, we need to create the instance of Socket
class. Here, we need to pass the IP address or hostname of the Server and a
port number. Here, we are using "localhost" because our server is running
on same system.
Java URL
The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a
resource on the World Wide Web.
https://www.google.com/java-notes
4. File Name or directory name: In this case, “java-notes” is the file name.
URL(String spec)
Creates an instance of a URL from the given protocol, host, port number, and file.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Creates an instance of a URL from the given protocol, host, port number, file, and handler.
Creates an instance of a URL from the given protocol name, host name, and file name.
Creates an instance of a URL by parsing the given spec within a specified context.
URL(URL context, String spec, URLStreamHandler handler)
Creates an instance of a URL by parsing the given spec with the specified handler within a given
context.
The java.net.URL class provides many methods. The important methods of URL class are given
below.
Method Description
public String getProtocol() it returns the protocol of the URL.
public String getHost() it returns the host name of the URL.
public String getPort() it returns the Port Number of the URL.
public String getFile() it returns the file name of the URL.
public String getAuthority() it returns the authority of the URL.
public String toString() it returns the string representation of the URL.
public String getQuery() it returns the query string of the URL.
public String getDefaultPort() it returns the default port of the URL.
public URLConnection it returns the instance of URLConnection i.e. associated with this
openConnection() URL.
public boolean equals(Object obj) it compares the URL with the given object.
public Object getContent() it returns the content of the URL.
public String getRef() it returns the anchor or reference of the URL.
public URI toURI() it returns a URI of the URL.
The Java URLConnection class represents a communication link between the URL and the
application. This class can be used to read and write data to the specified resource referred by the
URL.
The openConnection() method of URL class returns the object of URLConnection class. Syntax:
The URLConnection class provides many methods, we can display all the data of a webpage by
using the getInputStream() method. The getInputStream() method returns all the data of the
specified URL in the stream that can be read and displayed.
The Java HttpURLConnection class is http specific URLConnection. It works for HTTP
protocol only.
By the help of HttpURLConnection class, you can information of any HTTP URL such as header
information, status code, response code etc.
The java.net.HttpURLConnection is subclass of URLConnection class.
The openConnection() method of URL class returns the object of URLConnection class. Syntax:
Moreover, InetAddress has a cache mechanism to store successful and unsuccessful host name
resolutions.
Method Description
public static InetAddress getByName(String host) throws it returns the instance of InetAddress containing
UnknownHostException LocalHost IP and name.
public static InetAddress getLocalHost() throws it returns the instance of InetAdddress containing
UnknownHostException local host name and address.
public String getHostName() it returns the host name of the IP address.
public String getHostAddress() it returns the IP address in string format.
Java DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
A datagram is basically an information but there is no guarantee of its content, arrival or arrival
time.
DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port
Number on the localhost machine.
DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given
Port Number.
DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and
binds it with the specified port number and host address.
Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it
may arrive in any order. Additionally, packet delivery is not guaranteed.
DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used to receive
the packets.
DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram packet. This
constructor is used to send the packets.