SWE1007 - Programming in Java: Prof. A. Vijayarani
SWE1007 - Programming in Java: Prof. A. Vijayarani
in Java
By
Prof. A. Vijayarani
SITE
VIT
1
Unit VI
Networking Introduction to Networking in Java-What is TCP
and UDP-What is Socket and Port-Implementation of Socket
and InetAddress class-URL in terms of Java networking
programming-Datagram in network environment-Retrieve the
IP address from Host Name, vice-versa.
Programming in Java 2
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Networking
The term network programming refers to writing programs that
execute across multiple devices (computers), in which the devices
are all connected to each other using a network.The java.net
package provides support for the two common network protocols:
TCP: TCP stands for Transmission Control Protocol, which
allows for reliable communication between two applications.
TCP is typically used over the Internet Protocol, which is
referred to as TCP/IP.
UDP: UDP stands for User Datagram Protocol, a connection-
less protocol that allows for packets of data to be transmitted
between applications.
Programming in Java 3
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Networking URL
Processing
URL stands for Uniform Resource Locator and represents a
resource on the World Wide Web, such as a Web page or FTP
directory.
A URL can be broken down into parts, as follows:
protocol://host:port/path?query#ref
Examples of protocols include HTTP, HTTPS, FTP, and File. The path
is also referred to as the filename, and the host is also called the
authority.
The following is a URL to a web page whose protocol is HTTP:
http://www.amrood.com/index.htm?language=en#j2se
Programming in Java 4
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
URL - Constructors
The java.net.URL class represents a URL and has a complete set
of methods to manipulate URL in Java.
The URL class has several constructors for creating URLs,
including the following:
Programming in Java 5
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
URL Class Methods
The URL class contains many methods for accessing the various
parts of the URL being represented. Some of the methods in the URL
class include the following:
Methods Description
public String getPath() Returns the path of the URL.
public String getQuery() Returns the query part of the URL.
public String getAuthority() Returns the authority of the URL.
Programming in Java 6
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
URL Class Methods
Methods Description
public String getHost() Returns the host of the URL.
public String getFile() Returns the filename of the URL.
Programming in Java 7
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
URL Sample Program
import java.net.*;
import java.io.*;
public class URLDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.amrood.com/index.htm?
language=en#j2se");
System.out.println("URL is " + url.toString());
System.out.println("protocol is " + url.getProtocol());
System.out.println("authority is " + url.getAuthority());
System.out.println("file name is " + url.getFile());
System.out.println("host is " + url.getHost());
System.out.println("path is " + url.getPath());
System.out.println("port is " + url.getPort());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
}catch(IOException e) {
e.printStackTrace(); } } }
Programming in Java 8
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
URL Sample Program
Output:
URL is http://www.amrood.com/index.htm?language=en#j2se
protocol is http
authority is www.amrood.com
file name is /index.htm?language=en
host is www.amrood.com
path is /index.htm
port is -1
default port is 80
query is language=en
ref is j2se
Programming in Java 9
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket Programming
Sockets provide the communication mechanism between two
computers using TCP. TCP is a two-way communication protocol. A
client program creates a socket on its end of the communication and
attempts to connect that socket to a server.
Programming in Java 10
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket - TCP Connection
The following steps occur when establishing a TCP connection
between two computers using sockets:
The server instantiates a ServerSocket object, denoting
which port number communication is to occur on.
The server invokes the accept() method of the ServerSocket
class. This method waits until a client connects to the server
on the given port.
After the server is waiting, a client instantiates a Socket
object, specifying the server name and the port number to
connect to.
The constructor of the Socket class attempts to connect the
client to the specified server and the port number. If
communication is established, the client now has a Socket
object capable of communicating with the server.
Programming in Java 11
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket TCP Connection
On the server side, the accept() method returns a reference to
a new socket on the server that is connected to the client's
socket.
After the connections are established, communication can
occur using I/O streams. Each socket has both an
OutputStream and an InputStream. The client's OutputStream
is connected to the server's InputStream, and the client's
InputStream is connected to the server's OutputStream.
TCP is a two-way communication protocol, hence data can be
sent across both streams at the same time.
Programming in Java 12
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Server Socket Class -
Constructors
The java.net.ServerSocket class is used by server applications
to obtain a port and listen for client requests. The ServerSocket
class has four constructors:
Programming in Java 13
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Server Socket Class -
3 Constructors
public ServerSocket(int port, int backlog, InetAddress
address) throws IOException
Similar to the previous constructor, the InetAddress parameter specifies
the local IP address to bind to. The InetAddress is used for servers that
may have multiple IP addresses, allowing the server to specify which of
its IP addresses to accept client requests on.
4 public ServerSocket() throws IOException
Creates an unbound server socket. When using this constructor, use the
bind() method when you are ready to bind the server socket.
If the ServerSocket constructor does not throw an exception, it means
that your application has successfully bound to the specified port and is
ready for client requests.
Programming in Java 14
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Server Socket Class -
Methods
1 public int getLocalPort()
Returns the port that the server socket is listening on. This method is
useful if you passed in 0 as the port number in a constructor and let
the server find a port for you.
2 public Socket accept() throws IOException
Waits for an incoming client. This method blocks until either a client
connects to the server on the specified port or the socket times out,
assuming that the time-out value has been set using the
setSoTimeout() method. Otherwise, this method blocks indefinitely.
3 public void setSoTimeout(int timeout)
Sets the time-out value for how long the server socket waits for a
client during the accept().
Programming in Java 15
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Server Socket Class -
Methods
4 public void bind(SocketAddress host, int backlog)
Binds the socket to the specified server and port in the
SocketAddress object. Use this method if you have instantiated
the ServerSocket using the no-argument constructor.
When the ServerSocket invokes accept(), the method does not
return until a client connects. After a client does connect, the
ServerSocket creates a new Socket on an unspecified port and
returns a reference to this new Socket. A TCP connection now
exists between the client and the server, and communication can
begin.
Programming in Java 16
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket Class -
Constructors
The java.net.Socket class represents the socket that both the
client and the server use to communicate with each other. The
client obtains a Socket object by instantiating one, whereas the
server obtains a Socket object from the return value of the
accept() method.
1 public Socket(String host, int port) throws
UnknownHostException, IOException.
This method attempts to connect to the specified server at the
specified port. If this constructor does not throw an exception, the
connection is successful and the client is connected to the server.
2 public Socket(InetAddress host, int port) throws
IOException
This method is identical to the previous constructor, except that the
host is denoted by an InetAddress object.
Programming in Java 17
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket Class -
3 public Constructors
Socket(String host, int port, InetAddress
localAddress, int localPort) throws IOException.
Connects to the specified host and port, creating a socket on the
local host at the specified address and port.
5 public Socket()
Creates an unconnected socket. Use the connect() method to
connect this socket to a server.
public int getPort() Returns the port the socket is bound to on the
remote machine.
public int getLocalPort() Returns the port the socket is bound to on the
local machine.
public SocketAddress Returns the address of the remote socket.
getRemoteSocketAddress()
Programming in Java 19
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT
Socket Class - Methods
Method Description
public InputStream Returns the input stream of the socket. The
getInputStream() throws input stream is connected to the output stream
IOException of the remote socket.
public OutputStream Returns the output stream of the socket. The
getOutputStream() throws output stream is connected to the input stream
IOException of the remote socket.
public void close() throws Closes the socket, which makes this Socket
IOException object no longer capable of connecting again to
any server.
Programming in Java 20
Author: Prof. A. Vijayarani, Asst. Prof., SITE, VIT