0% found this document useful (0 votes)
32 views

Chapter 1 Java Networking

This document discusses Java networking concepts including: 1) Java networking allows sharing of resources between computing devices using sockets and protocols like TCP and UDP. 2) Key Java networking terms are defined including IP address, protocol, port number, and MAC address. Connection-oriented and connection-less protocols are also explained. 3) The java.net package supports networking in Java through classes like DatagramSocket, DatagramPacket, Socket, and InetAddress. It allows both low-level and high-level network programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Chapter 1 Java Networking

This document discusses Java networking concepts including: 1) Java networking allows sharing of resources between computing devices using sockets and protocols like TCP and UDP. 2) Key Java networking terms are defined including IP address, protocol, port number, and MAC address. Connection-oriented and connection-less protocols are also explained. 3) The java.net package supports networking in Java through classes like DatagramSocket, DatagramPacket, Socket, and InetAddress. It allows both low-level and high-level network programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ad.

Java Unit 1 DHB Sony College, Solapur


Prof:-Inamdar S.J

Networking

*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.

Advantage of Java Networking

1. Sharing resources
2. Centralize software management

The java.net package supports two protocols,

1. TCP: Transmission Control Protocol provides reliable communication between the


sender and receiver. TCP is used along with the Internet Protocol referred as
TCP/IP.
2. UDP: User Datagram Protocol provides a connection-less protocol service by
allowing packet of data to be transferred along two or more nodes

Java Networking Terminology

1) IP Address

IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 . It is


composed of octets that range from 0 to 255.

It is a logical address that can be changed.

2) Protocol

A protocol is a set of rules basically that is followed for communication. For example:

o TCP
o FTP
o Telnet
o SMTP
o POP etc.

Ad.Java Page 1
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

3) Port Number

The port number is used to uniquely identify different applications. It acts as a


communication endpoint between applications.

The port number is associated with the IP address for communication between two
applications.

4) 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 address.

For example, an ethernet card may have a MAC address of 00:0d:83::b1:c0:8e.

5) 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.

6) Socket

A socket is an endpoint between two way communications.

Visit next page for Java socket programming.

*java.net package

The java.net package can be divided into two sections:

1. A Low-Level API: It deals with the abstractions of addresses i.e. networking


identifiers, Sockets i.e. bidirectional data communication mechanism and Interfaces
i.e. network interfaces.
2. A High Level API: It deals with the abstraction of URIs i.e. Universal Resource
Identifier, URLs i.e. Universal Resource Locator, and Connections i.e. connections to
the resource pointed by URLs.

The java.net package provides many classes to deal with networking applications in Java. A
list of these classes is given below:

o Authenticator

Ad.Java Page 2
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

o CacheRequest
o CacheResponse
o ContentHandler
o CookieHandler
o CookieManager
o DatagramPacket
o DatagramSocket
o DatagramSocketImpl
o InterfaceAddress
o JarURLConnection
o MulticastSocket
o InetSocketAddress
o InetAddress
o Inet4Address
o Inet6Address
o IDN
o HttpURLConnection
o HttpCookie
o NetPermission
o NetworkInterface
o PasswordAuthentication
o Proxy
o ProxySelector
o ResponseCache
o SecureCacheResponse
o ServerSocket
o Socket
o SocketAddress
o SocketImpl
o SocketPermission
o StandardSocketOptions
o URI
o URL

Ad.Java Page 3
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

o URLClassLoader
o URLConnection
o URLDecoder
o URLEncoder
o URLStreamHandler

List of interfaces available in java.net package:

o ContentHandlerFactory
o CookiePolicy
o CookieStore
o DatagramSocketImplFactory
o FileNameMap
o SocketOption<T>
o SocketOptions
o SocketImplFactory
o URLStreamHandlerFactory
o ProtocolFamily

*Java DatagramSocket and DatagramPacket

Java DatagramSocket and DatagramPacket classes are used for connection-less socket
programming using the UDP instead of TCP.

*Datagram

Datagrams are collection of information sent from one device to another device via the
established network. When the datagram is sent to the targeted device, there is no
assurance that it will reach to the target device safely and completely. It may get damaged
or lost in between. Likewise, the receiving device also never know if the datagram received
is damaged or not. The UDP protocol is used to implement the datagrams in Java.

*Java DatagramSocket class

