Title: TCPIP programming
1TCP/IP programming
- Unix TCP/IP socket programming
2Tele-Marketing (motivating example)
customer
store
- phone line
- information
- phone number
- extension
- Dial call
- talk
- hang-up
- phone line
- setup
- phone number
- extension
- call-waiting
- Wait for calls
- Answer calls
- talk
- hang-up
3Internet sockets terminology
- phone line
- phone number
- extension
- call waiting
- dial
- answer
- talk
- hang-up
- socket()
- IP address
- port number
- bind()
- listen()
- connect()
- accept()
- send() / recv()
- close ()
4Sockets architecture
application
user
kernel
socket
TCP/UDP
IP (network)
5The client-server model
request
Port 2356
Port 23
reply
telnet
Client 1
IP132.66.48.37
Port 20
ftp
Port 3325
Client 2
Server
IP132.66.48.12
IP132.66.2.1
6Internet Sockets
- File descriptor
- two (main) types
- TCP (SOCK_STREAM)
- UDP (SOCK_DGRAM)
- Structure
- Byte order
- compatibility (host vs. network)
- conversion routines
sa_family sa_data
sock_addr
sa_family port IP_addr zero
sock_addr_in
7SOCKET ( ) - Get the file descriptor
int socket (int domain, int type, int protocol)
- Domain what type of communication
- AF_INET
- Type semantics of communication
- SOCK_STREAM (TCP)
- SOCK_DGRAM (UDP)
- Protocol which protocol to use
- set to 0
- OUTPUT socket file descriptor.
- return -1 for error
8BIND ( ) - What port am I on
int bind ( int sockfd, struct sockaddr
myaddr, int addrlen)
- Associates a socket with a port on local machine.
- port numbers 1024-65535.
- Unspecified
- port (set port0) chooses an available port.
- IP (set IP address to INADDR_ANY) returns local
IP address. - OUTPUT none
- return -1 for error
9CONNECT ( ) - Hey, you!
int connect ( int sockfd, struct sockaddr
serv_addr, int addrlen)
- Associates a socket with a port on local machine.
- Not necessary to bind() before connect().
- OUTPUT none
- return -1 for error
10LISTEN ( ) - Will somebody please call me?
int listen( int sockfd, int backlog)
- backlog - number of connections allowed in
incoming queue. - OUTPUT none
- return -1 for error
11ACCEPT ( ) - Thank you for calling
int accept ( int sockfd, void addr, int
addrlen)
- returns a new file descriptor.
- The communication (send recv) uses the new file
descriptor. - sockfd can be used to accept more calls.
- OUTPUT file descriptor
- return -1 for error
12send ( ) and recv ( ) - Talk to me!
int recv ( int sockfd, void buf, int
len unsigned int flags)
int send ( int sockfd, const void msg,
int len int flags)
- sockfd is the one to read from.
- buf points to the buffer to read the data into,
whose maximum length is len. - returns number of bytes read.
- sockfd is where to send.
- msg points to the data, whose length is len.
- Returns number of bytes sent.
13close ( ) and shutdown ( ) - Done!
int close (int sockfd)
int shutdown (int sockfd, int how)
- prevents read and writes from sockfd.
- Any attempt to read/write results in an error.
- selects how to close sockfd.
- how has the following values
- 0 - disallow receives.
- 1- disallow sends.
- 2 - disallow receive send
14Summary
server
client
- socket()
- bind()
- listen()
- accept()
- send()/recv()
- close()
- socket()
- connect()
- send()/ recv()
- close()
15sendto ( ) and recvfrom ( ) - Talk to me UDP!
int recvfrom (int sockfd, void buf, int
len unsigned int flags const struct sockaddr
from int fromlen)
int sendto ( int sockfd, const void msg,
int len int flags const struct sockaddr
to int tolen)
- Combines the send and receive with IP address.
- No need for listen(), accept() and connect().
- unreliable communication.
16getpeername( ) - Who am I talking with?
int getpeername( int sockfd, struct sockaddr
addr, int addrlen)
Return the IP address of the other party to
sockfd.
17Blocking
- what is blocking
- perform both accept() and recv().
- non-blocking socket.
- CPU wastage.
- The poll command
- Allows to wait on a few events.
int poll(struct pollfd fds unsigned long
nfds int timeout)
18Poll command
- Pollfd structure
- file descriptor (int fd)
- requested events (short events)
- returned events (short events)
- Information
- returns only when one or more request events
occur. - Can have multiple events.
- flags are bits set.
- more information in "man poll"
- System view
- The execution waits for the events to occur.
- No wastage of CPU.
19Naming and addressing
- There is more then one level of addressing.
- Domain name (www.tau.ac.il)
- IP address (132.66.16.6)
- MAC address (ethernet card number)
- Why do we need different addresses.
- How can we look up an address.
20Domain Name System (DNS)
- A level of abstraction in naming.
- domain name www.amazon.com
- IP address 208.216.182.15
- struct hostent gethostbyname( const char name)
- name is the domain name.
- struct hostent
- h_name official host name
- h_alises alternate names
- h_addrtype usually AF_INET
- h_length length of address in bytes
- h_addr_list IP address
- h_addr first adress in h_addr_list
21How does DNS work?
- Old system one file that matched names to IP
addresses. - Current system
- call a resolver with name.
- resolver send a UDP packet to DNS server.
- DNS server either has the IP address locally
(/etc/hosts), or - fetches it from another DNS server.
- Name server - recursive query
- DNS name space
- hierarchical
- case insensitive
- alpha-numeric
mansour.tau.ac.il
edu-server.net
mit.edu
22Address Resolution Protocol (ARP)
- Host is connected to a LAN
- Ethernet address (48 bits)
- Maps an IP address to an Ethernet address
- Data link level communication uses Ethernet
address - Why a simple look-up table is not good enough.
23ARP
host name
host name
- FTP calls gethostbyname
- FTP asks TCP .
- TCP asks to IP.
- IP converts the IP address to a next hop address
(ARP). - ARP sends ARP request, a broadcast packet
- IP address
- Its IP address
- Its Ethernet number
- The destination checks the ARP request.
- The "right" host sends ARP reply.
resolver
ftp
IP address
TCP
IP
ARP
Ethernet driver
Ethernet driver
Ethernet driver
ARP
IP
ARP