05/01/2020

Datagram Socket | TCP IP Socket | in Java | Studywow

Studywow: Topic=1. Datagram Socket. 2. TCP/IP Server and Client Socket. Read more topics of  Java (click here).


Q1. What do you mean by datagram socket?

Ans. A datagram socket is a type of connection-less socket, which is used for sending or receiving packet delivery services. Each packet sent or received on a datagram socket is individually addressed and routed.

Q2. Write down the various methods used in datagram socket programming ?

Ans. Methods used in datagram socket programming are as follows:
(a) getAddress(): Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
(b) getData(): Returns the data received or the data to be sent.
(c) getLength(): Returns the length of the data to be sent or the length of the data received.
(d) getPort(): Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.
(e) setAddress (InetAddress addr): It sets the IP address of the machine to which this datagram is being sent.
(f) setData (byte[] buf): It set the data buffer for this packet.
(g) setPort(intiport): It helps to set the port number on the remote host to which this datagram is being sent.
(h) setSocketAddress (SocketAddress.address): It sets the SocketAddress (ie. IPaddress and port number) of the remote host to which this datagram is being sent.

Q3. What is TCP/IP server socket?

Ans. TCP/IP ServerSocket : Java has a different socket class that must be used for creating server applications. The ServerSocket class is used to create servers that listen for either local or remote client programs to connect to them on published ports.

ServerSockets are quite different from normal sockets. When you create a ServerSocket, it will register itself with the system as having an interest in client connections. The constructors for ServerSocket reflect the port number that you want to accept connections on and, if optionally, how long you want the queue for said port to be.

The queue length tells the system how many client connections it can leave pending before it should simply refuse connections. The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to establish connections.

The following steps occur when establishing a TCP connection between two computers using sockets:
Step 1: The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
Step 2: The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.
Step 3: After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.
Step 4: The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket obiect capable of communicating with the server.
Step 5: 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.

Q4. Write a sample program of TCP/IP server socket and client socket in java programming ?

Ans. TCPClient.java
//Client Side
import java.io.*;
import java.net.*;
public class ClientSocket {
public void run() {
try {
int server Port = 4020;
InetAddress host = InetAddress.getByName("localhost");
System.out.println("Connecting to server on port" + server Port);
Socket socket = new Socket (host, serverPort);
// Socket socket = new Socket("127.0.0.1", serverPort);
System.out.println("Just connected to "+ socket.getRemoteSocketAddress());
Printwriter toServer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader fromServer = new BufferedReader(new InputStreamReader socket.getInputStream());
toServer.println("Hello from"+ socket.getLocalSocketAddress());
String line = fromServer.readLine();
System.out.println("Client received :"+ line +" from Server");
toServer.close();
fromServer.close();
socket.close();
}
catch (UnknownHostException ex) {
ex.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} }
public static void main(String[] args) {
ClientSocket client = new ClientSocket();
client.run();
} }

TCPServer.Java

import java.net.*;
import java.io*;
public class ServerSideSocket {
public void run () {
try {
int serverPort = 4020;
ServerSocket serverSocket= new ServerSocket (serverPort);
serverSocket.setSoTimeout (10000);
while(true) {
System.out.println("Waiting for client on port" + serverSocket.getLocalPort () + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "+ server.getRemoteSocketAddress());
PrintWriter toClient = new PrintWriter (server.getOutputStream(), true);
BufferedReader fromClient = new BufferedReader (new InputStreamReader (server.getInputStream());
String line = fromClient.readline();
System.out.println("Server received:" + line);
toClient.println("Thank you for connecting to" + server.getLocalSocketAddress () + "\nGoodbye!");
} }
catch (UnknownHostException. ex) {
ex.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} }
public static void main(String[]args) {
ServerSideSocket srv = new ServerSideSocket();
srv.run();
} }



[Topic= Datagram Socket and TCP/IP Socket in Java.]
[Topic= Datagram Socket and TCP/IP Socket in Java.]


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.com
Send us your query anytime!


External Website links:
Topic= Datagram Socket and TCP/IP Socket in Java (click here).