NETWORK SOFTWARE
How to
implement network software is an essential part of understanding computer
networks. This section first introduces some of the issues involved in
implementing an application program on top of a network, and then goes on to
identify the issues involved in implementing the protocols running within the
network. In many respects, network applications and network protocols are very
similar—the way an application engages the services of the network is pretty
much the same as the way a high-level protocol invokes the services of a
low-level protocol.
1 Application Programming
Interface (Sockets)
Most
network protocols are implemented in software (especially those high in the
protocol stack), and
nearly
all computer systems implement their network protocols as part of the operating
system, when we refer to the interface “exported by the network,” we are
generally referring to the interface that the OS
provides
to its networking subsystem. This interface is often called the network application programming interface (API).
The
advantage of industry-wide support for a single API is that applications can be
easily ported from one OS to another, and that developers can easily write
applications for multiple OSs. Just because two systems support the same
network API does not mean that their file system, process, or graphic
interfaces are the same. Still, understanding a widely adopted API like Unix
sockets gives us a good place to start.
Each
protocol provides a certain set of services,
and the API provides a syntax by
which those services can be invoked in this particular OS.
·
int socket(int domain, int type, int protocol)
·
int bind(int socket, struct sockaddr *address, int
addr_len)
·
int listen(int socket, int backlog)
·
int accept(int socket, struct sockaddr *address,
int *addr_len)
· int connect(int socket, struct sockaddr *address, intaddr_len)
· int send(int socket, char *message, int msg_len, int flags)
·
int recv(int socket, char *buffer, int buf_len, int
flags)
2. Example Application
The
implementation of a simple client/server program that uses the socket interface
to send messages over a TCP connection is discussed. The program also uses
other Unix networking utilities, Our application allows a user on one machine
to type in and send text to a user on another machine. It is a simplified
version of the Unix talk program,
which is similar to the program at the core of a web chat room.
Client
program :
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<netdb.h>
#define
SERVER_PORT 5432 #define MAX_LINE 256
int
main(int
argc, char * argv[])
{
FILE *fp;
struct
hostent *hp; struct sockaddr_in sin; char *host;
char
buf[MAX_LINE]; int s;
int len;
if
(argc==2) { host = argv[1];
}
else {
fprintf(stderr,
"usage: simplex-talk host\n"); exit(1);
}
/*
translate host name into peer’s IP address */ hp = gethostbyname(host);
if (!hp)
{
fprintf(stderr,
"simplex-talk: unknown host: %s\n", host); exit(1);
}
/* build
address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family
= AF_INET;
bcopy(hp->h_addr,
(char *)&sin.sin_addr, hp->h_length); sin.sin_port = htons(SERVER_PORT);
/* active
open */
if ((s =
socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk:
socket");
exit(1);
}
if
(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
exit(1);
}
/* main
loop: get and send lines of text */
while
(fgets(buf, sizeof(buf), stdin)) { buf[MAX_LINE-1] = ’\0’;
len =
strlen(buf) + 1; send(s, buf, len, 0);
}
}
Server Program :
#include
<stdio.h> #include <sys/types.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netdb.h>
#define
SERVER_PORT 5432 #define MAX_PENDING 5 #define MAX_LINE 256
int
main()
{
struct
sockaddr_in sin; char buf[MAX_LINE]; int len;
int s,
new_s;
/* build
address data structure */ bzero((char *)&sin, sizeof(sin)); sin.sin_family
= AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s =
socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("simplex-talk:
socket");
exit(1);
}
if
((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s,
MAX_PENDING);
/* wait
for connection, then receive and print text */ while(1) {
if
((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
while
(len = recv(new_s, buf, sizeof(buf), 0)) fputs(buf, stdout);
close(new_s);
}
}
3 Protocol Implementation Issues
The rest
of this section discusses the two primary differences between the network API
and the protocol-to-protocol interface found lower in the protocol graph.
Process Model
Most
operating systems provide an abstraction called a process, or alternatively, a thread.
Each process runs largely independently of other processes, and the OS is
responsible for making sure that resources, such as address space and CPU
cycles, are allocated to all the current processes.
Message Buffers
A second
inefficiency of the socket interface is that the application process provides
the buffer that contains the outbound message when calling send, and similarly
it provides the buffer into
which an
incoming message is copied when invoking the receive operation.This forces the
topmost protocol to copy the message from the application’s buffer into a
network buffer, and vice versa.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.