Java DatagramSocket class represents a connection-less socket for sending and receiving
datagram packets. It is a mechanism used for transmitting datagram packets over
network.`

A datagram is basically an information but there is no guarantee of its content, arrival or


arrival time.

Ad.Java Page 4
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

Commonly used Constructors of DatagramSocket class


o DatagramSocket() throws SocketEeption: it creates a datagram socket and binds
it with the available Port Number on the localhost machine.
o DatagramSocket(int port) throws SocketEeption: it creates a datagram socket
and binds it with the given Port Number.
o DatagramSocket(int port, InetAddress address) throws SocketEeption: it
creates a datagram socket and binds it with the specified port number and host
address.

Java DatagramSocket Class


Method Description

void bind(SocketAddress addr) It binds the DatagramSocket to a specific address and port.

void close() It closes the datagram socket.

void connect(InetAddress It connects the socket to a remote address for the socket.
address, int port)

void disconnect() It disconnects the socket.

boolean getBroadcast() It tests if SO_BROADCAST is enabled.

DatagramChannel It returns the unique DatagramChannel object associated


getChannel() with the datagram socket.

InetAddress getInetAddress() It returns the address to where the socket is connected.

InetAddress getLocalAddress() It gets the local address to which the socket is connected.

int getLocalPort() It returns the port number on the local host to which the
socket is bound.

SocketAddress It returns the address of the endpoint the socket is bound to.

Ad.Java Page 5
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

getLocalSocketAddress()

int getPort() It returns the port number to which the socket is connected.

int getReceiverBufferSize() It gets the value of the SO_RCVBUF option for this
DatagramSocket that is the buffer size used by the platform
for input on the DatagramSocket.

boolean isClosed() It returns the status of socket i.e. closed or not.

boolean isConnected() It returns the connection state of the socket.

void send(DatagramPacket p) It sends the datagram packet from the socket.

void receive(DatagramPacket It receives the datagram packet from the socket.


p)

Java DatagramPacket Class

Java DatagramPacket is a message that can be sent or received. It is a data container. If


you send multiple packet, it may arrive in any order. Additionally, packet delivery is not
guaranteed.

Commonly used Constructors of DatagramPacket class


o DatagramPacket(byte[] barr, int length): it creates a datagram packet. This
constructor is used to receive the packets.
o DatagramPacket(byte[] barr, int length, InetAddress address, int port): it
creates a datagram packet. This constructor is used to send the packets.

Java DatagramPacket Class Methods


Method Description

1) InetAddress getAddress() It returns the IP address of the machine to which the


datagram is being sent or from which the datagram
was received.

Ad.Java Page 6
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

2) byte[] getData() It returns the data buffer.

3) int getLength() It returns the length of the data to be sent or the


length of the data received.

4) int getOffset() It returns the offset of the data to be sent or the


offset of the data received.

5) int getPort() It returns the port number on the remote host to


which the datagram is being sent or from which the
datagram was received.

6) SocketAddress getSocketAddress() It gets the SocketAddress (IP address + port number)


of the remote host that the packet is being sent to or
is coming from.

7) void setAddress(InetAddress iaddr) It sets the IP address of the machine to which the
datagram is being sent.

8) void setData(byte[] buff) It sets the data buffer for the packet.

9) void setLength(int length) It sets the length of the packet.

10) void setPort(int iport) It sets the port number on the remote host to which
the datagram is being sent.

11) void It sets the SocketAddress (IP address + port number)


setSocketAddress(SocketAddress of the remote host to which the datagram is being
addr) sent.

Example of Sending DatagramPacket by DatagramSocket

//DSender.java

1. import java.net.*;
2. public class DSender{

Ad.Java Page 7
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

3. public static void main(String[] args) throws Exception {


4. DatagramSocket ds = new DatagramSocket();
5. String str = "Welcome java";
6. InetAddress ip = InetAddress.getByName("127.0.0.1");
7.
8. DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
9. ds.send(dp);
10. ds.close();
11. }
12. }

Output:

Example of Receiving DatagramPacket by DatagramSocket

1. //DReceiver.java
2. import java.net.*;
3. public class DReceiver{
4. public static void main(String[] args) throws Exception {
5. DatagramSocket ds = new DatagramSocket(3000);
6. byte[] buf = new byte[1024];
7. DatagramPacket dp = new DatagramPacket(buf, 1024);
8. ds.receive(dp);
9. String str = new String(dp.getData(), 0, dp.getLength());
10. System.out.println(str);

Ad.Java Page 8
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J

11. ds.close();
12. }
13. }

Output:

Ad.Java Page 9

You might also like