Sei sulla pagina 1di 12

PART A

QUESTION 1 :
What are sockets and how do sockets work?
A network socket is an endpoint of a connection across a computer network. Today, most
communication between computers is based on the Internet Protocol; therefore most network sockets
are Internet sockets.
More precisely, a socket is a handle abstract reference that a local program can pass to the
networking application programming interface (API) to use the connection.
Sockets are internally often simply integers, which identify which connection to use. .Sockets allow
communication between two different processes on the same or different machines.
The following figure shows the typical flow of events (and the sequence of issued functions) for a
connection-oriented socket session. An explanation of each event follows the figure.

1) The socket() function creates an endpoint for communications and returns a socket descriptor
that represents the endpoint.
2) When an application has a socket descriptor, it can bind a unique name to the socket. Servers
must bind a name to be accessible from the network.
3) The listen() function indicates a willingness to accept client connection requests. When a
listen() is issued for a socket, that socket cannot actively initiate connection requests. The
listen() API is issued after a socket is allocated with a socket() function and the bind() function
binds a name to the socket. A listen() function must be issued before an accept() function is
issued.
4) The client application uses a connect() function on a stream socket to establish a connection to
the server.
5) Servers use stream sockets to accept a client connection request with the accept() function.
The server must issue the bind() and listen() functions successfully before it can issue an
accept() function to accept an incoming connection request.
6) When a connection is established between stream sockets (between client and server), you
can use any of the socket API data transfer functions. Clients and servers have many data
transfer functions from which to choose, such as send(), recv(), read(), write(), and others.
7) When a server or client wants to cease operations, it issues a close() function to release any
system resources acquired by the socket.

QUESTION 2 :
Differentiate between InputStream and OutputStream and explain how it is used in socket
programming.
InputStream

InputStream is an abstract superclass that provides a minimal programming interface and a


partial implementation of input streams.

The InputStream class defines methods for reading bytes or arrays of bytes, marking locations
in the stream, skipping bytes of input, finding out the number of bytes available for reading, and
resetting the current position within the stream.

An input stream is automatically opened when you create it. You can explicitly close a stream
with the close method, or let it be closed implicitly when the InputStream is garbage collected.

OutputStream

The OutputStream class is an abstract superclass that provides a minimal programming


interface and a partial implementation of output streams.

OutputStream defines methods for writing bytes or arrays of bytes to the stream.

An output stream is automatically opened when you create it.

You can explicitly close an output stream with the close method, or let it be closed implicitly
when the OutputStream is garbage collected, which occurs when the object is no longer
referenced.

QUESTION 3 :
Where can one get a library for programming sockets in Java and other languages?
The library for programming sockets in Java can be obtained in Apache MINA which is a network
application framework which helps users develop high performance and high scalability network
applications easily. It provides an abstract event-driven asynchronous API over various transports
such as TCP/IP and UDP/IP via Java NIO.
Apache MINA is often called:
NIO framework library,
client server framework library, or
a networking socket library

Another way you can get programming sockets is from Netty which is a NIO client server framework
which enables quick and easy development of network applications such as protocol servers and
clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket
server.
Besides that, the java.net package provides two classes--Socket and ServerSocket, that implement
the client side of the connection and the server side of the connection, respectively.

QUESTION 4:
How can my client work through a firewall/proxy server in socket?
A Proxy is a central machine on the network that allows other machines in that network to use a
shared Internet connection. Proxy servers are intermediate servers which accept requests from clients
and forward them to other proxy servers, a source server, or service the request from their own cache.
The proxy is also called 'server' or 'gateway'. Proxy allows users on a network to browse the Web,
send files over FTP, and work with E-mail and other Internet services.
Proxy servers work by opening a socket on the server and allowing the connection to pass through.
A proxy basically does the following:

Receives a request from a client inside the firewall

Sends this request to the remote server outside of the firewall

Reads the response

Sends it back to the client

Usually, the same proxy is used by all of the clients on the network. This enables the proxy to
efficiently cache documents that are requested by several clients.
If you are working through sockd, you will need to "socksify" your application.
Socket Secure (SOCKS) is an Internet protocol that exchanges network packets between a client and
server through a proxy server. SOCKS5 additionally provides authentication so only authorized users
may access a server. Practically, a SOCKS server proxies TCP connections to an arbitrary IP address,
and

provides a means for UDP packets to be forwarded.

QUESTION 5 :
What are the strengths and weaknesses of Java Socket as compared to Remote Method
Invocation?

1. Flexible

Strengths and weaknesses of java socket


Strength
Weakness
1. The java applets

2. Cause low network traffic if efficiently


used

can

establish

communication only with the machine


requested and not with any other

3. Only updated information can be sent.

machine on the network


2. Sockets allow only raw data to be sent
which is that both client and server
need to have mechanisms to interpret
data

Strengths and weaknesses of Remote Method Invocation


Strength
Weakness
1) Simple and clean to implement that
1) Less efficient than Socket objects.
leads to more robust, maintainable and
flexible applications
2) Distributed

systems

allow ignoring the coding, being the


creations

are

allowed while decoupling the client and


server objects simultaneously
3) It is possible to create zero-install client
for the users.
4) No client installation is needed except
java capable browsers
5)

2) Assuming the default threading will

At the time of changing the database,


only the server objects are to be
recompiled but not the server interface
and the client remain the same.

servers are thread- safe and robust.


3) Cannot use the code out of the scope
of java.
4) Security issues need to be monitored
more closely.

