You are on page 1of 29

IT-15

NT

CH.7
SOCKET PROGRAMMING
•A socket is one endpoint of a two way communication link between
two programs running on the network.
•The socket mechanism provides a means of inter-process
communication (IPC) by establishing named contact points between
which the communication take place.
•The socket provides bidirectional FIFO Communication facility over
the network.
•A socket connecting to the network is created at each end of the
communication. Each socket has a specific address. This address is
composed of an IP address and a port number.
•Socket are generally employed in client server applications.
•The server creates a socket, attaches it to a network port addresses
then waits for the client to contact it.
•The client creates a socket and then attempts to connect to the
server socket. When the connection is established, transfer of data
takes place.
Socket Types
There are four types of sockets available to the users. The first two are
most commonly used and the last two are rarely used.
Processes are presumed to communicate only between sockets of the
same type but there is no restriction that prevents communication
between sockets of different types.
Stream Sockets − Delivery in a networked environment is guaranteed. If
you send through the stream socket three items "A, B, C", they will
arrive in the same order − "A, B, C". These sockets use TCP (Transmission
Control Protocol) for data transmission. If delivery is impossible, the
sender receives an error indicator. Data records do not have any
boundaries.
Datagram Sockets − Delivery in a networked environment is not
guaranteed. They're connectionless because you don't need to have an
open connec*on as in Stream Sockets − you build a packet with the
destination information and send it out. They use UDP (User Datagram
Protocol).
Raw Sockets − These provide users access to the underlying
communication protocols, which support socket abstractions. These
sockets are normally datagram oriented, though their exact
characteristics are dependent on the interface provided by the protocol.
Raw sockets are not intended for the general user; they have been
provided mainly for those interested in developing new communication
protocols, or for gaining access to some of the more cryptic facilities of
an existing protocol.
Sequenced Packet Sockets − They are similar to a stream socket, with
the exception that record boundaries are preserved. This interface is
provided only as a part of the Network Systems (NS) socket abstraction,
and is very important in most serious NS applications. Sequenced-
packet sockets allow the user to manipulate the Sequence Packet
Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet
or a group of packets, either by writing a prototype header along with
whatever data is to be sent, or by specifying a default header to be used
with all outgoing data, and allows the user to receive the headers on
incoming packets.
Socket:
1.Sockets are a service provided by transport layer.
2.A socket is one endpoint of a two-way communication link between two programs running on the
network.
Berkeley Socket:
1.Berkeley sockets is an application programming interface (API) for Internet sockets and UNIX
domain sockets.
2.It is used for inter-process communication (IPC).
3.It is commonly implemented as a library of linkable modules.
4.It originated with the 4.2BSD UNIX released in 1983.
Primitive used in Berkeley Socket:
Socket Programming:
I) Server side:
 Server startup executes SOCKET, BIND & LISTEN primitives.
 LISTEN primitive allocate queue for multiple simultaneous clients.
 Then it use ACCEPT to suspend server until request.
 When client request arrives: ACCEPT returns.
 Start new socket (thread or process) with same properties as original, this handles the request,
server goes on waiting on original socket.
 If new request arrives while spawning thread for this one, it is queued.
 If queue full it is refused.
II) Client side:
 It uses SOCKET primitives to create.
 Then use CONNECT to initiate connection process.
 When this returns the socket is open.
 Both sides can now SEND, RECEIVE.
 Connection not released until both sides do CLOSE.
 Typically client does it, server acknowledges.
Berkeley sockets
•Berkeley sockets is an application programming interface (API)
for Internet sockets and Unix domain sockets, used for inter-
process communication (IPC).
•It is commonly implemented as a library of linkable modules.
•It originated with the 4.2BSD Unix released in 1983.
•A socket is an abstract representation (handle) for the local
endpoint of a network communication path.
•The Berkeley sockets API represents it as a file descriptor (file
handle) in the Unix philosophy that provides a common interface
for input and output to streams of data.
•Berkeley sockets evolved with little modification from a de
facto standard into a component of the POSIX specification.
Therefore, the term POSIX sockets is essentially synonymous with
Berkeley sockets.
•They are also known as BSD sockets, acknowledging the first
implementation in the Berkeley Software Distribution.
Socket Protocols
•A protocol is a standard set of rules for transferring data, such as UDP/IP and
TCP/IP.
•An application program can specify a protocol only if more than one protocol
is supported for this particular socket type in this domain.
•Each socket can have a specific protocol associated with it. This protocol is
used within the domain to provide the semantics required by the socket type.
Not all socket types are supported by each domain; support depends on the
existence and implementation of a suitable protocol within the domain.
•The /usr/include/sys/socket.h file contains a list of socket protocol families.
The following list provides examples of protocol families (PF) found in
the socket header file:

PF_UNIX Local communication


PF_INET Internet (TCP/IP)
PF_NS Xerox Network System (XNS)
architecture
PF_NDD AIX NDD
•These protocols are defined to be the same as their corresponding
address families in the socket header file.
•Before specifying a protocol family, the programmer should check
the socket header file for currently supported protocol families. Each
protocol family consists of a set of protocols.

•TCP
•UDP
•IIP
•Internet Control Message Protocol (ICMP)
System Data Structures for Sockets
Specifying an End Point Address
The client and server can now communicate by writing to or reading from their
sockets.
Definition:
A socket is one endpoint of a two-way communication link between two
programs running on the network. A socket is bound to a port number so that
the TCP layer can identify the application that data is destined to be sent to.
•An endpoint is a combination of an IP address and a port number. Every TCP
connection can be uniquely identified by its two endpoints. That way you can have
multiple connections between your host and the server.
•The java.net package in the Java platform provides a class, Socket, that
implements one side of a two-way connection between your Java program and
another program on the network.
•The Socket class sits on top of a platform-dependent implementation, hiding the details
of any particular system from your Java program. By using the java.net.Socket class
instead of relying on native code, your Java programs can communicate over the
network in a platform-independent fashion.
•Additionally, java.net includes the ServerSocket class, which implements a socket
that servers can use to listen for and accept connections to clients. This lesson shows
you how to use the Socket and ServerSocket classes.
•If you are trying to connect to the Web, the URL class and related classes
(URLConnection, URLEncoder) are probably more appropriate than the socket classes.
In fact, URLs are a relatively high-level connection to the Web and use sockets as part
of the underlying implementation.
Major System Calls with Sockets
Utility routines for Integer Conversions
Consider a 16-bit internet that is made up of 2 bytes. There are two
ways to store this value.
Little Endian − In this scheme, low-order byte is stored on the starting
address (A) and high-order byte is stored on the next address (A + 1).
Big Endian − In this scheme, high-order byte is stored on the starting
address (A) and low-order byte is stored on the next address (A + 1).

•To allow machines with different byte order conventions communicate


with each other, the Internet protocols specify a canonical byte order
convention for data transmitted over the network. This is known as
Network Byte Order.
While establishing an Internet socket connection, you must make sure
that the data in the sin_port and sin_addr members of the sockaddr_in
structure are represented in Network Byte Order.
Byte Ordering Functions
Routines for converting data between a host's internal representation and Network Byte Order
are as follows −
Sequence of socket API calls and data flow for TCP
Here, we are going to make one-way client and server communication.
•In this application, client sends a message to the server, server reads the message and prints
it.
•Here, two classes are being used: Socket and ServerSocket.
•The Socket class is used to communicate client and server. Through this class, we can read
and write message.
• The ServerSocket class is used at server-side. The accept() method of ServerSocket class
blocks the console until the client is connected. After the successful connection of client, it
returns the instance of Socket at server-side.
ServerSocket class

You might also like