You are on page 1of 5

Agenda

Client Server Model


Net. Programming: Socket (API)
Network Programming Socket Programming
(Supplement)

Jaringan Komputer
(IKI-20240)
Acuan: Bab 1.4 Implementing Network Software
Johny Moningka
(moningka@cs.ui.ac.id)
Fakultas Ilmu Komputer
Universitas Indonesia
Semester 2003/2004
Versi: 1.1 2 Fasilkom UI v-1.1

Network Software Software to Send and Receive

Realisasi cetak biru SW Send steps


Implementasi protokol & arsitektur => software 1: Application copies data to OS buffer
sebagai bagian dari OS => menentukan sukses 2: OS calculates checksum, starts timer
tidaknya protokol. 3: OS sends data to network interface HW and says start
Application Programming Interface (API) => SW Receive steps
Dukungan software & kemudahan programming 3: OS copies data from network interface HW to OS buffer
aplikasi terjaring => populernya protokol dan 2: OS calculates checksum, if matches send ACK; if not,
pemakaian jaringan. deletes message (sender resends when timer expires)
Contoh Internet: realisasi interface protokol Sequence of steps for SW: protocol
TCP/IP di-”bundle” dengan distribusi UNIX OS 1: If OK, OS copies data to user address space and
(4.2 BSD Socket) signals application to continue
• Pemakaian meluas model programming Unix Socket
• De facto standar implementasi protokol jaringan

3 Fasilkom UI v-1.1 4 Fasilkom UI v-1.1

OS & Internet Application A Client-Server Transaction

Every network application is based on the client-server


model:
Internet client host Internet server host A server process and one or more client processes
Server manages some resource.
Client User code Server
Sockets interface
Server provides service by manipulating resource for
(system calls) clients.
TCP/IP Kernel code TCP/IP
1. Client sends request
Hardware interface
(interrupts) Client Server
process process Resource
Network Hardware Network
4. Client 3. Server sends response 2. Server
adapter and firmware adapter
handles handles
response request

Global IP Internet
Note: clients and servers are processes running on hosts
(can be the same or different hosts).

5 Fasilkom UI v-1.1 6 Fasilkom UI v-1.1

1
A Programmer’s View of the Internet Internet Connections

Identifikasi komputer (host) dipetakan dalam Clients and servers communicate by sending
bentuk alamat: 32-bit IP Address: streams of bytes over connections.
Contoh: 128.2.203.179 (notasi desimal utk 32 bit) Connections are point-to-point, full-duplex (2-
way communication), and reliable.
Kumpulan IP address selanjutnya dipetakan ke
identifikasi alamat domain yang lebih flexible: Client socket address Server socket address
128.2.194.242 : 51213 208.216.181.15 : 80
Internet domain names:
Contoh: 128.2.203.179 => www.cs.cmu.edu Server
Client
Connection socket pair (port 80)
(128.2.194.242:51213, 208.216.181.15:80)
Sebuah proses pada komputer dapat
Client host address Server host address
berhubungan dengan komputer lain melalui 128.2.194.242 208.216.181.15
“koneksi” pada jaringan Internet (menggunakan
Note: 51213 is an Note: 80 is a well-known port
identifikasi alamat di atas). port allocated associated with Web servers
by the kernel
7 Fasilkom UI v-1.1 8 Fasilkom UI v-1.1

Clients Using Ports to Identify Services

Examples of client programs Server host 128.2.194.242


Web browsers, ftp, telnet, ssh Client host Service request for Web server
How does a client find the server? 128.2.194.242:80 (port 80)
(i.e., the Web server)
The IP address in the server socket address identifies the Client Kernel
host (more precisely, an adapter on the host) Echo server
(port 7)
The (well-known) port in the server socket address
identifies the service, and thus implicitly identifies the
server process that performs that service.
Examples of well know ports
• Port 7: Echo server Service request for Web server
• Port 23: Telnet server 128.2.194.242:7 (port 80)
(i.e., the echo server)
• Port 25: Mail server Client Kernel
• Port 80: Web server Echo server
(port 7)

9 Fasilkom UI v-1.1 10 Fasilkom UI v-1.1

Servers Server Examples

Servers are long-running processes (daemons). Web server (port 80)


Created at boot-time (typically) by the init process Resource: files/compute cycles (CGI programs)
(process 1) Service: retrieves files and runs CGI programs on behalf
Run continuously until the machine is turned off. of the client
Each server waits for requests to arrive on a FTP server (20, 21)
See /etc/services for a
well-known port associated with a particular Resource: files comprehensive list of the
service. Service: stores and retrieve files services available on a
Linux machine.
Port 7: echo server Telnet server (23)
Port 23: telnet server Resource: terminal
Port 25: mail server Service: proxies a terminal on the server machine
Port 80: HTTP server Mail server (25)
Resource: email “spool” file
A machine that runs a server process is also
Service: stores mail messages in spool file
often referred to as a “server.”
11 Fasilkom UI v-1.1 12 Fasilkom UI v-1.1

2
Sockets

