47. What is a socket ? How do you facilitate inter process communication in Java ?


A socket is a communication channel, which facilitates inter-process communication (For example communicating between two JVMs, which may or may not be running on two different physical machines). A socket is an endpoint for communication. There are two kinds of sockets, depending on whether one wishes to use a connectionless or a connection-oriented protocol. The connectionless communication protocol of the Internet is called UDP. The connection-oriented communication protocol of the Internet is called TCP. UDP sockets are also called datagram sockets. Each socket is uniquely identified on the entire Internet with two numbers. The first number is a 128-bit integer called the Internet Address (or IP address). The second number is

a 16-bit integer called the port of the socket. The IP address is the location of the machine, which you are trying to connect to and the port number is the port on which the server you are trying to connect is running. The port numbers 0 to 1023 are reserved for standard services such as e-mail, FTP, HTTP etc.

The lifetime of the socket is made of 3 phases: Open Socket ->Read and Write to Socket -> Close Socket

To make a socket connection you need to know two things: An IP address and port on which to listen/connect. In Java you can use the Socket (client side) and ServerSocket (Server side) classes.


No comments:

Post a Comment