Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

NETWORK PROGRAMMING

by
A. Vijayalakshmi
1

Course Topics
System Programming
Process, thread and signals management
File System API
User, Process credentials
Daemons
Inter Process Communication
Socket Programming
Sockets API
Writing client-server applications
Broadcasting and multicasting
Raw sockets and data link access
2

Text Books
Prescribed Text Books
T1 : Stevens, R.W., Unix Network Programming, Vol-I Networking APIS : Sockets
and XTI, Pearson Education/PHI, 2nd Edition, 1998.
T2: Stevens, R.W., Unix Network Programming: Vol-II Inter Process
Communications, Pearson Education/ PHI, 2nd Edition, 1999.
Reference Books
R1: The Linux Programming Interface: Linux and UNIX System Programming
Handbook by Michael Kerrisk, No Starch Press 2010
(http://library.books24x7.com/toc.aspx?bookid=41558)
R2: W.R. Stevens, Advanced Programming in the UNIX Environment, Addition
Wisley, 1998.

Why network programming


WWW(Internet explorer,Firebox)
FTP
P2P(BIT torrent)

Telephone network -Analogy

A telephone call over a telephone network work as follows


Both parties have a telephone installed.
A telephone no is assigned to each telephone
Turn on ringer to listen
Caller lifts the phone and dials a number
Telephone rings and receiver of the rings calls picks it up
Both parties talk and exchange data
After the conversation is over they hang up the phone

Dissecting the analogy


A network application works as follows
An end point for communication(telephone) is created for both
ends.
An address (phone no) is assigned to both ends to distinguish
them from the rest of the network
One of the end points (caller )initiate connection to the other
The other endpoints wait (receiver )for the communication to
start.
Once the connection has been made, data is exchanged(talk)
Once the data has been exchanged ,the end points are
closed(hangup)
6

In the wolds of sockets

Socket () End point for communication [telephone]

Bind () --Assign unique telephone number [telling other people your


number]
Listen() Waits for a caller [turning on the ringer]
Connect () Dial a number

Accept ()-Receive a call

Send() ,Recv() -Talk


Close() Hang up

Client server model


Server-An entity which is a provider of information
Client -An entity which is a seeker of information
Apache is a web server providing web pages and internet
explorer is a web client which requests those pages form the
server
In the socket programming world almost all communication is
base on the client server model.
Server starts up the first and waits for the client to connect to it.
After a client successfully connects,it requests some information.
The server serves this information to the client.The client then
disconnects
And the server waits for more clients
8

A TCP server Client interaction

Network Application

Client

Communication link

Server

10

Server handling multiple clients

Client

Client

Server

Client

11

Introduction

A Simple Daytime Client


A Simple Daytime Server
Error handling: wrapper functions
BSD networking history
Discover details of your local network

12

A Simple Daytime Client

daytimetcpcli.c

1 #include"unp.h"
2 int
3 main(int argc, char **argv)
4 {
5
int sockfd, n;
6
char recvline[MAXLINE + 1];
7
struct sockaddr_in
servaddr;
8
9

if (argc != 2)
err_quit("usage: a.out <IPaddress>");

10
11

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)


err_sys("socket error");

13

12
13
14
15
16

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);

17 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)


18
err_sys("connect error");
19 while ( (n = read(sockfd, recvline, MAXLINE)) > 0)
{
20
recvline[n] = 0;
/* null terminate */
21
if (fputs(recvline, stdout) == EOF)
22
err_sys("fputs error");
23
}
24
if (n < 0)
25
err_sys("read error");
26
exit(0);
27 }

14

A Simple Daytime Client

Include Header
Command-line arguments
Create TCP socket: get a file descriptor
Prepare server address structure: fill-in IP address and port number
Connect to the server: bind the file descriptor with the remote server
Read/write from/to server
Close socket

15

Protocol Independence
#include

daytimetcpcliv6.c

"unp.h"

int main(int argc, char **argv)


{
int sockfd, n;
struct sockaddr_in6 servaddr;
char recvline[MAXLINE + 1];
if (argc != 2)
err_quit("usage: a.out <IPaddress>");
if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
err_sys("socket error");

16

bzero(&servaddr, sizeof(servaddr));
servaddr.sin6_family = AF_INET6;
servaddr.sin6_port = htons(13); /* daytime server */
if (inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");
while ( (n = rea00d(sockfd, recvline, MAXLINE)) > 0)
{
recvline[n] = 0;
/* null terminate */
if (fputs(recvline, stdout) == EOF)
err_sys("fputs error");
}
if (n < 0)
err_sys("read error");
exit(0);
}

17

Error Handling: wrappers

/* include Socket */
int
Socket(int family, int type, int protocol)
{
int
n;
if ( (n = socket(family, type, protocol)) < 0)
err_sys("socket error");
return(n);
}
/* end Socket */

18

Unix errno Value

When an error occurs in a Unix function, the


global variable errno is set to a positive value
indicating the type of error and the function
normally returns -1.

19

daytimetcpsrv.c

A Simple Daytime Server


1 #include"unp.h"
2 #include<time.h>
3 int
4 main(int argc, char **argv)
5
{
6
intlistenfd, connfd;
7
struct sockaddr_inservaddr;
8
charbuff[MAXLINE];
9
time_tticks;
10

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

20

11
12
13
14

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port= htons(13); /* daytime server */

15 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));


16 Listen(listenfd, LISTENQ);
17 for ( ; ; ) {
18 connfd = Accept(listenfd, (SA *) NULL, NULL);
19
20
21
22
23
24

ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(connfd, buff, strlen(buff));
Close(connfd);
}
}

21

A Simple Daytime Server

Create TCP socket: get a file descriptor


Bind the socket with its local port
Listen: convert the socket to a listening descriptor
Accept blocks to sleep
Accept returns a connected descriptor
Read/write
Close socket

22

OSI Model
Application
details
7

Application

Presentation

Session

Transport

Network

Datalink

Physical

OSI model

Application

TCP

User
process

UDP

IPv4,IPv6
Device
Driver
And
Hardware

kernel

Communication
details

Internet protocol
suite

23

BSD Networking History

BSD releases:
Licensed: 4.2 BSD with TCP/IP and socket APIs(1983), 4.3
BSD (1986), 4.3 BSD Tahoe (1988), 4.3 Reno (1990), 4.4 BSD
(1993)
Non-licensed: Net/1 (Tahoe) (1989), Net/2 (Reno) (1991), 4.4
BSD-Lite (Net/3) (1994), 4.4 BSD-Lite2 (1995) ---> BSD/OS,
FreeBSD, NetBSD, OpenBSD
System V Release 4: Solaris and Linux
UNIX Standards: POSIX and The Open Group

24

Discovering Details of Your Local Network

To find out interfaces: netstat -ni


To find out routing table: netstat -rn
To find out details of an interface: ifconfig
To discover hosts on a LAN: ping

25

You might also like