The Socket Interface: Seminar Report
The Socket Interface: Seminar Report
ON
Table of Contents
Acknowledgement
Certificate Page No.
1.Introduction………………………………………………………………………………………………………………….1
2.UNIX I/O Paradigm And Network I/O………………………………………………………….1
3.Sockets…………………………………………………………………………………………………………………………………3
4.Socket Abstraction………………………………………………………………………………………………….4
5.Creating A Socket…………………………………………………………………………………………………..4
6.Socket Inheritance And Termination………………………………………………….……5
7.Specifying A Local Address…………………………………………………………………………..6
8.Connecting Sockets To Destination Addresses…………………………….…7
9.Sendig Data Through A Socket……………………………………………………………………….8
10.Receiving Data Through A Socket…………………………………………………………….9
11.Obtaining Local And Remote Socket addresses………………………….10
12.Specifying A Queue Length For A Server…………………………………………10
13.How A Server Accepts Connections……………………………………………………….11
14.Servers That Handle Multiple Services……………………………………….…13
15.Obtaining And Setting The Internal Host Domain………………….14
16.Obtaining Information About Hosts…………………………………………………….15
17.Obtaining Information About Networks…………………………………………….16
1
18.Obtaining Information About Protocols……………………………………….…17
19.Obtaining Information About Network Services……………………….17
20.Summary………………………………………………………………………………………………………………………….18
21.A Java Based Socket………………………………………………………………………………………….18
References
1.Introduction:
We must have an interface between the application programs and the
protocol software in order to use network facilities. My seminar is on a
model of an interface between application programs and TCP/IP
protocols. The standard of TCP/IP protocol do not specify exactly how
application programs interact with the protocol software. Thus the
interface architecture is not standardized; its design lies outside of scope
of the protocol suite. It is further should be noticed that it is
inappropriate to tie the protocols to a particular interface because no
single interface architecture works well on all systems. In particular,
because protocol software resides in a computer’s operating system,
interface details depend on the operating system.
One thing more, the operations that I will list here, will have no standard
in any sense.
2
2.UNIX I/O Paradigm And Network I/O:
Developed in the late 1960s and early 1970s, Unix was originally
designed as a timesharing system for single processor computers. It is a
process-oriented operating system in which each application program
executes as a user level process. An application program interacts with
the operating system by making system calls. From the programmer’s
point of view, system calls look and behave exactly like other procedure
calls. They take arguments and return one or more results. Arguments
can be values or pointers to objects in the application program.
3
/dev/tcp
3.Sockets:
A socket is defined as an end point for communication .A pair of
processes communicating over a network employs a pair of sockets-one
for each process. A socket is made up of an IP address concatenated with
a port number. In general sockets use client-server architecture. The
waits for incoming client requests by listening to a specified port. Once a
request is received, the server accepts a connection from the client socket
to complete the connection.
Socket
4 (161.25.19.8/80
)
Socket
(146.86.5.20/1024)
4.Socket Abstraction:
The basis for network I/O in UNIX centers on an abstraction i.e. socket.
We can consider socket as a generalization of the UNIX file access
mechanism that provides an endpoint for communication. As with the
file access, application program request the operating system to create a
socket when one is needed. The system returns a small integer that the
application program uses to reference the newly created socket. The
5
application can choose to supply a destination address each time it uses
the socket, or it can choose to bind the destination address to the socket
and avoid specifying destination repeatedly.
Socket performs exactly like UNIX files or devices, so they can be used
with traditional operations like read and write. For example, once an
application program create a socket and creates a TCP connection from
the socket to a foreign destination, the program can use write to send a
stream of data across the connection. To make it possible to use
primitives like reading and writing with both files and sockets, the
operating system allocates socket descriptors and file descriptors from
the same set of integers and makes sure that if a given integer has been
allocated as file descriptor, it will not also be allocated as a socket
descriptor.
5.Creating A Socket:
The Socket system call creates sockets on demand. It takes three integer
arguments and returns an integer result:
6
connectionless datagram delivery service (SOCK_DGRAM) , as well as a
raw type (SOCK_RAW) that allows privileged programs to access low level
protocols or network interfaces. Two additional types have been planned
but not implemented.
7
Both the old and new processes have the same access rights to existing
sockets, and both can access the sockets. Thus it is responsibility of
programmer to ensure that the two processes use the shared socket
meaningfully.
When a process finishes using a socket it calls close. Close has the form:
close(socket)
8
giving merely address as a sequence of bytes, the designers chose to use
a structure for addresses. The structure generally named sockaddr,
begins with a 16 bit ADDRESS FAMILY field that identifies the protocol
suite. To which the address belongs. It is followed by an address of up to
14 octets. The value in the ADDRESS FAMILY field determines the
format of the remaining address octets. For example, the value 2 in the
ADDRESS FAMILY fields means the remaining address octets contain a
TCP/IP address.
0 16 31
ADDRESS FAMILY
ADDRESS OCTETS 0-1
9
Sockets used with connectionless datagram services need not be
connected before they are used, but doing so makes it possible to
transfer data without specifying the destination each time.
The connect system call has the form :
10
Where argument socket specifies the socket in use, argument message
gives the address of data to be sent, argument length specifies the
number of bytes to be sent, and argument flags control the transmission.
The first four arguments are exactly the same as those used with the
send system call. The final two arguments specify a destination address
and give the length of that address.
11
Argument socket specifies a socket descriptor from which data should be
received. Argument buffer specifies the address in memory into which
the message should be placed, and argument length specifies the length
of buffer area. Finally argument flags allows caller to control reception.
The system call recvfrom allows the caller to specify input from an
unconnected socket. It includes additional arguments that allow the
caller to specify where to record the sender’s address. The form is:
Argument socket specifies the socket for which the address is desired.
Argument destaddr is a pointer to structure that will receive socket
address. Finally, argument addrlen is a pointer to an integer that will
receive the length of the address. Getpeername only works with
connected sockets.
12
System call getsockname returns the local address associated with a
socket. It has the form:
The system call listen allows servers to prepare a socket for incoming
connections. Listen puts the sockets in a passive mode ready to accept
connection when the server invokes listen it also informs the operating
system that the protocol software should enqueue multiple simultaneous
requests that arrive at the socket. The form is:
13
card, allowing the socket to receive connection requests from an arbitrary
client.
Once the socket has been established, the server needs to wait for a
connection. To do so it uses the system call accept. A call to accept
blocks until a connection request arrives. It has the form:
14
understanding the machanism lies in the way underlying protocols treat
protocol ports. In TCP a pair of end point define a connection. Thus , it
does not matter how many process use a given local protocol port
number as long as they connrct to different destination. In the case of a
concurrent server , there is one process per client and one additional
process that accepts connections. The socket the master server process
uses has wild card for the foreign destination, allowing it to connect with
an arbitrary foreign site. Each remaining process has a specific foreign
destination . when a TCP segment arrives, it will be sent to the socket
connected to the segment’s source. If no such socket exits, the segment
will be sent to the socket that has a wild card for its foreign destination.
Furthermore , because the socket with a wild card foreign destination
does not have an open connection, it will only honour TCP segments that
request a new connection.
In general , a call to select blocks waiting for one of a set of file descriptor
to become ready. Argument ndesc specifies how many descriptor should
be examined( the descriptors checked are always 0 through ndesc-1).
Argument indesc is a pointer to bit mask that specifies the file descriptor
to check for input, argument outdesc is a pointer to a bit mask that
specifies the file descriptor to check for output, and argument excdesc is
15
a pointer to a bit mask that specifies the file descriptors to check for
exception conditions. Finally, if argument timeout is non zero, if is the
address of an integer that specifies how long to wait for a connection
before returning to caller. A zero value for time out forces the call to
block until a descriptor becomes ready. Because the time out arument
contains the address of the timeout integer and not the integer itself, a
process can request zero delay by passing the address of an integer that
contains zero( i.e. a process call poll to see if I/O is ready).
A call to select returns the number of descriptor from the specified set
that are ready for I/O. It also changes the bit mask specified by indesc,
outdesc and excdesc to inform the application which of the selected file
descriptors are ready. Thus, before calling select, the caller must turn
on those bits that correspond to descriptors to be checked. Following the
call, all bit that remain set to 1 correspond to a ready file descriptor.
To communicate over more than one socket at a time, a process first
creates all the sockets it needs and then uses select to determine which
of them becomes ready for I/O first. Once it finds a socket has become
ready, the process uses the input or output procedures define above to
communicate.
cs.purdue.edu
16
have names taken from the Arthurian legend. Thus , one finds machines
named merlin, Arthur , guenevere, and lancelot. The domain itself has
been named Camelot, so the operating system on each hosts in the group
must be informed that it resides in the Camelot domain. To do so, a
privileged process uses system call setdomainname, which has the form:
setdomainname(name, length)
argument name gives the address of an array of bytes that contain the
name of domain, and argument length is an integer that gives the length
of the name.
Argument name specifies the address of an array where the name should
be stored, and argument length is an integer that specifies the length of
the array.
ptr=gethostbyname(namestr)
17
Argument namestr is pointer to a character string that contains a
domain name for the host. The value returned, ptr, points to a structure
that contains the following information: the official host name, a list of
aliases thathave been registered for the host, the host address type, the
address length, and a list of one or more addresses for the host. More
details can be found in the UNIX programmer’s Manual.
ptr=gethostbyaddr(addr,len, type)
ptr=getnetbyname(name)
18
network, a list of registered aliases, an integer address type and an 32 bit
network address(i.e. an IP address with the host portion set to zero.)
ptr=getnetbyaddr(netaddr, addrtype)
Argument netaddr is a 32 bit network address, and argument addrtype is
an integer that specifies the type of netaddr.
ptr=getprotobyname(name)
ptr=getprotobynumber(number)
19
19. Obtaining Information About Network Services:
ptr=getservbyname(name, proto)
Argument name specifies the address of a string that contains the name
of desired service, and integer argument proto specifies the protocol with
which the service is to be used.the value returned is a pointer to a
structure that contains fields for the name of services, a list of aliases, an
identification of the protocol with which the service is used, and an
integer protocol port number assigned for that service.
20. Summary:
Because TCP/IP protocol software resides inside an operating system,
the exact interface between an application program and TCP/IP protocols
depends on the details of the operating system; it not specified by TCP/IP
protocol standard. We examined the socket interface originally designed
for UNIX, and saw that it adopted the UNIX open-read-write-close
paradigm. To use TCP/IP a must create a socket, bind addresses to it,
accepts incoming connectins, and then communicate using the read or
write primitives. Finally , when finished using a socket , the program
must close it. In addition to socket abstraction and system calls that
operate on sockets, UNIX includes library routines that have programs
create and manipulate IP addresses, convert integers between local
machine format and network standard byte order, an search for
information such as network addresses.
The socket interface has become popular and is widely supported by
many vendors. Vendors who do not offer socket facilities in their
20
operating system often provides a socket library that makes it possible
for program to write applications using socket calls even though the
underlying operating system uses a different set of system calls.
The Server:
The server create a ServerSocket that specifies it will listen to port 5155.
It then begins listening to the port with the accept method . The server
blocks on the accept method waiting for a client to request a connection.
When a connection request is received , accept returns a socket that the
server can use to communicate with the client.
The detail illustrating how the server communicates with socket are as
follows. The server first establishes a PrintWriter object that it will use to
communicate with the client. A PrintWriter object allows the server to
write to the socket using the normal print and println methods for
output. The server process sends the time of day to the client calling the
method println. Once it has written the time of day to the socket, the
server closes the socket to the client and resumes listening for more
request.
21
import java.net.*;
import java.io.*;
22
//we have a connection
pout= new PrintWriter ( client.getOutputStream(), true);
//write the data to the socket
pout.println(new java.util.Date().toString());
pout.close();
client.close();
}
}
}
The Client:
A client communicates with the server by creating a socket and
connecting to the port the server is listening. We implement such a client
in the java program. The client create a socket and requests a connection
with the server at IP address 127.0.0.1 on port 5155. Once the
connection is made, the client can read from the socket using normal
stream I/O statements. After it has received the time of day from server ,
the client closes the socket and exits.
23
be replaced with the IP address of another host running the time-of-day
server.
import java.net.*;
import java.io.*;
public class Client {
public static void main(String agrs[])
{
InputStream in= null;
BufferedReader bin=null;
Socket sock=null;
//make connection to socket
sock=new Socket(“127.0.0.1”,5155);
in=sock.getInputStream();
bin= new BufferedReader( new InputStreamReader(in));
String line;
24
While((line=bin.readLine())!=null)
System.out.println(line);
}
}
References:
1.Internetworking with TCP/IP –Douglas E. Comer- PHI
2.Computer Networks – Andrew S. Tanenbaum-PHI
3.Operating System Concepts-Silberschatz,Galvin,gagne-John
Wiley & Sons, Inc.
4.Unix System-Das-TMH
Internet Resource:
www.mit.edu
25
26