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 :
- sharing resources
- centralized software management
Some of the widely used java networking terminologies are as follows:
-
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.
-
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. It acts as a communication endpoint between applications. The port number is associated with the IP address for communication between two 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. The main difference between MAC and IP address is that, MAC Address is used to ensure the physical address of computer. It uniquely identifies the devices on a network. While IP address are used to uniquely identifies the connection of network with that device take part in a network.
-
Socket
- A socket is an endpoint between two way communication. More details in "Socket" section below.
-
Connection-oriented 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.
-
Connection-less protocol
- 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.
The java.net package provides many classes to deal with networking applications in Java. A list of these classes is given below:
- Authenticator
- CacheRequest
- CacheResponse
- ContentHandler
- CookieHandler
- CookieManager
- DatagramPacket
- DatagramSocket
- DatagramSocketImpl
- InterfaceAddress
- JarURLConnection
- MulticastSocket
- InetSocketAddress
- InetAddress
- Inet4Address
- Inet6Address
- IDN
- HttpURLConnection
- HttpCookie
- NetPermission
- NetworkInterface
- PasswordAuthentication
- Proxy
- ProxySelector
- ResponseCache
- SecureCacheResponse
- ServerSocket
- Socket
- SocketAddress
- SocketImpl
- SocketPermission
- StandardSocketOptions
- URI
- URL
- URLClassLoader
- URLConnection
- URLDecoder
- URLEncoder
- URLStreamHandler
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
- Port number.
A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket.
Important methods :
Method | Description |
---|---|
1) public InputStream getInputStream() | returns the InputStream attached with this socket. |
2) public OutputStream getOutputStream() | returns the OutputStream attached with this socket. |
3) public synchronized void close() | closes this socket |
The ServerSocket class can be used to create a server socket. This object is used to establish communication with the clients.
Important methods :
Method | Description |
---|---|
1) public Socket accept() | returns the socket and establish a connection between server and client. |
2) public synchronized void close() | closes the server socket. |
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept(); //establishes connection and waits for the client
Socket s = new Socket("localhost",6666);