Professional Documents
Culture Documents
Key Components:
Internet protocols
Sockets
IP is standard
allows a common namespace across most of Internet
reduces number of translations, which incur overhead
Sockets
CS/ECE 338
CS/ECE 338
Internet Protocols
Application
Layers
FTP
HTTP
NV
TFTP
Transport
Network
Transport
TCP
UDP
Data Link
Network
IP
Physical
Data Link
Ethernet
FDDI
ATM
Modem
Principles and
Concepts
Physical
CS/ECE 338
CS/ECE 338
Outline
Reading:
Client-Sever Model
TCP Connection
UDP Services
Addresses and Data
Sockets API
Example
Sockets API:
A transport layer service interface
Introduced in 1981 by BSD 4.1
Implemented as library and/or system calls
Similar interfaces to TCP and UDP
Can also serve as interface to IP (for super-user);
known as raw sockets
CS/ECE 338
Client-Server Model
Client
Asymmetric
Communication
Client
Server/Daemon
Server
Client
CS/ECE 338
CS/ECE 338
Examples
Web
FTP
Telnet
Client
Initiates contact
Waits for response
Robin Kravets & Steve Lumetta, UIUC Spring 2003
Client-Server Model
Client
Client
CS/ECE 338
Communication Link
Server
8
Client-Server Communication
Model
TCP Connections
Service Model
Concurrent:
Server processes multiple clients requests
simultaneously
Sequential:
Server processes only one clients requests at a time
Hybrid:
Server maintains multiple connections, but processes
responses sequentially
TCP Service
CS/ECE 338
10
3-Way Handshake
Sequence Numbers
Active open
Client
J,K
Message Types
Synchronize (SYN)
Acknowledge (ACK)
Passive Open
Client initiates
connection to server
11
CS/ECE 338
roniz
e
listen
(SYN
)J
K,
+1
SY N
CK) J
dge (A
owle
n
k
c
a
AC K
Active Open
Data Transmission
Server
Synch
K+1
UDP Services
Active close
Passive close
Client
Server
Finis
Finished (FIN)
Acknowledge (ACK)
ACK
)J
J+1
FIN K
Active Close
Sends no more data
AC K
Passive close
Accepts no more data
CS/ECE 338
hed (F
IN
K+1
13
CS/ECE 338
UDP Services
Unit of Transfer
Human readable
Variable length
Ex: sal.cs.uiuc.edu
Unreliable
No guaranteed delivery
Drops packets silently
IP addresses
Easily handled by routers/computers
Fixed length
Somewhat geographical
Ex: 128.174.252.217
Unordered
No guarantee of maintained order of delivery
Unlimited Transmission
No flow control
CS/ECE 338
14
15
CS/ECE 338
16
Byte Ordering
int m, n;
short int s,t;
m
s
n
t
17
=
=
=
=
ntohl
ntohs
htonl
htons
CS/ECE 338
(n)
(t)
(m)
(s)
net-to-host
net-to-host
host-to-net
host-to-net
18
Address Access/Conversion
Functions
struct in_addr {
in_addr_t s_addr;
};
/* 32-bit IP address */
/* e.g., AF_INET */
/* TCP/UDP port */
/* IP address */
CS/ECE 338
20
Address Access/Conversion
Functions
Sockets API
21
CS/ECE 338
Actoris
Input/Output I/O
I am Bothous
port 46.
Error Model
Talk to Bothous
port 46.
Return value
client
0 on success
-1 on failure
NULL on failure for routines returning pointers
I am accepting
connections.
I will speak to
Actoris port 87.
server
errno variable
CS/ECE 338
22
23
CS/ECE 338
24
Functions: socket
25
Function: bind
1-512
513-1023
1024-49151
49152-65535
CS/ECE 338
26
ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers
CS/ECE 338
typically 0
CS/ECE 338
27
CS/ECE 338
28
Functions: listen
Establishing a Connection
Include file <sys/socket.h>
CS/ECE 338
29
30
Functions: connect
Functions: accept
the caller passes the size of the address structure, the kernel
returns the size of the clients address (the number of bytes
written)
31
CS/ECE 338
32
CS/ECE 338
Functions: write
Functions: read
CS/ECE 338
35
34
CS/ECE 338
36
Functions: sendto
Functions: recvfrom
CS/ECE 338
37
CS/ECE 338
Functions: close
Close a socket.
38
Close a socket.
39
CS/ECE 338
40
10
Functions: shutdown
Advanced Sockets
41
Examples
CS/ECE 338
42
server
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define PORT 3490
/* well-known port */
#define BACKLOG 10
/* how many pending
connections queue
will hold */
43
CS/ECE 338
44
11
server
server
main()
{
int sockfd, new_fd;
CS/ECE 338
45
46
Spring 2003
server
server
CS/ECE 338
48
12
client
client
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 3490
/* well-known port */
#define MAXDATASIZE 100 /* max number of bytes we
can get at once */
CS/ECE 338
CS/ECE 338
49
Structure: hostent
struct hostent {
char* h_name;
/* official name of host */
char** h_aliases;
/* NULL-terminated alias list */
int h_addrtype
/* address type (AF_INET) */
int h_length;
/* length of addresses (4B) */
char** h_addr_list; /* NULL-terminated address list */
#define h_addr h_addr_list[0];/* backward-compatibility */
};
50
client
CS/ECE 338
51
CS/ECE 338
52
13
client
Summary
buf[numbytes] = \0;
printf (Received: %s, buf);
close (sockfd);
return 0;
TCP version
UDP version
CS/ECE 338
53
CS/ECE 338
54
client
server
client
server
socket
socket
socket
connect
Synch
roniz
e
AC K
socket
bind
listen
connect
listen
write
accept
(SYN
)J
K,
+1
SY N
CK) J
dge (A
owle
n
k
c
a
connect completes
bind
connection moved to
complete queue
read
write
read
K+1
close
close
connection added to
incomplete queue
accept
CS/ECE 338
55
CS/ECE 338
56
14
Framing
Goal
server
socket
socket
bind
sendto
Note:
recvfrom
sendto
Questions:
recvfrom
close
Hint
string storage in C and Pascal
format strings with printf
CS/ECE 338
57
CS/ECE 338
58
Framing Problem
Approach
Think about the problem for a minute or two
Introduce yourself to 2-3 people near you
(form groups of 3-4)
Discuss the problem and agree on a solution
Pick a representative
Well ask a couple groups to present their
answers
CS/ECE 338
59
15