Chapter 1 Java Networking
Chapter 1 Java Networking
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.
1. Sharing resources
2. Centralize software management
1) IP Address
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 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.
6) Socket
*java.net package
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
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 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 represents a connection-less socket for sending and receiving
datagram packets. It is a mechanism used for transmitting datagram packets over
network.`
Ad.Java Page 4
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J
void bind(SocketAddress addr) It binds the DatagramSocket to a specific address and port.
void connect(InetAddress It connects the socket to a remote address for the socket.
address, int port)
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.
Ad.Java Page 6
Ad.Java Unit 1 DHB Sony College, Solapur
Prof:-Inamdar S.J
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.
10) void setPort(int iport) It sets the port number on the remote host to which
the datagram is being sent.
//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
Output:
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