Sunteți pe pagina 1din 20

Socket Programming

Socket
• A socket is an end-point for communication between
processes running on different machines.
Or
• A socket is one end-point of a two communication
link between two programs running on the network.
• It consists of an IP address and a port
number which uniquely identifies a running process
on a system.
Contd..
• IP Address identifies a machine and Port
No. identifies the specific application ( process ).
Client-Server Architecture
• In a client-server architecture of computing,
a server hosts a resource or a service which is
accessed by clients.
• A server is basically a machine which provides some
kind of services.
• Client will send a request to the server and server will
do some processing and send back a response to the
client.
Contd…

Request Service

Client Machine Server Machine

Response Service
Contd..
• Example:
Apache is a Web Server providing web pages
and Internet Explorer is a web client which
request those web pages from the server.
Contd…

1. The server starts up first.


2. Waits for a client to connect to it.
3. After a client successfully connects.
4. It requests some information (web pages).
5. The server serves this information to the client.
6. The client then disconnect and the server waits for
more clients.
Day-to-Day Scenario
• A telephone call over a “telephony network” works as follows:
1. Both parties have a telephone installed.
2. A phone number is assigned to each telephone which is
unique for the telephonic network.
3. Turn on ringer to listen for a caller.
4. Caller lifts telephone and dials a number.
5. Telephone rings and the receiver of the call picks it up.
6. Both parties talk and exchange data.
7. After conversation is over they hang-up the phone.
Network Application work as follows:
1. An end-point for communication is created on both
ends.
2. An address is assigned to both the ends to
distinguish them from the rest of the network.
3. One of the endpoints initiate a connection to the
network.
4. The other end-points waits for the communication
to start.
5. Once a connection has been made, data is
exchanged.
6. Once data has been exchanged the endpoints are
closed.
Comparison between two Network
Telephony Network Network Application
1. Telephone 1. End-points
2. Phone No. is assigned 2. IP Address
3. Caller 3. One end-point initiate
4. Receiver 4. Other end-point waits
5. Talk 5. Data is exchanged
6. Hang-up 6. End-points are closed
Socket & ServerSocket Classes
• Socket classes are used to represent the
connection between a client program and a
server program.
• The java.net package provides these two
classes
1. Socket
2. ServerSocket
that implement the client side of the
connection and the server side of the
connection.
Contd…
• Often client and server communicates over network
using sockets.
• The ServerSocket class is used by the server side
program to obtain and bind to a port and listen for
incoming connections from client.
• The Socket class is used by both client and server to
communicate with each other.
Network Application Communication

• Client and Server wants to communicate over


a network by sending and receiving messages
over the network.
• ServerSocket -> Job of a ServerSocket is to
wait for incoming client connection request.
• The server does not have a socket initially.
• The server only has the ServerSocket object.
Contd…
• The socket on the client side will try to established a
connection with the server by sending a request.
• ServerSocket will be waiting for the request.
• As soon as the ServerSocket get the request, accepts
the request.
• After the request is accepted a socket object will be
created on the server side.
• Once a socket object is created, the connection is
established between these two socket objects.
• The client and server starts sending and receiving
messages.
Primitives
1. Socket() : End points for communication
2. Bind() : Attaching to an IP address and port
number.
3. Listen() : Wait for a connection
4. Connect() : Connect to a service
5. Accept() : accept a connection
6. Send() and Recv() : send and receive data
7. Close() : close a socket
Classes & Methods for Socket
Programming
• Java InetAddress class
• Java InetAddress class represents an IP address.
• The java.net.InetAddress class provides methods to
get the IP of any host name.
• Example:
www.javatpoint.com,
www.google.com,
www.facebook.com etc.
Methods of InetAddress class
• InetAddress getByName(String host)  it returns the
instance of InetAddress containing LocalHost IP and
name.
• InetAddress getLocalHost() it returns the instance
of InetAdddress containing local host name and
address.
• getHostName()  it returns the host name of the IP
address.
• getHostAddress() it returns the IP address in string
format.
URL Class
• The Java URL class represents an URL.
• URL is an acronym for Uniform Resource
Locator.
• It points to a resource on the World Wide Web.
A URL contains many information:

• Protocol
• Server name or IP Address
• Port Number
• File Name or directory name
Methods of Java URL class
• getProtocol()  it returns the protocol of the
URL.
• getHost() it returns the host name of the
URL.
• getPort() it returns the Port Number of the
URL.
• getFile()  it returns the file name of the
URL.
• openConnection()it returns the instance of
URLConnection i.e. associated with this URL.

S-ar putea să vă placă și