What is a socket?
To the kernel, a socket is an endpoint of
communication.
Socket Programming To an application, a socket is a file descriptor that
lets the application read/write from/to the network.
• Remember: All Unix I/O devices, including networks,
are modeled as files.
Jaringan Komputer Clients and servers communicate with each by
(IKI-20240) reading from and writing to socket descriptors.
Johny Moningka The main distinction between regular file I/O and
(moningka@cs.ui.ac.id)
socket I/O is how the application “opens” the
Fakultas Ilmu Komputer
Universitas Indonesia
socket descriptors.
Semester 2003/2004
Versi: 1.1 14 Fasilkom UI v-1.1

A Simple Client-Server Program The socket() System Call


Server
socket() Creates a socket of a particular type
int socket (int family, int type, int protocol)
Client bind() Well-known
port Family
socket()
• AF_INET: IPv4 protocols
listen()
• AF_INET6: IPv6 protocols
connect() • AF_LOCAL: UNIX socket
accept()
• AF_ROUTE: Routing socket
write()
read()
Type
eof()? • SOCK_STREAM: Stream (TCP) socket
write() • SOCK_DGARM: Datagram (UDP) socket
read()
• SOCK_RAW: Raw (IP) socket

close() close()

15 Fasilkom UI v-1.1 16 Fasilkom UI v-1.1

The bind() Call The listen() call

Executed on the server Moves the socket from the CLOSED to the
Assign a (well-known) port address to the socket LISTEN state in the TCP state diagram – socket
Pertama: dibuat socket address is now ready to accept connections
int bind (int sockfd, const struct sockaddr
*myaddr, socklen_t addrlen) int listen (int sockfd, int backlog)

IP Address
Port Address

17 Fasilkom UI v-1.1 18 Fasilkom UI v-1.1

3
Server Initialization The connect() Call

Executes at Client
Establishes a connection with a server
Web Server int connect ( int sockfd,
1. socket()
const struct sockaddr
2. bind(80) *servaddr,
3. listen() socklen_t addrlen )

OS 80

Listen
queue

19 Fasilkom UI v-1.1 20 Fasilkom UI v-1.1

Connecting to the Server Busy Server Operation

Web Server Web Server


Client requests
1. socket() get queued-up in
Client 2. bind(80) the listen queue
3. listen() First-come first- OS 80
connect() served
Listen
queue
OS 80

Listen
Request
queue
from (IP, port)

Client 1 Client 2 Client 3


21 Fasilkom UI v-1.1 22 Fasilkom UI v-1.1

The accept() Call Summary: Socket API (1)

Creating a socket
int socket(int domain, int type, int protocol)
Web Server • domain = PF_INET, PF_UNIX
Connected • type = SOCK_STREAM, SOCK_DGRAM
Client requests socket accept()
get queued-up in
the listen queue OS 80 Passive Open (on server)
First-come first- Listen
int bind(int socket, struct sockaddr *addr, int
served queue addr_len)
int listen(int socket, int backlog)
int accept(int socket, struct sockaddr *addr, int
addr_len)
Client 1 Client 2 Client 3
23 Fasilkom UI v-1.1 24 Fasilkom UI v-1.1

4
Summary: Socket API (2) IP Addresses

Active Open (on client) 32-bit IP addresses are stored in an IP address


int connect(int socket, struct sockaddr *addr, struct
int addr_len) IP addresses are always stored in memory in
network byte order (big-endian byte order)
Sending/Receiving Messages True in general for any integer transferred in a
int send(int socket, char *msg, int mlen, int flags) packet header from one machine to another.
• E.g., the port number used to identify an Internet
int recv(int socket, char *buf, int blen, int flags) connection.

/* Internet address structure */


struct in_addr {
unsigned int s_addr; /* network byte order (big-endian) */
};

25 Fasilkom UI v-1.1 26 Fasilkom UI v-1.1

Domain Naming System (DNS) Socket Address Structures

The Internet maintains a mapping between IP addresses Generic socket address:


and domain names in a huge worldwide distributed For address arguments to connect, bind, and accept.
database called DNS. Necessary only because C did not have generic (void *)
pointers when the sockets interface was designed.
Conceptually, programmers can view the DNS database Internet-specific socket address:
as a collection of millions of host entry structures: Must cast (sockaddr_in *) to (sockaddr *) for connect,
/* DNS host entry structure */ bind, and accept.
struct hostent {
char *h_name; /* official domain name of host */ struct sockaddr {
char **h_aliases; /* null-terminated array of domain names */ unsigned short sa_family; /* protocol family */
int h_addrtype; /* host address type (AF_INET) */ char sa_data[14]; /* address data. */
int h_length; /* length of an address, in bytes */ };
char **h_addr_list; /* null-terminated array of in_addr structs */
}; struct sockaddr_in {
unsigned short sin_family; /* address family (always AF_INET) */
Functions for retrieving host entries from DNS: unsigned short sin_port; /* port num in network byte order */
gethostbyname: query key is a DNS domain name. struct in_addr sin_addr; /* IP addr in network byte order */
unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */
gethostbyaddr: query key is an IP address. };

27 Fasilkom UI v-1.1 28 Fasilkom UI v-1.1

For More Information

W. Richard Stevens, “Unix Network


Programming: Networking APIs: Sockets and
XTI”, Volume 1, Second Edition, Prentice Hall,
1998.
network programming bible.

29 Fasilkom UI v-1.1

You might also like