Network Programming
NETWORK PROGRAMMING
L T P C
3 0 0 3
UNIT I - INTRODUCTION 9
Introduction – Overview of UNIX OS - Environment of a UNIX process - Process control - Process relationships Signals – Interprocess Communication- overview of TCP/IP Protocols.
UNIT II - ELEMENTARY TCP SOCKETS 9
Introduction to Socket Programming –Introduction to Sockets – Socket address Structures – Byte ordering functions – address conversion functions – Elementary TCP Sockets – socket, connect, bind, listen, accept, read, write , close functions – Iterative Server – Concurrent Server.
UNIT III - APPLICATION DEVELOPMENT 9
TCP Echo Server – TCP Echo Client – Posix Signal handling – Server with multiple clients – boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and reboots, Server Shutdown – I/O multiplexing – I/O Models – select function – shutdown function – TCP echo Server (with multiplexing) – poll function – TCP echo Client (with Multiplexing).
UNIT IV - SOCKET OPTIONS, ELEMENTARY UDP SOCKETS 9
Socket options – getsocket and setsocket functions – generic socket options – IP socket options – ICMP socket options – TCP socket options – Elementary UDP sockets – UDP echo Server – UDP echo Client – Multiplexing TCP and UDP sockets – Domain name system – gethostbyname function – Ipv6 support in DNS – gethostbyadr function – getservbyname and getservbyport functions.
UNIT V ADVANCED SOCKETS 9
Ipv4 and Ipv6 interoperability – threaded servers – thread creation and termination – TCP echo server using threads – Mutexes – condition variables – raw sockets – raw socket creation – raw socket output – raw socket input – ping program – trace route
program.
TOTAL = 45
REFERENCES:
1. W. Richard Stevens, “Advanced Programming in The UNIX Environment”, Addison
Wesley, 1999.
2. W. Richard Stevens, “UNIX Network Programming - Volume 1”, Prentice Hall
International, 1998.
Introduction
UNIX
UNIX has been a popular OS for more than two decades because of its multi-user, multi-tasking
environment, stability, portability and powerful networking capabilities as shown in the below
figure.
In the late 1960s, researchers from General Electric, MIT and Bell Labs launched a joint project to develop an ambitious multi-user, multi-tasking OS for mainframe computers known as MULTICS (Multiplexed Information and Computing System). MULTICS failed, but it did inspire Ken Thompson, who was a researcher at Bell Labs, to have a go at writing a simpler operating system himself. He wrote a simpler version of MULTICS on a PDP7 in assembler and called his attempt UNICS (Uniplexed Information and Computing System). Because memory and CPU power were at a premium in those days, UNICS (eventually shortened to UNIX) used short commands to minimize the space needed to store them and the time needed to decode them- hence the tradition of short
UNIX commands we use today, example: ls, cp, rm, mv etc. Ken Thompson then teamed up with Dennis Ritchie, the author of the first C compiler in 1973.They rewrote the UNIX kernel in C - this was a big step forwards in terms of the system's portability - and released the Fifth Edition of UNIX to universities in 1974.
The Seventh Edition, released in 1978, marked a split in UNIX development into two main branches: SYSV (System5) and BSD (Berkeley Software Distribution). BSD arose from the University of California at Berkeley where Ken Thompson spent a sabbatical year. Its development was continued by students at Berkeley and other research institutions. SYSV was developed by AT&T and other commercial companies. UNIX flavors based on SYSV have traditionally been more conservative, but better supported than BSD-based flavors. The latest incarnations of SYSV (SVR4 or System 5 Release 4) and BSD Unix are actually very similar.
Overview of UNIX OS
Introduction
• UNIX is multi-user and multi-tasking operating system.
• Multi-tasking: Multiple processes can run concurrently.
• Example, different users can read mails, copy files, and print all at once.
Logging In
• Enter login name and password !
• System password file: /etc/passwd (usually).
• You can change password using the command: passwd.
Shell
• After a successful login, the shell program is run. The default shell of bingsuns: tcsh
• bingsun2% ps
PID TTY TIME CMD
2159 pts/2 0:00 tcsh
• Shell is a command line interpreter that reads user commands and executes them.
Unix Shells
• Common Shells: Bourne shell, the C shell, and the Korn shell.
• The shell on bingsuns is tcsh (tc shell).
• Users can switch between shells, using the commands bash, csh, ksh, sh.
• Control D (^d) to return back to original shell, or just use the command: exit.
Some shell commands
• Most Important command: man (manual pages).
• Help: unix commands, C functions.
• Usage: man <command/function>
• Try “man man” !
• Example:
manls, man passwd, man printf.
• pwd: working directory (/u0/users/2/kliu1).
• ls: list contents of directory
• mkdirdir-name>: make directory
• rmdirdir-name>: remove an empty directory
• rm –r <dir-name>: remove a directory with all the contents
• cd <directory>: change directory, ~/ means your home directory
• cpsource<target>: copy command.
• chmod<mode<filename>: change mode of a file/directory
• ls –l <directory or filename>: long list with details
• 9 permission bits: d r w x r w x r w x
• 3 categories: user/group/all.
• Permissions: read/write/execute (r/w/x).
• E.g.: mode= 644 means r w _ r_ _ r _ _
• command: chmod 644 <filename>
• first 3 bits for user. Next group. Next all others
• rm<option<filename>: remove files
e.g.: rm –fr directory/filename
• mv <old<new>: change the name of a file
• Pipes: Connect the stdout of one command with the stdin of another command
e.g.: ls -l | more orls –l | less
File System
• Hierarchical arrangement of files and directories.
• Top level: root or /
e.g.: cd /
• . Current directory, .. One level higher directory
e.g.: cd .No change for it is current directory
or cd .. Change to parent directory.
• Pathname: absolute and relative.
• Absolute pathname: /u0/users/2/kliu1
• Relative pathname: abc.
Editors
• Different editors: emacs, pico, vi
• emacs<filename>
• pico<filename>
• vi <filename>
The easiest editor: pico or nano
• pico<filename>
• Full screen editor
• Help on the bottom of the screen
• The nano is an extension to the pico
ARCHITECTURE OF THE UNIX SYSTEM
The below figure depicts the high level architecture of UNIX system.
The Kernel
The kernel is the part of the operating system that interacts directly with the hardware of a computer, through device drivers that are built into the kernel. It provides sets of services that can be used by programs, insulating these programs from the underlying hardware. The major functions of the kernel are to manage computer memory, to control access to the computer, to maintain the file system, to handle interrupts (signals to terminate execution), to handle errors, to perform input and output services (which allow computers to interact with terminals, storage devices, and printers), and to allocate the resources of the computer (such as the CPU or input/output devices) among users.
Programs interact with the kernel through system calls. System calls tell the kernel to carry out various tasks for the program, such as opening a file, writing to a file, obtaining information about a file, executing a program, terminating a process, changing the priority of a process, and getting the time of day Different implementations of a variant of the UNIX system may have compatible system calls, with each call having the same functionality However, the internals, programs that perform the functions of system calls (usually written in the C language), and the system architecture in two different UNIX variants or even two different implementations of a particular UNIX variant may bear little resemblance to one another.
SALIENT FEATURES OF UNIX
· The system is written in a high level language, making it easy to read, understand, change
and move to other machine.
· It has simpler user interface that has the power to provide the services that users want.
· It provides primitives that permit complex programs to be built from the simpler programs.
· It uses hierarchical file systems that allow easy maintenance and efficient implementation.
· Multi-user Capability
· Multi-tasking Capability
· It hides the machine architecture from the user, making it easier to write programs thatrun on different hardware implementations.
· It has excellent provision for communication with fellow users.
Environment of a UNIX Process
Program & Process
• Program is an executable file that resides on the disk.
• Process is an executing instance of a program.
• A Unix process is identified by a unique non-negative integer called the process ID.
• Check process status using the “ps” command.
Foreground/background processes
• A program run using the ampersand operator “&” creates a background process.
• E.g.:
bingsun2% back &
• otherwise it creates a foreground process.
• E.g.:
bingsun2% back
How to stop a process?
• Foreground processes can generally be stopped by pressing CONTROL C (^C).
• Background processes can be stopped using the kill command.
• Usage: kill SIGNAL <process id list>
• kill -9 <process id list> (-9 means no blocked)Or kill <process id list>.
• If a foreground process is not stopping by ^C, you can open another session and use the kill command.
The Environment of a Unix Process
§ Objective:
• How main function is called when program is executed.
• How program terminates execution
• How command-line arguments are passed
• What the typical memory layout is
Alloc memory, use environment variables,….
main function
intmain(intargc, char *argv[]) -- prototype
• When program is started by kernel, a special start-up routine is called before calling main(). Start-up routine is set up by the link editor as part of the executable. Start-up routine takes command-line arguments and environment variable to prepare execution of main function.
How a C program is started and terminated?
Historically, a C program has been composed of the following pieces:
· Text segment, the machine instructions that the CPU executes. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
· Initialized data segment, usually called simply the data segment, containing variables that are specifically initialized in the program. For example, the C declaration
· int maxcount = 99;
appearing outside any function causes this variable to be stored in the initialized data segment with its initial value.
· Uninitialized data segment, often called the "bss" segment, named after an ancient assembler operator that stood for "block started by symbol." Data in this segment is initialized by the kernel to arithmetic 0 or null pointers before the program starts executing. The C declaration
· long sum[1000];
appearing outside any function causes this variable to be stored in the uninitialized data segment.
· Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller's environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn't interfere with the variables from another instance of the function.
· Heap, where dynamic memory allocation usually takes place. Historically, the heap has been located between the uninitialized data and the stack.
Figure shows the typical arrangement of these segments. This is a logical picture of how a program looks; there is no requirement that a given implementation arrange its memory in this fashion. Nevertheless, this gives us a typical arrangement to describe. With Linux on an Intel x86 processor, the text segment starts at location 0x08048000, and the bottom of the stack starts just below 0xC0000000. (The stack grows from higher-numbered addresses to lower-numbered addresses on this particular architecture.) The unused virtual address space between the top of the heap and the top of the stack is large.
Several more segment types exist in an a.out, containing the symbol table, debugging information, linkage tables for dynamic shared libraries, and the like. These additional sections don't get loaded as part of the program's image executed by a process.
Process Control
PROCESS RELATED SYSTEM CALLS
In addition to the process ID, there are other identifiers for every process. The following functions return these identifiers.
It is very simple to print the PID of a running process programmatically. Here is the program that achieves this…
/* pid.c */
int main( )
{
printf ( "Process ID = %d", getpid( ) ) ;
}
Here getpid( ) is a library function which returns the process ID of the calling process. When the execution of the program comes to an end the process stands terminated. Every time we run the program a new process is created. Hence the kernel assigns a new ID to the process each time. This can be verified by executing the program several times—each time it would produce a different output.
fork Function
As we know, our running program is a process. From this process we can create another process. There is a parent-child relationship between the two processes. The way to achieve this is by using a library function called fork( ). This function splits the running process into two processes, the existing one is known as parent and the new process is known as child.
An existing process can create a new one by calling the fork function.
#include <unistd.h