Java networking allows connecting computing devices to share resources using sockets. Sockets provide the communication mechanism between devices using TCP. The client creates a socket to connect to a server socket. Once connected, input/output streams allow bidirectional communication between client and server sockets. Common networking concepts in Java include IP addresses, protocols, port numbers, and socket classes.
Java networking allows connecting computing devices to share resources using sockets. Sockets provide the communication mechanism between devices using TCP. The client creates a socket to connect to a server socket. Once connected, input/output streams allow bidirectional communication between client and server sockets. Common networking concepts in Java include IP addresses, protocols, port numbers, and socket classes.
Java networking allows connecting computing devices to share resources using sockets. Sockets provide the communication mechanism between devices using TCP. The client creates a socket to connect to a server socket. Once connected, input/output streams allow bidirectional communication between client and server sockets. Common networking concepts in Java include IP addresses, protocols, port numbers, and socket classes.
Java networking allows connecting computing devices to share resources using sockets. Sockets provide the communication mechanism between devices using TCP. The client creates a socket to connect to a server socket. Once connected, input/output streams allow bidirectional communication between client and server sockets. Common networking concepts in Java include IP addresses, protocols, port numbers, and socket classes.
Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 21
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.
The widely used java networking terminologies are given
below: IP Address Protocol Port Number MAC Address Connection-oriented and connection-less protocol Socket Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server. When the connection is made, the server creates a socket object on its end of the communication. The client and the server can now communicate by writing to and reading from the socket. 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 following steps occur when establishing a TCP connection between two computers using sockets − The server instantiates a ServerSocket object, denoting which port number communication is to occur on. The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. After the server is waiting, a client instantiates a Socket object, specifying the server name and the port number to connect to. The constructor of the Socket class attempts to connect the client to the specified server and the port number. If communication is established, the client now has a Socket object capable of communicating with the server. 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. After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream. TCP is a two-way communication protocol, hence data can be sent across both streams at the same time. Following are the useful classes providing complete set of methods to implement sockets. The client in socket programming must know two information: IP Address of Server, and Port number. A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket. The ServerSocket class can be used to create a server socket. This object is used to establish communication with the clients Let's see a simple of java socket programming in which client sends a text and server receives it. The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a resource on the World Wide Web. For example: http://www. helloworld.com/data A URL contains many information: Protocol: In this case, http is the protocol. Server name or IP Address: In this case, www.helloworld.com is the server name. Port Number: It is an optional attribute. If we write http//ww.helloworld.com:80/rajeeev/ , 80 is the port number. If port number is not mentioned in the URL, it returns -1. File Name or directory name: In this case, index.jsp is the file name. //URLDemo.java import java.io.*; import java.net.*; public class URLDemo{ public static void main(String[] args){ try{ URL url=new URL("http://www.helloworld.com/real_world"); System.out.println("Protocol: "+url.getProtocol()); System.out.println("Host Name: "+url.getHost()); System.out.println("Port Number: "+url.getPort()); System.out.println("File Name: "+url.getFile()); }catch(Exception e){System.out.println(e);} } } Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the IP of any host name for example www.google.com, www.facebook.com import java.io.*; import java.net.*; public class InetDemo{ public static void main(String[] args){ try{ InetAddress ip=InetAddress.getByName("www.facebook. com");
System.out.println("IP Address: "+ip.getHostAddress()); }catch(Exception e){System.out.println(e);} } } Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming. Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets. A datagram is basically an information but there is no guarantee of its content, arrival or arrival time. DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine. DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number. DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address. Java 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. Commonly used Constructors of DatagramPacket class DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used to receive the packets. DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram packet. This constructor is used to send the packets.