An Introductory 4'4BSD Interprocess Communication Tutorial - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

An Introductory 4'4BSD Interprocess Communication Tutorial

Description:

{ fprintf(stderr, '%s: unknown host0, argv[1]); exit(2); Sending Internet domain datagrams ... server.sin_port = htons(atoi(argv[2])); if (connect(sock, &server, ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 42
Provided by: acade116
Category:

less

Transcript and Presenter's Notes

Title: An Introductory 4'4BSD Interprocess Communication Tutorial


1
An Introductory 4.4BSDInterprocess Communication
Tutorial
  • Stuart Sechrest

2
The Socket Interface
  • Funded by ARPA (Advanced Research Projects
    Agency) in 1980.
  • Developed at UC Berkeley
  • Objective to transport TCP/IP software to UNIX
  • The socket interface has become a standard.

3
History of Sockets
  • Sockets were introduced in 1981 as the Unix BSD
    4.2 generic interface for Unix to Unix
    communications over networks (IPC between
    machines).
  • In 1985, SunOS introduced NFS and RPC over
    sockets.
  • In 1986, ATT introduced the Transport Layer
    Interface (TLI) with socket-like functionality
    but more network independent.
  • Another popular socket Interface Winsock

4
Berkeley Unix 4.4BSD IPC choices.
  • PIPES a one-way mechanism that allows two related
    processes ( one could be an ancestor of the
    other) to send a byte stream from one to the
    other.
  • Sockets software entity that connects an
    application to a network protocol.
  • Socketpairs a pair of connected sockets for
    two-way stream communication.

5
Concepts
  • A program is both a sequence of statements and a
    rough way of referring to the computation that
    occurs when the compiled statements are run.
  • A process is an instance of a program running in
    a computer
  • Forking is when a program control splits into two
    independent lines.

6
Fork() system call.
  • fork()
  • This system call causes the current process to
    be split into two processes - a parent process,
    and a child process.
  • In the parent process. the return value is the
    process ID (PID) of the child process.
  • Inside the child process the return value is '0'.
  • In case of failure (not enough memory, too many
    processes, etc.), no new process is created, and
    the return value of the call is '-1'.

7
Pipes.
  • A pipe is a one-way communication mechanism, with
    one end opened for reading and the other end for
    writing.
  • pipe() system call is used to create a read-write
    pipe that may later be used to communicate with a
    forked off process.
  • An array of 2 integers as the argument that will
    be used to save the two file descriptors used to
    access the pipe.
  • first to read from the pipe
  • second to write to the pipe.

8
Use of a pipe
include ltstdio.hgt define DATA PIPE
example" main() int sockets2, child /
Create a pipe / if (pipe(sockets) lt 0)
perror("opening stream socket pair")
exit(10) if ((child fork()) -1)
perror("fork")
9
Use of pipe (continued)
else if (child) char buf1024
/ This is still the parent. It reads the childs
message. / close(sockets1) if
(read(sockets0, buf, 1024) lt 0)
perror("reading message")
printf("--gts\n", buf) close(sockets0)
else / This is the child. It writes a message
to its parent. / close(sockets0) if
(write(sockets1, DATA, sizeof(DATA)) lt
0) perror("writing message") close(sockets1)

10
Effect of a fork.
11
Socketpairs
  • Socketpair a pair of connected sockets for
    two-way stream communication.
  • socketpair(int d, int type, int protocol, int
    sv2)
  • The call creates an unnamed pair of connected
    sockets in the specified domain d, of the
    specified type, and using the optionally
    specified protocol. The descriptors used in
    referencing the new sockets are returned in sv0
    and sv1.
  • It returns 0 on success and -1 otherwise

12
Use of a socket pair
include ltsys/types.hgt include
ltsys/socket.hgt include ltstdio.hgt define DATA1
First message . . ." define DATA2 Second
message . . ." main() int sockets2,
child char buf1024 if (socketpair(AF_UNIX,
SOCK_STREAM, 0, sockets) lt 0) perror("opening
stream socket pair") exit(1)
13
Use of a socket pair (continued)
if ((child fork()) -1)
perror("fork") else if (child) / This is the
parent. / close(sockets0)
if (read(sockets1, buf, 1024, 0) lt 0)
perror("reading stream message")
printf("--gts\n", buf) if
(write(sockets1, DATA2, sizeof(DATA2)) lt 0)
perror("writing stream message")
close(sockets1) else / This is the child.
/ close(sockets1) if (write(sockets0,
DATA1, sizeof(DATA1)) lt 0) perror("writing
stream message") if (read(sockets0, buf, 1024,
0) lt 0) perror("reading stream
message") printf("--gts\n", buf) close(sockets0
)
14
Effect of the fork
15
Sockets
  • The idea of sockets is that there is a server
    that is listening on a particular port at a
    particular address.
  • A client tries to open a connection with the
    server. Considering that everything is in order
    (no errors), the server accepts the connection
    and thus creates a socket.

16
Sockets
  • Once the socket is created, communication between
    the two computers can take place.
  • There are several different ways a connection can
    be established.
  • Examples
  • TCP / UDP (Connection vs. Connectionless)
  • IP / IPX

17
Modes of services available.Connection-oriented
and connectionless
  • Connection-oriented service an application
    program must first establish a connection to the
    other end before the actual communication can
    take place.(reliable delivery service)
  • Connectionless mode the application sends its
    data immediately without waiting for the
    connection to get established.

18
Socket Calls for connection-oriented communication
19
Socket calls for Connectionless communication
20
Socket Functions
  • socket create a descriptor for use in network
    communication.
  • int socket(int family, int type, int protocol)
  • connect connect to a remote peer (used by
    client)
  • int connect(int sockfd, struct sockaddr addr,
    int addrlen)
  • bind bind a local IP address and protocol number
    to a socket.
  • int bind(int sockfd, string addr , int port)
  • listen place the socket in passive mode and set
    the number of incoming TCP connections the system
    will enqueue.
  • int listen(int s, int backlog)
  • accept accept the next incoming connection (by
    server).
  • int accept(int s, struct sockaddr addr,
    socklen_t addrlen)

21
Socket Functions (Continued)
  • close terminate communication and deallocate a
    descriptor.
  • write send outgoing data across a connection.
  • int write(int sockfd, void buf, int count)
  • read acquire incoming data from a connection.
  • int read(int sockfd, void buf, int count)

22
Creating A Socket
  • The socket system call establishes the end point
    of a communications link.
  • sdsocket(family, type, protocol)
  • family AF_UNIX, AF_INET, PF_INET or AF_IMPLINK
  • type SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
  • protocol IPPROTO_TCP _UDP _ICMP or _RAW
  • The socket descriptor sd is then used in system
    calls.
  • Close sockets with close()

23
Domain
  • Domain is the space from which an address is
    drawn.
  • There are several Domains for sockets. For the
    purpose of this presentation the internet domain
    AF_INET will be used in the code Examples.
  • Addresses in the Internet domain consist of a
    machine network address and a port.

24
Type of Communication
  • SOCK_STREAM provides a data delivery service as a
    sequence of bytes and does not preserve message
    boundaries.
  • SOCK_DGRAM provides a data delivery service in
    blocks of bytes called datagrams or packet.
  • A datagram is an independent, self-contained
    message sent over the network whose arrival,
    arrival time, and content are not guaranteed.

25
Data Conversion Functions
  • The functions htons, ntohs, htonl, and ntohl are
    used to convert binary integers between the
    hosts native byte order and the network standard
    byte order.
  • This makes the source code portable to any
    machine, independent of the the native byte order

26
Code Examples
  • Internet Domain Datagrams

27
Reading Internet domain datagrams
include ltsys/types.hgt include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltstdio.hgt / In the included file
ltnetinet/in.hgt a sockaddr_in is defined as
follows struct sockaddr_in short
sin_family u_short sin_port struct in_addr
sin_addr char sin_zero8 This
program creates a datagram socket, binds a name
to it, then reads from the socket. /
28
Reading Internet domain datagrams
main() int sock, length struct
sockaddr_in name char buf1024 / Create
socket from which to read. / sock
socket(AF_INET, SOCK_DGRAM, 0) if (sock lt 0)
perror("opening datagram socket")
exit(1) / Create name with wildcards.
/ name.sin_family AF_INET name.sin_addr.s_addr
INADDR_ANY name.sin_port 0 if (bind(sock,
name, sizeof(name))) perror("binding
datagram socket") exit(1)
29
Reading Internet domain datagrams
/ Find assigned port value and print it out.
/ length sizeof(name) if (getsockname(sock,
name, length)) perror("getting socket
name") exit(1) printf("Socket has port
d\n", ntohs(name.sin_port)) / Read from the
socket / if (read(sock, buf, 1024) lt 0)
perror("receiving datagram packet") printf("--gts
\n", buf) close(sock)
30
Sending Internet domain datagrams
include ltsys/types.hgt include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltnetdb.hgt include ltstdio.hgt define DATA
DATAGRAM example . . ." / Here I send a
datagram to a receiver whose name I get from the
command line arguments. The form of the command
line is dgramsend hostname portnumber / main(ar
gc, argv) int argc char argv int
sock struct sockaddr_in name struct hostent
hp, gethostbyname()
31
Sending Internet domain datagrams
/ Create socket on which to send. / sock
socket(AF_INET, SOCK_DGRAM, 0) if (sock lt 0)
perror("opening datagram socket")
exit(1) / Construct name, with no
wildcards, of the socket to send to.
Getnostbyname() returns a structure including the
network address of the specified host. The port
number is taken from the command line. / hp
gethostbyname(argv1) if (hp 0)
fprintf(stderr, "s unknown host0, argv1)
exit(2)
32
Sending Internet domain datagrams
bcopy(hp-gth_addr, name.sin_addr,
hp-gth_length) name.sin_family
AF_INET name.sin_port htons(atoi(argv2)) /
Send message. / if (sendto(sock, DATA,
sizeof(DATA), 0, name, sizeof(name)) lt 0)
perror("sending datagram message") close(sock)

33
Code Examples
  • Internet Domain stream connection

34
Passive/Active Socket
  • A passive socket is used by a server to wait for
    an incoming connection.
  • An active socket is used by a client to initiate
    a connection.

35
Client Internet Domain Stream Connection
include ltsys/types.hgt include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltnetdb.hgt include ltstdio.hgt define DATA "Half a
league, half a league . . ." / This program
creates a socket and initiates a connection with
the socket given in the command line. One message
is sent over the connection and then the socket
is closed, ending the connection. The form of the
command line is streamwrite hostname
portnumber / main(argc, argv) int argc char
argv
36
Client Internet Domain Stream Connection
int sock struct sockaddr_in server struct
hostent hp, gethostbyname() char buf1024 /
Create socket / sock socket(AF_INET,
SOCK_STREAM, 0) if (sock lt 0)
perror("opening stream socket")
exit(1) / Connect socket using name specified
by command line. / server.sin_family
AF_INET hp gethostbyname(argv1) if (hp
0) fprintf(stderr, "s unknown host0,
argv1) exit(2)
37
Client Internet Domain Stream Connection
bcopy(hp-gth_addr, server.sin_addr,
hp-gth_length) server.sin_port
htons(atoi(argv2)) if (connect(sock, server,
sizeof(server)) lt 0) perror("connecting
stream socket") exit(1) if (write(sock,
DATA, sizeof(DATA)) lt 0) perror("writing on
stream socket") close(sock)
38
Server Internet Domain Stream Connection
include ltsys/types.hgt include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltnetdb.hgt include ltstdio.hgt define TRUE
1 /This program creates a socket and then begins
an infinite loop. Each time through the loop it
accepts a connection and prints out messages from
it. When the connection breaks, or a termination
message comes through, the program accepts a new
connection. / main() int sock, length struct
sockaddr_in server int msgsock char
buf1024 int rval int i
39
Server Internet Domain Stream Connection
/ Create socket / sock socket(AF_INET,
SOCK_STREAM, 0) if (sock lt 0)
perror("opening stream socket") exit(1) /
Name socket using wildcards / server.sin_family
AF_INET server.sin_addr.s_addr
INADDR_ANY server.sin_port 0 if (bind(sock,
server, sizeof(server))) perror("binding
stream socket") exit(1) / Find out
assigned port number and print it out / length
sizeof(server) if (getsockname(sock, server,
length)) perror("getting socket name")
exit(1)
40
Server Internet Domain Stream Connection
printf("Socket has port d\n",
ntohs(server.sin_port)) / Start accepting
connections / listen(sock, 5) do msgsock
accept(sock, 0, 0) if (msgsock -1)
perror("accept") else do
bzero(buf, sizeof(buf)) if ((rval
read(msgsock, buf, 1024)) lt 0)
perror("reading stream message")
i 0 if (rval 0)
printf("Ending connection\n")
else printf("--gts\n", buf)
while (rval ! 0) close(msgsock)
while (TRUE)
41
Any Questions ?
Write a Comment
User Comments (0)
About PowerShow.com