AJP Unit 2 QB Bank
AJP Unit 2 QB Bank
8. Which exception is thrown when a connection cannot be established with a remote server?
a) IOException
b) ConnectionException
c) NetworkException
d) UnknownHostException
10. Which Java class represents a socket address, consisting of an IP and port number?
a) InetAddress
b) InetPort
c) SocketAddress
d) InetSocketAddress
11. Template which can send and receive JMS messages with much less code
a) JmsTemplate
b) EMail
c) All of the mentioned
d) None of the mentioned
12. The template handles the boilerplate tasks for you and also converts the JMS API
JMSException hierarchy into Spring runtime exception:-
a) org.springframework.jms.Jms
b) org.springframework.jms.JmsException
c) org.springframework.jms.JmsTemplate
d) none of the mentioned
13. To address different JMS APIs, Spring provides :-
a) JmsTemplate
b) JmsTemplate102
c) All of the mentioned
d) None of the mentioned
14. Before you can send and receive JMS messages, you need to install a JMS message broker:-
a) Apache ActiveM
b) Apache Active
c) Apache MQ
d) Apache ActiveMQ
19. Which class creats a TCP server socket, bound to the specified port ?
a) Socket
b)InetAddress
c)ServerSocket
d)DatagramSocket
20. Which method of URL class returns the object of URL Connection class ?
a) getLocalHost()
b) openConnection()
c) getByName(String host)
d) getHostAddress()
Part II
1. Define Sockets programming in java networking with example.
Communication - To communicate over a socket connection, streams are used to both input
and output the data.
Closing the connection - The socket connection is closed explicitly once the message to the
server is sent.
Server Program
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
2. Write notes on Types of Socket in java.
1. Datagram Sockets: Datagram sockets allow processes to use the User Datagram Protocol
(UDP). It is a two-way flow of communication or messages. It can receive messages in a
different order from the sending way and also can receive duplicate messages. These sockets
are preserved with their boundaries. The socket type of datagram socket is SOCK_DGRAM.
2. Stream Sockets: Stream socket allows processes to use the Transfer Control Protocol
(TCP) for communication. A stream socket provides a sequenced, constant or reliable, and
two-way (bidirectional) flow of data. After the establishment of connection, data can be read
and written to these sockets in a byte stream. The socket type of stream socket is
SOCK_STREAM.
3. Raw Sockets: Raw Socket provide user access to the Internet Control Message Protocol
(ICMP). Raw sockets are not used for most applications. These sockets are the same as the
datagram oriented, their characteristics are dependent on the interfaces. They provided
support in developing new communication protocols or for access to more facilities of an
existing protocol. Only the super users can access the Raw Sockets. The socket type of Raw
Socket is SOCK_RAW.
4. Multicast Socket :The multicast datagram socket class is useful for sending and receiving IP
multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for
joining "groups" of other multicast hosts on the internet. A multicast group is specified by a
class D IP address and by a standard UDP port number.
Platform Independence − One of the biggest advantages of Java Sockets is that they are
platform-independent. This means that the same Java code can be run on multiple operating
systems and devices without the need for modification. This allows for easy deployment of
network-based applications across different systems and ensures that the application can be
run on different devices without the need for platform-specific code.
Easy to Use − Java Sockets are also relatively easy to use, even for developers who are new to
network programming. The Java API provides a simple, consistent interface for creating and
managing sockets, which makes it easy to implement network-based applications without
needing to understand the underlying network protocols.
Scalability − Java Sockets are highly scalable, making them suitable for large-scale network-
based applications. They can easily handle thousands of simultaneous connections and can be
used to create distributed systems that can handle high levels of traffic.
Security − Java Sockets provide built-in support for secure communication, including SSL and
TLS encryption. This makes it easy to create secure network-based applications and ensures
that sensitive data is protected while in transit.
Multithreading − Java Sockets support multithreading, which means that multiple threads can
be used to handle multiple connections simultaneously. This improves the performance of
network-based applications and allows them to handle a large number of requests without
becoming overloaded.
Chat Applications − Java Sockets are often used to create chat applications, such as instant
messaging programs and online chat rooms. These types of applications typically use a client-
server architecture, where clients connect to a central server to send and receive messages.
File Transfer Applications − Java Sockets can also be used to create file transfer applications,
such as peer-to-peer file sharing programs. These types of applications use a peer-to-peer
architecture, where each device acts as both a client and a server. This allows for direct
communication between devices, which can improve the speed and reliability of file transfers.
Remote Control Applications − Java Sockets can also be used to create remote control
applications, such as remote desktop software. These types of applications use a client-server
architecture, where a client connects to a remote server to control the desktop of the server.
This allows users to access and control their desktop from any device with an internet
connection.
Multiplayer Games − Java Sockets are also commonly used to create multiplayer games, such
as online role-playing games and first-person shooters. These types of applications typically
use a client-server architecture, where clients connect to a central server to play the game.
The server acts as the intermediary between clients, handling communication and game logic.
IoT Applications − Java Sockets can also be used in IoT (Internet of Things) applications, such
as smart home systems. These types of applications use a client-server architecture, where
IoT devices connect to a central server to send and receive data. This allows for remote
monitoring and control of the devices, as well as data collection and analysis.
1. Datagram Sockets: Datagram sockets allow processes to use the User Datagram Protocol
(UDP). It is a two-way flow of communication or messages. It can receive messages in a
different order from the sending way and also can receive duplicate messages. These sockets
are preserved with their boundaries. The socket type of datagram socket is SOCK_DGRAM.
2. Stream Sockets: Stream socket allows processes to use the Transfer Control Protocol
(TCP) for communication. A stream socket provides a sequenced, constant or reliable, and
two-way (bidirectional) flow of data. After the establishment of connection, data can be read
and written to these sockets in a byte stream. The socket type of stream socket is
SOCK_STREAM.
3. Raw Sockets: Raw Socket provide user access to the Internet Control Message Protocol
(ICMP). Raw sockets are not used for most applications. These sockets are the same as the
datagram oriented, their characteristics are dependent on the interfaces. They provided
support in developing new communication protocols or for access to more facilities of an
existing protocol. Only the superusers can access the Raw Sockets. The socket type of Raw
Socket is SOCK_RAW.
4. Multicast Socket :The multicast datagram socket class is useful for sending and receiving IP
multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for
joining "groups" of other multicast hosts on the internet. A multicast group is specified by a
class D IP address and by a standard UDP port number.
7. Constructors of FileReader class in java
FileReader - Java FileReader class is used to read data from the file. It returns data in byte
format like FileInputStream class. It is character-oriented class which is used for file handling
in java.
Constructor Description
FileReader(String It gets filename in string. It opens the given file in read mode. If file
file) doesn't exist, it throws FileNotFoundException.
FileReader(File file) It gets filename in file instance. It opens the given file in read mode. If file
doesn't exist, it throws FileNotFoundException.
import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
8. Explain in short about FileInputStream in java.
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-
oriented data (streams of raw bytes) such as image data, audio, video etc. You can also read
character-stream data. But, for reading streams of characters, it is recommended to
use FileReader class.
Scanner : The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods
found in the Scanner class documentation. In our example, we will use the nextLine() method,
which is used to read Strings:
import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
class GFG {
public static void main(String[] args)
throws IOException
{
// initialize a string
String str = "ABC";
try {
System.out.println("Successfully written");
Declaration:
public class FileOutputStream extends OutputStream
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}
OUTPUT
Success...
11. List the Common Network Protocols and it classes in java and explain.
12. Write notes Uniform Resource Loader and its classes in java
The java.net.URL class provides many methods. The important methods of URL class are given
below.
Method Description
//URLDemo.java
import java.net.*;
public class URLDemo{
public static void main(String[] args){
try{
URL url=new URL("http://www.javatpoint.com/java-tutorial");
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);}
}
}
Output:
Protocol: http
Host Name: www.javatpoint.com
Port Number: -1
File Name: /java-tutorial
Part III
1. Discuss the key components of Java Messaging Services (JMS) and how they facilitate reliable and
asynchronous communication in distributed systems.
Java Message Service (JMS) is a Java-based messaging API that allows applications to communicate in a
loosely coupled, reliable, and asynchronous manner. JMS provides a standardized way for Java
applications to send and receive messages, making it easier to develop distributed systems. The key
components of JMS include:
Message: The fundamental unit of communication in JMS. A message can contain data, such as text or
binary content, and additional header information. There are two main types of messages in JMS:
TextMessage: Contains a java.lang.String object.
BytesMessage: Contains a stream of uninterpreted bytes.
ObjectMessage: Contains a serializable Java object.
MapMessage: Contains name-value pairs.
StreamMessage: Contains a stream of primitive values.
Producer: A JMS producer is responsible for creating and sending messages to a JMS destination, such as
a queue or a topic. The MessageProducer interface provides methods to send messages to the
destination.
Consumer: A JMS consumer is responsible for receiving and processing messages from a JMS
destination. Consumers subscribe to a specific destination (queue or topic) and use the
MessageConsumer interface to retrieve messages.
Queue: A point-to-point messaging model where each message is consumed by a single consumer. In
JMS, queues are used for point-to-point communication. Multiple consumers can listen to the same
queue, but each message is consumed by only one consumer.
Topic: A publish-subscribe messaging model where each message can be consumed by multiple
subscribers. In JMS, topics are used for publish-subscribe communication. Multiple subscribers can
subscribe to the same topic, and each subscriber receives a copy of the message.
ConnectionFactory: An object that is used to create connections to the JMS provider. It is typically
configured with connection parameters, such as the JMS provider's URL.
Connection: Represents a connection to the JMS provider. The Connection interface provides methods
to create sessions.
Session: A session is a single-threaded context for producing and consuming messages. It is created by a
connection and provides a transactional context if needed. Sessions are used to create producers and
consumers.
By using these components, JMS facilitates reliable and asynchronous communication in distributed
systems. Producers can send messages to destinations, and consumers can asynchronously receive and
process these messages.
2. Write detail notes on Reading and Writing Data between server and Client
This client program is straightforward and simple because the echo server implements a simple
protocol. The client sends text to the server, and the server echoes it back. When your client
programs are talking to a more complicated server such as an HTTP server, your client program
will also be more complicated. However, the basics are much the same as they are in this
program:
1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's protocol.
4. Close the streams.
5. Close the socket.
Java API that can be used to read and write files in Java:
FileReader, FileInputStream, Scanner, FileWriter,
FileOutputStream, etc.
FileReader - Java FileReader class is used to read data from the file. It returns data in byte
format like FileInputStream class. It is character-oriented class which is used for file handling
in java.
Constructor Description
FileReader(String It gets filename in string. It opens the given file in read mode. If file
file) doesn't exist, it throws FileNotFoundException.
FileReader(File file) It gets filename in file instance. It opens the given file in read mode. If file
doesn't exist, it throws FileNotFoundException.
import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
FileInputStream - Java FileInputStream class obtains input bytes from a file. It is used for
reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc. You can
also read character-stream data. But, for reading streams of characters, it is recommended to
use FileReader class.
Scanner : The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods
found in the Scanner class documentation. In our example, we will use the nextLine() method,
which is used to read Strings:
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
Java FileWriter class of java.io package is used to write data in character form to file. Java
FileWriter class is used to write character-oriented data to a file. It is a character-oriented
class that is used for file handling in java.
This class inherits from OutputStreamWriter class which in turn inherits from the Writer
class.
o The main task of the internet is to provide services to users. For example, users want to
run different application programs at the remote site and transfers a result to the local
site. This requires a client-server program such as FTP, SMTP. But this would not allow
us to create a specific program for each demand.
Types of Telnet: At Local Site
o Keystrokes are accepted by the terminal driver when the user types at the terminal.
o Terminal Driver passes these characters to OS.
o Now, OS validates the combination of characters and opens the required application.
The commands in NVT forms are transmitted to the TCP/IP at the remote machine.
Here, the characters are delivered to the operating system and then pass to the TELNET
server. The TELNET server transforms the characters which can be understandable by a
remote computer.
4. Can you provide a comprehensive explanation of the Telnet application implemented in Java,
including detailed information on its functionalities and components?
5. Provide detailed information on the process of reading and writing data between a server and client,
with a focus on the mechanisms and methods involved in Java programming?
6. Can you elaborate on the essential elements of Java Messaging Services (JMS) and their roles in
enabling dependable and asynchronous communication within distributed systems?