What is meant by socket? Discuss the major classes used in network programming briefly.
SOCKETS AND SOCKET-BASED COMMUNICATION
- The socket is an interface for programming networks at the transport layer. Network communication using Sockets is very much similar to performing file I/O. In fact, the socket handle is treated like filehandle. Socket-based communication is independent of the language used for implementing it.
- That means a socket program written in Java language can communicate to a program written in non-Java (say C or C++) socket program. A server program runs on a specific computer and has a socket that is bound to a specific port, The server listens to the socket for a client to make a connection request.
- If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket and a different port number so that it can continue to listen to the original socket for connection requests while serving the connected client.
Socket
A socket is one endpoint of a two-way communication connection between the two applications running on the network. The socket mechanism presents a method of inter-process communication (IPC) by setting named contact points between which the communication occurs. A socket is tied to a port number so that the TCP layer can recognize the application to which the data is intended to be sent.
Java Socket Programming
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.
NETWORKING CLASSES IN THE JDK
Java programs can use TCP or UDP to communicate over the Internet. Some of the classes provided in java.net package are:
Socket Class: The Java Socket class is used to create sockets when we use TCP for communication.
Server Socket Class: The ServerSocket class is used to create sockets for servers when TCP is used for communication.
InetAddress Class: The InetAddress is Java's representation of an IP address. Instances of this class are used together with UDP DatagramSockets and TCP Sockets and ServerSockets.
DatagramSocket Class: This class is used to create sockets at client or server when we use UDP protocol for communication
DatagramPacket Class: This class is used to create datagram packets that are exchanged between UDP clients and UDP servers.
URL Class: 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.
URLConnection Class: The Java URLConnection 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.
Comments
Post a Comment