0% found this document useful (0 votes)
61 views21 pages

Java Networking: Anas Dange

This document provides an overview of Java networking concepts. It defines network programming as writing programs that execute across multiple computers connected by a network. It discusses the java.net package for TCP and UDP support. Socket is defined as an endpoint for two-way communication and data sharing between devices. The key classes for socket programming are Socket, ServerSocket, DatagramSocket, and DatagramPacket. The document describes the constructors and methods of these classes and how they work together for client-server communication. It also covers URL, URLConnection, and HttpURLConnection classes for network resource access. The InetAddress class represents IP addresses. The document concludes with some reserved port numbers and networking terms.

Uploaded by

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

Java Networking: Anas Dange

This document provides an overview of Java networking concepts. It defines network programming as writing programs that execute across multiple computers connected by a network. It discusses the java.net package for TCP and UDP support. Socket is defined as an endpoint for two-way communication and data sharing between devices. The key classes for socket programming are Socket, ServerSocket, DatagramSocket, and DatagramPacket. The document describes the constructors and methods of these classes and how they work together for client-server communication. It also covers URL, URLConnection, and HttpURLConnection classes for network resource access. The InetAddress class represents IP addresses. The document concludes with some reserved port numbers and networking terms.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Java Networking

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.

1. URL url=new URL("http://www.mysite.com/


mytutorial");
2. HttpURLConnection
huc=(HttpURLConnection)url.openConnectio
n();
InetAddress Class
 Java InetAddress class represents an IP address.
 Methods:-
1. public static InetAddress getByName(String host) it
returns the instance of InetAddress containing
LocalHost IP and name.
2. public static InetAddress getLocalHost() it returns
the instance of InetAdddress containing local host
name and address.
3. public String getHostName() it returns the host
name of the IP address.
4. public String getHostAddress() it returns the IP
address in string format.
5. public String getByName(String) Determines the IP
address of a host, given the host's name.
DatagramSocket Class
 Java DatagramSocket class represents a
connection-less socket for sending and
receiving datagram packets.
 Constructors:-
1. DatagramSocket(): it creates a datagram
socket and binds it with the available Port
Number on the localhost machine.
2. DatagramSocket(int port): it creates a
datagram socket and binds it with the given
Port Number.
3. DatagramSocket(int port, InetAddress
address): it creates a datagram socket and
binds it with the specified port number and
host address.
DatagramSocket Methods
1. close(): Closes this datagram socket.
2. getLocalAddress(): Gets the local address to
which the socket is bound.
3. getLocalPort(): Returns the port number on
the local host to which this socket is bound.
4. receive(DatagramPacket): Receives a
datagram packet from this socket.
5. send(DatagramPacket): Sends a datagram
packet from this socket.
6. setSoTimeout(int): Enable/disable
SO_TIMEOUT with the specified timeout, in
milliseconds.
DatagramPacket Class
 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.
 Constructors:-
1. DatagramPacket(byte[] b, int length): it
creates a datagram packet. This constructor
is used to receive the packets.
2. DatagramPacket(byte[] b, int length,
InetAddress address, int port): it creates a
datagram packet. This constructor is used to
send the packets.
DatagramPacket Methods
1. getAddress() Returns the IP address of the machine to
which this datagram is being sent or from which the
datagram was received.
2. getData() Returns the data received or the data to be
sent.
3. getLength() Returns the length of the data to be sent
or the length of the data received.
4. getPort() Returns the port number on the remote host
to which this datagram is being sent or from which the
datagram was received.
5. setAddress(InetAddress)
6. setData(byte[])
7. setLength(int)
8. setPort(int)
Reserved Sockets
 Sockets reserved by specific protocols for
communication
 Eg.:
 FTP - 21
 E-mail - 25
 HTTP - 80
Terms
1.Proxy Server
2.URL format
3.Domain Name Service (DNS)
4.Internet Addressing
1. Class A
2. Class B
3. Class C
4. Class D
5. Class E
“Nothing pushes students to do their
best work like a professor who takes
pride not in his or her own
accomplishments, but in helping others
realize their potential”

You might also like