Java Networking: Anas Dange
Java Networking: Anas Dange
Anas Dange
M.E(CMPN), B.E(I.T)
Lecturer,
Dept. of Computer Engineering,
ARKP, Panvel
Defn.
The term network programming refers to
writing programs that execute across
multiple computers, in which the devices are
all connected to each other using a network.
The java.net package provides support for
TCP and UDP protocol.
Socket is an endpoint between two way
communication and provides facility to share
data between different computing devices.
Socket Programming
Java Socket programming can be connection-
oriented or connection-less.
Socket and ServerSocket classes are used for
connection-oriented socket programming.
DatagramSocket and DatagramPacket classes
are used for connection-less socket
programming.
Working
The server instantiates a ServerSocket object,
denoting the port number for communication.
The server invokes the accept method of the
ServerSocket class to connect to the client.
After the server is waiting, a client
instantiates a Socket object, specifying the
server name and port number to connect to.
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.
ServerSocket Class
This class is used by server applications to obtain a port
and listen for client requests.
Constructors:-
1. public ServerSocket(int port) Attempts to create a
server socket bound to the specified port.
2. public ServerSocket(int port, int backlog) the backlog
parameter specifies how many incoming clients to
store in a wait queue.
3. public ServerSocket (int port, int backlog,
InetAddress address) 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()
ServerSocket Class - Methods
1. public int getLocalPort ()
Returns the port that the server socket is listening on.
2. public Socket accept ()
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.
3. public void setSoTimeout(int timeout)
Sets the time-out value for how long the server socket
waits for a client during the accept.
4. public void close()
Closes the socket
Socket Class
This class represents the socket that both the client
and server use to communicate with each other.
Constructors:-
1. public Socket(String host, int port) This attempts to
connect to the specified server at the specified port.
2. public Socket(InetAddress host, int port) the host is
denoted by an InetAddress object.
3. public Socket(String host, int port, InetAddress
localAddress, int localPort) Connects to the specified
host and port, creating a socket on the local host at
the specified address and port.
4. public Socket(InetAddress host, int port, InetAddress
localAddress, int localPort) the host is denoted by an
InetAddress object instead of a String.
5. public Socket()Creates an unconnected socket.
Socket Class - Methods
1. public void connect(SocketAddress host, int timeout) This
method connects the socket to the specified host. This
method is needed only when you instantiated the Socket
using the no-argument constructor.
2. public InetAddress getInetAddres() This method returns the
address of the other computer that this socket is connected
to.
3. public int getPort() Returns the port the socket is bound to
on the remote machine.
4. public int getLocalPort() Returns the port the socket is
bound to on the local machine.
5. public InputStream getInputStream() Returns the input
stream of the socket.
6. public OutputStream getOutputStream() Returns the output
stream of the socket.
7. public void close() Closes the socket
URL Class
This class represents an URL. It points to a
resource on the World Wide Web.
A URL contains many information:
1. Protocol
2. Server name or IP Address:
3. Port Number: If port number is not
mentioned in the URL, it returns -1.
4. File Name or directory name
URL Class - Methods
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 URLConnection openConnection() it
returns the instance of URLConnection.
URLConnection Class
This 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.
public URLConnection openConnection()
It returns the instance of URLConnection.
URLConnection Class - Methods
1. Object getContent() Retrieves the contents of this
URL connection.
2. int getContentLength() Returns the value of the
content-length header field.
3. String getContentType() Returns the value of the
content-type header field.
4. int getLastModified() Returns the value of the last-
modified header field.
5. InputStream getInputStream() Returns the input
stream of the URL connection for reading from the
resource.
6. OutputStream getOutputStream() Returns the output
stream of URL connection for writing to the resource.
7. public URL getURL() Returns the URL that this
URLConnection object is connected to.
HttpURLConnection Class
The Java HttpURLConnection class is http
specific URLConnection. It works for HTTP
protocol only.
The java.net.HttpURLConnection is subclass
of URLConnection class.
You can typecast URLConnection type to
HttpURLConnection type as given below.