0% found this document useful (0 votes)
14 views34 pages

Chapter_4_NetworkBasic

The document outlines various networking concepts and Java programming techniques related to sockets and URLs, presented by Mr. Gajanan Jadhav from Government Polytechnic, Chhatrapati SambhajiNagar. It covers the use of InetAddress and URLConnection classes for network communication, including TCP/IP and datagram protocols. Additionally, it details the lifecycle and methods of Socket and ServerSocket classes for client-server interactions.

Uploaded by

Pranav
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)
14 views34 pages

Chapter_4_NetworkBasic

The document outlines various networking concepts and Java programming techniques related to sockets and URLs, presented by Mr. Gajanan Jadhav from Government Polytechnic, Chhatrapati SambhajiNagar. It covers the use of InetAddress and URLConnection classes for network communication, including TCP/IP and datagram protocols. Additionally, it details the lifecycle and methods of Socket and ServerSocket classes for client-server interactions.

Uploaded by

Pranav
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
You are on page 1/ 34

Government Polytechnic

Chhatrapati SambhajiNagar
Department Of Computer Engineering

-With Mr. G.U.Jadhav


Lecturer in Computer Engineering
import java.net*

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. Use InetAddress class to know the IP address of the given Host name.

2. Use URLConnection class to read and write data to the specified resource
referred by the given URL.

3. Develop program for client/server communication through TCP/IP server


sockets for the given problem.

4. Write program to illustrate the client/server communication using


datagram protocol for the given problem.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Definition :
- A socket is one endpoint of a two-way communication link Between two
programs running on the network.

- A socket is bound to a port Number so that the TCP layer can identify the
application that data is destined to be sent to.

Socket = IP Address+ Port Number

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Client : Wants to gain access to particular resources of server.

Server : has some resource to share.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. In networking a port is a communication endpoint.
2. At the software level, within an operating system, a port is a logical construct that
identifies a specific process or a type of network service.
3. Port is special type of numbered socket to which client connects.
4. A port number is a 16-bit unsigned integer, thus ranging from 0 to 65535.
5. There are around 1024 reserved ports.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Protocol Port
FTP 20 , 21
telnet 23
Smtp 25
http 80
POP3 110
Echo 7
Discard 9
Daytime 13
Ssh 22
Whois 43
Time 37

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. Encapsualte numerical IP address and domain name of that address.

1. The InetAddress class has no visible constructors.

2. To create an InetAddress object, we have to use one of the available factory methods.

3. Factory methods are merely a convention whereby static methods in a class return an
instance of that class.

4. Three commonly used InetAddress factory methods are shown here.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. static InetAddress getLocalHost( ) throws UnknownHostException

2. static InetAddress getByName(String hostName) throws


UnknownHostException

3. static InetAddress[ ] getAllByName(String hostName) throws


UnknownHostException

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. boolean equals(Object other)

2. byte[ ] getAddress( )

3. String getHostAddress( )

4. String getHostName( )

5. boolean isMulticastAddress( )

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. Url refers a “resource” on the WWW.
2. Resource can be file , folder or query to search engine.

Syntax:

Protocol :// host name or Ipaddress : port_number /path


Ex:
1. http://www.rediffmail.com/
2. http://www.rediffmail.com:80/index.html/

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. URL(String urlSpecifier)

2. URL(String protName, String hostName, int port, String path)

3. URL(String protName, String hostName, String path)

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. public String getProtocol()

2. public String getHost()

3. public String getPort()

4. public String getFile()

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
import java.net.*;

public class URLExample


{
public static void main(String[] args)
{
URL url = new URL("https://www.example.com");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort()); // Returns -1 if
no port is specified
System.out.println("Default Port: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery()); // Returns
null if no query is present

}
}
Protocol: https
Host: www.example.com
Port: -1
Default Port: 443
File:
Path:
Query: null
1. This class represents a communication link between the URL and the application.
2. URLConnection is an abstract class that represents an active connection to a resource
specified by a URL
2. can be used to read and write data to the specified resource referred by the URL.

How to Create Object of URLConnection

1. The openConnection() method of URL class returns the object of URLConnection class
2. Syntax:

public URLConnection openConnection ()throws IOException

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. public String getContentType( )
2. public int getContentLength( )
3. public long getDate( )
4. public long getExpiration( )
5. public long getLastModified( )
HTTP/1.1 200 OK
Date: Mon, 18 Oct 1999 20:06:48 GMT
Server: Apache/1.3.4 (Unix) PHP/3.0.6 mod_perl/1.17
Last-Modified: Mon, 18 Oct 1999 12:58:21 GMT
ETag: "1e05f2-89bb-380b196d"
Accept-Ranges: bytes
Content-Length: 35259
Connection: close
Content-Type: text/html

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
A program that uses the URLConnection class directly follows this
basic sequence of steps:

1. Construct a URL object.


2. Invoke the URL object's openConnection( ) method to retrieve
a URLConnection object for that URL.
3. Configure the URLConnection.
4. Read the header fields.
5. Get an input stream and read data.
6. Get an output stream and write data.
7. Close the connection.
1. TCP/IP Sockets are used to write network program across multile computer in
network.
2. TCP/IP sockets are used to implement t reliable, bidirectional, persistent,point-
to-point,
and stream-based connections between hosts on the Internet.
3. A socket can be used to connect Java’s I/O system to other programs that may
reside either on the local machine or on any other machine on the Internet.
4. The creation of a Socket object implicitly establishes a connection between the
client and server
5. The most widely used classes for socket programming are :

a. java.net.Socket
b. java.net.ServerSocket

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
This class is used to create socket at client side and creation of socket leads to establishment
of connection with server side socket.

How to Create Socket -- Constructors

1. Socket(String hostName, int port) throws UnknownHostException, IOException


2. Socket(InetAddress ipAddress, int port) throws UnknownHostException, IOException

Methods :
1. InetAddress getInetAddress( )
2. int getPort( )
3. int getLocalPort( )
4. InputStream getInputStream( )
5. OutputStream getOutputStream( )

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1. This class is used to create socket at server side and creation of server socket leads to
showing interest in accepting request from client.
2. Life Cycle:
2.1 A new ServerSocket is created on a particular port using a ServerSocket() constructor.
2.2 The ServerSocket listens for incoming connection attempts on that port using its accept( )
method. accept( ) blocks until a client attempts to make a connection, at which point accept( )
returns a Socket object connecting the client and the server.
2.3 Depending on the type of server, either the Socket's getInputStream() method,
getOutputStream( ) method, or both are called to get input and output streams that
communicate with the client.
2.4 The server and the client interact according to an agreed-upon protocol
until it is time to close the connection.
2.5 The server, the client, or both close the connection.
2.6 The server returns to step 2 and waits for the next connection.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Mr. Gajanan Jadhav
Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
Constructor :

1. ServerSocket(int port) throws BindException, IOException


2. ServerSocket(int port, int maxQueue) throws BindException,IOException
3. ServerSocket(int port, int maxQueue, InetAddress localAddress)throws IOException

Methods

1. public Socket accept( ) throws IOException


2. public void close( ) throws IOException
3. public InetAddress getInetAddress( )
4. public int getLocalPort( )
5. InputStream getInputStream( )
6. OutputStream getOutputStream( )

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar
1.

Mr. Gajanan Jadhav


Lecturer In Computer Engineering
Government Polytechnic,
Chhatrapati SambhajiNagar

You might also like