04/01/2020

Socket in Java Programming | Types of Socket :Studywow

 Studywow: Topics= Socket in Java Programming? Types of Socket ? Define also the methods & class of these different sockets.

Q1. What is socket?

Ans. Socket in Java Programming: A socket is an end-point between two way communication running on the same network. Socket classes establish connection between a client program and a server program. It provides communication mechanism between two computers using TCP protocol. The java.net package provides two classes;
java.net.Socket and java.net.ServerSocket that implement the client side and the server side of the connection, respectively.

Q2. How many different types of socket you can create in Java? Define also the methods & class of these different sockets ?

Ans. Socket in Java Programming: Socket in Java Programming are used for communication between the applications running on different environments. Java Sockets can be connection-oriented or connection-less. Different types of socket that can be created in Java are:
(a) Simple Socket and ServerSockets are used for connection-oriented socket programming.
(b) Datagram Socket and DatagramPacket are used for connection-less socket programming.
The client in socket programming must know two information:
(a) IP Address of Server, and      (b) Port number Simple Sockets

1. Simple Socket

A socket is simply an endpoint for communications between the machines. The socket class can be used to create a socket.
Some methods in the socket class are:
(a) public void connect(SocketAddress host, int timeout) throws IOException: This method connects the socket to the specified host. This method is needed only when you instantiate the socket using the no-argument constructor.

(b) public InetAddress getInetAddress(): This method returns the address of the other computer that this socket is connected to.

(c) public int getPort(): Returns the port the socket is bound to on the remote machine.

(d) public int getLocalPort(): Returns the port the socket is bound to on the local machine.

(e) public SocketAddress getRemote SocketAddress(): Returns the address of the remote socket.

(f) public InputStream getInputStream() throws IOException: Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.

(g) public OutputStream getOutputStream() throws IOException: Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket.

(h) public void close() throws IOException: Closes the socket, which makes this socket object no longer capable of connecting again to any server.

2. Server Sockets

The ServerSocket class can be used to create a server socket. This object is used to establish communication with the clients.
Some of the common methods of the ServerSocket class:
(a) public int getLocalPort(): Returns the port that the server socket is listening on. This method is useful if you passed in 0 as the port number in a constructor and let the server find a port for you.

(b) public Socket accept() throws IOException: Waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out, assuming that the time-out value has been set using the setSoTimeout() method. Otherwise, this method blocks indefinitely.

(c) public void setSoTimeout(int timeout): Sets the time-out value for how long the server socket waits for a client during the accept().

(d) public void bind (SocketAddress host, int backlog): Binds the socket to the specified server and port in the SocketAddress object. Use this method if you have instantiated the ServerSocket using the no-argument constructor.
When the ServerSocket invokes accept(), the method does not return until a client connects. After a client does connect, the ServerSocket creates a new Socket on an unspecified port and returns a reference to this new Socket. A TCP connection now exists between the client and the server, and communication can begin.

3. Datagram Socket

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.
Some methods used are:
(a) close(): Closes this datagram socket.
(b) getLocalAddress(): Gets the local address to which the socket is bound.
(c) getLocalPort(): Returns the port number on the local host to which this socket is bound.
(d) getSoTimeout(): Retrive setting for SO_TIMEOUT.
(e) receive (DatagramPacket): Receives a datagram packet from this socket.
(f) send (DatagramPacket): Sends a datagram packet from this socket.
(g) setSoTimeout(int): Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

4. DatagramPacket

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. Some of the methods used are:

(a) Send: public void send (DatagramPacket p) throws IOException
Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.
Parameters: p-the DatagramPacket to be sent.
Throws: IOException-if an I/O error occurs.

(b) Receive: public synchronised void receive (DatagramPacket p) throws IOException.
Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.
Parameters: p-the DatagramPacket into which to place the incoming data.
Throws: IOException-if an I/O error occurs.

(c) getLocalAddress: public InetAddress getLocalAddress()
Gets the local address to which the socket is bound.

(d) getLocalPort: public int getLocalPort().
Returns the port number on the local host to which this socket is bound.
Returns: The port number on the local host to which this socket is bound.

(e) SetSoTimeout: public synchronised void setSoTimeout(inttimeout) throws Socket Exception

Enable/disable SO TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive () for this DatagramSocket will block for only this amount of time. If the timeout expires, a Java.io.InterruptedIOException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

(f) GetSoTimeout: public synchronised int getSoTimeout() throws SocketException.
Retrive setting for SO_TIMEOUT. O returns implies that the option is disabled (i.e., timeout of infinity).

(g) Close: public void close()
Closes this datagram socket.


[Topic: Socket in Java Programming and types of Socket ]

Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.comSend us your query anytime about socket in java programming and types of Socket!

socket in java programming and types of socket.
Socket in Java Programming and types of Socket.