IT692 Lectures
IT692 Lectures
Internet
• The server does not need to know anything about the client
– even that it exists
• The client should always know something about the server
– at least where(address) it is located
Clients
user
Server
ports space
IP IP
6
Transmission Control Protocol
(TCP): An Analogy
TCP Telephone Call
• Guaranteed delivery
• Reliable – guarantee
• In-order delivery
delivery • Connection-oriented
• Byte stream – in-order • Setup connection followed by
delivery conversation
• Connection-oriented –
single socket per
connection
• Setup connection followed
by data transfer
Example TCP applications
Web, Email, Telnet
7
Network Addressing Analogy
Telephone Call Network Programming
Telephone No IP Address
Central Number Network No.
Exchange Host Number
Area Code
Students Clients
Internet Connections (TCP/IP)
• Address the machine on the network
– By IP address
• Address the process
– By the “port”-number
• The pair of IP-address + port – makes up a “socket-address”
Server
Client
Connection socket pair (port 80)
(128.2.194.242:3479, 208.216.181.15:80)
10
Using Ports to Identify Services
Server host 128.2.194.242
• The main distinction between regular file I/O and socket I/O is
how the application “opens” the socket descriptors.
Types of socket
• Two most common types:
– SOCK_STREAM: Stream sockets, which provide reliable, two-way,
connection-oriented communication streams. <Uses TCP>
– SOCK_DGRAM: Datagram sockets, which provide connectionless,
unreliable service, used for packet-by-packet
transfer of information. <Uses UDP>
• Other types like SOCK_RAW also exist.
– Beyond the scope of the present discussion.
Systems calls for using sockets
• socket()
• bind()
• connect()
• listen()
• accept()
• send() & recv()
• sendto() & recvfrom()
• close() & shutdown()
• getpeername()
• gethostname()
• gethostbyname()
connection-oriented : TCP Client-Server
Interaction
TCP Server
socket()
bind()
TCP Client
listen()
socket()
socket( )
bind( ) server
socket( ) listen( )
client bind( )
connect( ) TCP conn. request
accept( )
send( ) TCP ACK
recv( )
recv( )
send( )
close( ) close( )
controlled by
application process
process
developer socket
socket
controlled by TCP with
TCP with
operating buffers, internet
system buffers,
variables
variables
connectionless: UDP Client-Server
Interaction
UDP Server
socket()
bind()
UDP Client
recvfrom()
socket()
blocks until datagram
sendto() received from a client
data request
close()
#include <netinet/in.h>
20
The struct sockaddr
#include <netinet/in.h>
• The generic: • The Internet-specific:
struct sockaddr_in {
struct sockaddr {
u_short sa_family; short
char sa_data[14]; sin_family;
}; u_short sin_port;
struct in_addr
sin_addr;
char
sin_zero[8];
};
/* Internet address structure */
struct in_addr {
u_long s_addr;
};
– sin_family = AF_INET
21
– sin_port: port # (0-65535)
Structure Casts
– You will see a lot of ‘structure casts’
/*Server Side */
bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
accept(sockfd, (struct sockaddr *) &cli_addr, &clilen) ;
…
…
struct sockaddr_in serv_addr
/*Client Side Connect takes (struct sockaddr *) as its second argument */
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
Socket primitives
• SOCKET: int socket(int domain, int type,
int protocol);
– domain := AF_INET (IPv4 protocol)
– type := (SOCK_DGRAM or SOCK_STREAM )
– protocol := 0 (IPPROTO_UDP or IPPROTO_TCP)
– returned: socket descriptor (sockfd), -1 is an error