Chapter 1
Chapter 1
Chapter 1
Chapter 1: Introduction
Rana AlQurem
Fall 2021
1
• Writing programs that communicate with each other over a
computer network
• We need a protocol to govern how data is exchanged
between applications/programs
• A given program can be a client (caller) or a server (callee)
• The client initiates a communication with the server
• The client-server architecture/model is used by most
networked applications
Request
Client Server
Response
2
3
1.1 Introduction (continued)
• Example : Client and Server on different LANs
connected through WAN.
client server
application application
Host Host
with with
TCP/IP TCP/IP
LAN LAN
router router
WAN
router router router router
Figure 1.4 Client and server on different LANs connected through a WAN
4
5
Sockets API
• We need socket API (or, simply a socket) to access (read from
/ write to) the network
• The API is provided by the Operating System
• Example library functions: read(), write(), close(), …
• We will cover the Socket API in UNIX-based systems using the
C programming language
• High-level applications/programs are built upon sockets
– Web browsers/servers; E-mail; Instant messaging; Peer-to-peer
file sharing systems
• Types of Sockets
– Stream sockets implement connection-oriented semantics (TCP)
– Datagram sockets implement connection-less semantics (UDP)
– Raw sockets bypass the transport layer & use IP directly
6
1.2 A Simple Daytime Client
• A TCP daytime client
• Many Unix machines run a daytime server on port #13
• Try on an appropriate UNIX machine
TCP Client
– telnet <machineIPaddress> 13
socket()
8
1.2 A Simple Daytime Client (continued)
• The preamble (include required header files)
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
10
1.2 A Simple Daytime Client (continued)
• 3. Send data to the server (Not needed in this example)
char sendline[500];
// Fill some text in the `sendline’ buffer
write(sockfd, sendline, strlen(sendline)); System call
11
1.4 Error Handling: Wrapper Functions
• Since terminating on an error is the common case, we can shorten our program
by defining a wrapper function that performs the actual function call, tests the
return value, and terminates on an error. (capitalize function name)
• sockfd = Socket(AF_INET, SOCK_STREAM, 0);
int
Socket(int family, int type, int protocol)
{
int n;
if( (n = socket( family, type, protocol)) < 0 )
err_sys(“socket error”);
return (n);
}
• Unix errno Value: a global variable set to a positive value indicating the error when
function fails (the function returns -1)
– The value is undefined if the function returns no error
– Error values are defined in <sys/error.h> header file, and names start with E
12
1.5 A Simple Daytime Server
• A TCP daytime server TCP Server
• Server Development socket()
1. Create TCP socket: get a file descriptor
2. Bind the socket with its local (well-known) port Bind()
3. Listen: convert the socket to a listening socket
4. Accept client connection then send reply
listen()
1.Accept blocks to sleep
2.Accept returns a connected descriptor
Accept()
* (when 3-way handshaking finishes)
*( a new descriptor is returned for new client )
3.Read/write: to a client using the connected descriptor write()
5.Close socket
close()
13
1.5 A Simple Daytime Server
14
1.5 A Simple Daytime Server
– A daytime service simply sends the current date and time as a
character string without regard to the input
– A server listens for TCP connections on TCP port 13
– Once a connection is established the current date and time is
sent out the connection as an ASCII character string
– And any data received is thrown away
– The service closes the connection after sending the quote
15
Practice
• Download the full source code file (daytimetcpcli.c)
from elearning
• Change the code in line 26 to reflect the IP address of a
Linux/Unix machine running the daytime service
• Compile and run the program
$ gcc daytimetcpcli.c -o daytimetcpcli.o
$ ./daytimetcpcli.o
16