0% found this document useful (0 votes)
12 views19 pages

Java Networking and Socket Programming

Uploaded by

rula mohammad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Java Networking and Socket Programming

Uploaded by

rula mohammad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Networking

What is Java Networking


• Java Networking is a concept of connecting two or more computing
devices together so that we can share resources.
• Java socket programming provides facility to share data between
different computing devices.
• The advantage of Java networking include:
sharing resources
centralize software management
Java Networking Terminology
The widely used java networking terminologies are:
• IP Address: IP address is a unique number assigned to a node of a
network e.g. [Link] . It is composed of octets that range from 0
to 255.
• Protocol: A protocol is a set of rules basically that is followed for
communication. For example: TCP, FTP, Telnet, SMTP, POP etc.
• Port Number: The port number is used to uniquely identify different
applications.
• MAC Address: MAC (Media Access Control) Address is a unique
identifier of NIC (Network Interface Controller). A network node can
have multiple NIC but each with unique MAC.
Java Networking Terminology Cnt.

• Connection-oriented and connection-less protocol: In connection-


oriented protocol, acknowledgement is sent by the receiver. So it is
reliable but slow. The example of connection-oriented protocol is TCP.
But, in connection-less protocol, acknowledgement is not sent by the
receiver. So it is not reliable but fast. The example of connection-less
protocol is UDP.
• Socket: A socket is an endpoint between two way communication.
Java Socket Programming
Java Socket programming is used for communication between the
applications running on different JRE.
• Java Socket programming can be connection-oriented or connection-
less.
• Socket and ServerSocket classes are used for connection-oriented
socket programming and DatagramSocket and DatagramPacket
classes are used for connection-less socket programming.
• The client in socket programming must know two information:
IP Address of Server, and
Port number.
Socket class
• A socket is simply an endpoint for communications between the
machines. The Socket class can be used to create a socket.
Important methods of socket class include:

public InputStream getInputStream() returns the InputStream attached with this socket.

public OutputStream getOutputStream() returns the OutputStream attached with this socket.

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.

public Socket accept() returns the socket and establish a connection between
server and client.
public synchronized void close() closes the server socket.
Example of Java Socket Programming
• Here is a simple of java socket programming in which client sends a text
and server receives it. You need to classes one for the server and the other
one for the client, the server part is as follows:
Example of Java Socket Programming Cnt.
• the server part is as follows:
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. For
example: [Link]
A URL contains many information:
 Protocol: In this case, http is the protocol.
 Server name or IP Address: In this case, [Link] is the server
name.
 Port Number: It is an optional attribute. If we write
http//[Link]/sonoojaiswal/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.
 File Name or directory name: Specific page or file need to be accessed
for example: [Link].
Commonly used methods of Java URL class

• The [Link] class provides many methods. The important


methods of URL class are given below.
public String getProtocol() returns the protocol of the URL.
public String getHost() returns the host name of the URL.
public String getPort() returns the Port Number of the URL.
public String getFile() returns the file name of the URL.
public URLConnection openConnection() returns the instance of URLConnection i.e. associated with
this URL.
Example of Java URL class
Java URLConnection class
• 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.
• To get the object of URLConnection class use openConnection()
method of URL class. The syntax is as follows:

public URLConnection openConnection() throws IOException{}


Displaying source code of a webpage by
URLConnecton class
• 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.
Example of Java URLConnection class
The following code shows the use of getInputStream() method
Java HttpURLConnection class
• The Java HttpURLConnection class is http specific URLConnection. It
works for HTTP protocol only.
• By the help of HttpURLConnection class, you can get information of any
HTTP URL such as header information, status code, response code etc.
• The [Link] is subclass of URLConnection class.
Obtaining the object of HttpURLConnection
class
To get the object of HttpURLConnection class use
The openConnection() method of URL class The Syntax:

You can typecast it to HttpURLConnection type as follows.


Java HttpURLConnecton Example:
The end

You might also like