QUESTION 6 :
Why do you need multithreading? How can multiple threads run simultaneously in a singleprocessor system
To take advantage of multiprocessor systems. Multiprocessor systems are much more common
than they used to be. Once they were found only in large data centers and scientific computing
facilities. Now many low-end servers systems -- and even some desktop systems -- have multiple
processors.
Simplify modeling - In some cases, using threads can make your programs simpler to write and
maintain. Consider a simulation application, where you simulate the interaction between multiple
entities. Giving each entity its own thread can greatly simplify many simulation and modeling
applications.

In a single-processor system, multiple threads execute , one after the other or wait until one thread
finishes or is preempted by the OS , depending on the thread priority and the OS policy.But the
running threads , gives an illusion that they run simultaneous , relative to the required application
response time of the User space application.

QUESTION 7 :
What are two ways to create threads? When do you use the Thread class, and when do you
use the Runnable interface? What are the differences between the Thread class and the
Runnable interface?
There are the two different ways to create thread. In this case, these are the ways to create threads in
Java.
In these two ways first step we need to override run() method and place corresponding logic that
should be executed concurrently.
The second thing is to call the start() method on Thread class object to create a thread of execution
in Java Stack area.
Thread class provide constructors and methods to create and perform operations on a thread. Thread
class extends Object class and implements Runnable interface. Commonly used Constructors of
Thread class are Thread(), Thread(String name), Thread(Runnable r) and Thread(Runnable r, String
name).
The Runnable interface should be implemented by any class whose instances are intended to be
executed by a thread. Runnable interface have only one method named run() such as public void
run(): is used to perform action for a thread.
Difference between Thread and Runnable interface in Java are:
1) Inheritance Option:

The limitation with "extends Thread" approach is that if you extend

Thread, you cannot extend anything else. Java does not support multiple inheritance. In
reality, you do not need Thread class behavior, because in order to use a thread you need
to instantiate one anyway. On the other hand, implementing the Runnable interface gives
you the choice to extend any class you like, but still define behavior that will be run by
separate thread.
2) Reusability: In "implements Runnable", we are creating a different Runnable class for a
specific behavior job (if the work you want to be done is job). It gives us the freedom to
reuse the specific behavior job whenever required. "Extends Thread" contains both thread
and job specific behavior code. Hence once thread completes execution, it cannot be
restart again.
3) Object Oriented Design: Implementing Runnable should be preferred. It does not

specializing or modifying the thread behavior. You are giving thread something to run. We
conclude that Composition is the better way. Composition means two objects A and B
satisfies has-a relationship. "Extends Thread" is not a good Object Oriented practice.
4) Loosely-coupled: "implements Runnable" makes the code loosely-coupled and easier to
read .Because the code is split into two classes. Thread class for the thread specific code
and your Runnable implementation class for your job that should be run by a thread code.
"Extends Thread" makes the code tightly coupled. Single class contains the thread code as
well as the job that needs to be done by the thread.
QUESTION 8 :
Explain the life-cycle of a thread object. How do you set a priority for a thread? What is the
default priority?
A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in
java new, runnable, non-runnable and terminated. There is no running state.
The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:
1.New
2.Runnable
3.Running
4.Non-Runnable (Blocked)
5.Terminated
Every Java thread has a priority that helps the operating system determine the order in which threads
are scheduled. Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and
MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a
constant of 5).

QUESTION 9 : Describe a thread group. How do you create a thread group?


Thread groups provide a mechanism for collecting multiple threads into a single object and
manipulating those threads all at once, rather than individually. For example, you can start or suspend
all the threads within a group with a single method call. Java thread groups are implemented by the
ThreadGroup class in the java.lang package.

You can create thread groups in the main group. You can also create threads and thread
groups in subgroups of main. The result is a root-like hierarchy of threads and thread
groups:

QUESTION 10 :
How do you start the threads in a group? How do you find the number of active threads in a
group of threads?
ThreadGroup(String name) and ThreadGroup(ThreadGroup parent, String name) are constructors that are needed
to create a thread group and give it a name, as the name parameter specifies. The constructors differ in their
choice of what thread group serves as parent to the newly created thread group.
Each thread group, except system, must have a parent thread group. For ThreadGroup(String name), the parent is
the thread group of the thread that calls ThreadGroup(String name). As an example, if the main thread calls
ThreadGroup(String name), the newly created thread group has the main thread's group as its parent main. For
ThreadGroup(ThreadGroup parent, String name), the parent is the group that parent references.
The following code shows how to use these constructors to create a pair of thread groups:

In the code above, the main thread creates two thread groups: A and B. First, the main thread creates A
by calling ThreadGroup(String name). The tg1-referenced thread group's parent is main because main
is

the

main

thread's

thread

group.

Second,

the

main

thread

creates

by

calling

ThreadGroup(ThreadGroup parent, String name). The tg2-referenced thread group's parent is A because
tg1's reference passes as an argument to ThreadGroup (tg1, "B") and A associates with tg1. By
themselves, thread groups are useless. To be of any use, they must group threads. You group threads
into thread groups by passing ThreadGroup references to appropriate Thread constructors:

The code above first creates a subgroup 2 group with main as the parent group. (I assume the main
thread executes the code.) The code next creates a my thread Thread object in the subgroup 2 group.
A thread group's maximum priority is the highest priority any of its threads can attain. Consider the aforementioned
network server program. Within that program, a thread waits for and accepts requests from client programs.
Before doing that, the wait-for/accept-request thread might first create a thread group with a maximum priority

just below that thread's priority. Later, when a request arrives, the wait-for/accept-request thread creates a new
thread to respond to the client request and adds the new thread to the previously created thread group. The new
thread's priority automatically lowers to the thread group's maximum. That way, the wait-for/accept-request
thread responds more often to requests because it runs more often.

Potrebbero piacerti anche