Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | paki-garrett |
View: | 19 times |
Download: | 0 times |
2: Application Layer 1
Chapter 2: Application layer
2.1 Principles of network applications
2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
2.6 P2P file sharing 2.7 VOIP 2.8 Socket
programming with TCP 2.9 Socket
programming with UDP
2.10 Building a Web server
2: Application Layer 2
Socket programming
Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API:
unreliable datagram (UDP) reliable, byte stream-oriented (TCP)
Goal: learn how to build client/server application that communicate using sockets
2: Application Layer 3
Socket-programming using TCP
Socket: an interface between application process and end-end-transport protocol (UCP or TCP)
Why socket?: A Layer seen by application, OS transparent
process
TCP withbuffers,
variables
socket
controlled byapplicationdeveloper
controlled byoperating
system
host orserver
process
TCP withbuffers,
variables
socket
host orserver
internet
2: Application Layer 4
Socket programming with TCPClient must contact server server process must first
be running server must have created
socket (door) that accepts client’s contact
Client contacts server by: creating client-local TCP
socket specifying IP address, port
number of server process When client creates socket:
client TCP establishes connection to server TCP
When contacted by client, server TCP creates new socket for server process to communicate with client allows server to talk
with multiple clients source port numbers
used to distinguish clients (more in Chap 3)
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
application viewpoint
2: Application Layer 5
Many Versions of Socket APIs
Unix socket (berkeley socket) Winsock MacTCP ….
We introduce Unix socket API here Can program under SUN OS, Linux, etc A good tutorial on socket programming: http://beej.us/guide/bgnet/
2: Application Layer 6
Unix Descriptor Table
Descriptor TableDescriptor Table
0
1
2
3
4
Data structure for file 0Data structure for file 0
Data structure for file 1Data structure for file 1
Data structure for file 2Data structure for file 2
2: Application Layer 7
Socket Descriptor Data Structure
Descriptor TableDescriptor Table
0
1
2
3
4
Family: AF_INETFamily: AF_INETService: SOCK_STREAMService: SOCK_STREAMLocal IP: 111.22.3.4Local IP: 111.22.3.4Remote IP: 123.45.6.78Remote IP: 123.45.6.78Local Port: 2249Local Port: 2249Remote Port: 3726Remote Port: 3726
Family: AF_INETFamily: AF_INETService: SOCK_STREAMService: SOCK_STREAMLocal IP: 111.22.3.4Local IP: 111.22.3.4Remote IP: 123.45.6.78Remote IP: 123.45.6.78Local Port: 2249Local Port: 2249Remote Port: 3726Remote Port: 3726
2: Application Layer 8
TCP Client/Server Socket Overview
socket()
bind()
listen()
accept()
send()
recv()
recv()
close()
socket()
TCP Client
connect()
send()
recv()
close()
connection establishment
data request
data reply
end-of-file notification
TCP Server
bind()
2: Application Layer 9
What is a Socket?
socket returns an integer (socket descriptor) sockfd < 0 indicates that an error occurred socket descriptors are similar to file descriptors
AF_INET: associates a socket with the Internet protocol family
SOCK_STREAM: selects the TCP protocol SOCK_DGRAM: selects the UDP protocol
int sockfd; /* socket descriptor */if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(“socket”); exit(1);}
2: Application Layer 10
Socket Structure (Client)struct sockaddr_in { short int sin_family; // Address family unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // all zero};
AF_INET
// Internet address (Network Byte Order)// (a structure for historical reasons)
struct in_addr { unsigned long s_addr; // that's a 32-bit long, or 4 bytes
};
… … Big-Endian (Network Byte Order)
101 103102
1A 2D 3C 4B
IP: 1A.2D.3C.4B100
2: Application Layer 11
Bind (Client)
int sockfd;
struct sockaddr_in local_addr;
local_addr.sin_family = AF_INET;
local_addr.sin_port = 0; // random assign a port
local_addr.sin_addr.s_addr = INADDR_ANY; // use my IP address
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
sockfd = socket(AF_INET, SOCK_STREAM, 0); // create an empty socket
bind(sockfd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr);
Localhostinfo
2: Application Layer 12
Remote Host Structure
hostent *hp;hp = gethostbyname(“mail.cs.ucf.edu”);
“132.170.108.1”
struct hostent { char *h_name; /* official name */ char **h_aliases; /* alias list */ int h_addrtype; /* address type */ int h_length; /* address length */ char **h_addr_list; /* address list */ }; #define h_addr h_addr_list[0] /* backward compatibility */
Longwood.cs.ucf.edu
struct sockaddr_in remote_addr;remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(80); // short, network byte order remote_addr.sin_addr = *((struct in_addr *)hp->h_addr);memset(&(remote_addr.sin_zero), '\0', 8); // zero the rest
mail.cs.ucf.edu
Remotehostinfo
2: Application Layer 13
Connect(), send(), recv() by Client
connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr);
Struct sockaddr sockaddr_in
After connecting to the remote sever….
char sendStr[100], recvStr[100];….send(sockfd, sendStr, strlen(sendStr), 0);…recvNumByte = recv(sockfd, recvStr, MaxDataSize, 0);close(sockfd);
Blocking call
Remote hostinfo
Local hostsocket
2: Application Layer 14
Partial Send() and recv()Due to multiple packets in transmission
#include <sys/types.h> #include <sys/socket.h> int sendall(int s, char *buf, int *len) {
int total = 0; // how many bytes we've sent int bytesleft = *len; // how many we have left to send int n; while(total < *len) {
n = send(s, buf+total, bytesleft, 0); if (n == -1) { break; } total += n; bytesleft -= n;
} *len = total; // return number actually sent here return n==-1?-1:0; // return -1 on failure, 0 on success }
2: Application Layer 15
Non-Blocking Call and Select()fcntl(sockfd, F_SETFL, O_NONBLOCK); Con: poll data, waste CPU time
fcntl(sockfd, F_SETFL, O_ASYNC); // set to asynchronous I/O
Set the socket to do asynchronous I/O. When data is ready to be recv()'d on the socket, the signal SIGIO will be raised.
With multiple sockets, use select() to determine which
socket gives the signal SIGIO.
2: Application Layer 16
Socket Programming in Server No need to connect() a remote host Need to listen() on specified port Accept() a connection request
Generate a new socket for one connection• Support multiple connections
listen(sockfd, backLog); // backLog is the number of connections in queuenew_fd = accept(sockfd, (struct sockaddr *)&remote_addr,
&sizeof(struct sockaddr_in))New socket discriptorFollowing commun. through this
2: Application Layer 17
Socket Programming in Server: fork() for multi-connection service
while(1) { // main accept() loop sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size);printf("server: got connection from %s\n", inet_ntoa(remote_addr.sin_addr)); if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener send(new_fd, "Hello, world!\n", 14, 0); close(new_fd); exit(0);
} close(new_fd); // parent doesn't need this
} See the following link for tuotrial on fork():http://www.erlenstar.demon.co.uk/unix/faq_2.html
2: Application Layer 18
Chapter 2: Application layer
2.1 Principles of network applications
2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
2.6 P2P file sharing 2.7 VOIP 2.8 Socket
programming with TCP 2.9 Socket
programming with UDP
2.10 Building a Web server
2: Application Layer 19
Socket programming with UDP
UDP: no “connection” between client and server
no handshaking sender explicitly attaches
IP address and port of destination to each packet
server must extract IP address, port of sender from received packet
UDP: transmitted data may be received out of order, or lost
application viewpoint
UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server
2: Application Layer 20
UDP Socket Programming
sockfd = socket(AF_INET, SOCK_DGRAM, 0)
No connect(), accept() Send() sendto(), recv() recvfrom()
Sendto() includes target address/port
SOCK_STREAM (tcp)
2: Application Layer 21
Chapter 2: Summary
Application architectures client-server P2P hybrid
application service requirements: reliability, bandwidth, delay
Internet transport service model connection-oriented, reliable:
TCP unreliable, datagrams: UDP
Our study of network apps now complete!
specific protocols: HTTP FTP SMTP, POP, IMAP DNS
socket programming
2: Application Layer 22
Chapter 2: Summary
typical request/reply message exchange: client requests info or
service server responds with
data, status code
message formats: headers: fields giving
info about data data: info being
communicated
Most importantly: learned about protocols
control vs. data msgs in-band, out-of-band
(ftp) centralized vs.
decentralized stateless vs. stateful reliable vs. unreliable msg
transfer “complexity at network
edge”