Quick Overview
description
Transcript of Quick Overview
![Page 1: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/1.jpg)
Quick OverviewQuick Overview
![Page 2: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/2.jpg)
22
ISO/OSI Reference ModelISO/OSI Reference Model
• ApplicationApplication
• PresentationPresentation
• SessionSession
• TransportTransport
• NetworkNetwork
• Data LinkData Link
• PhysicalPhysical
![Page 3: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/3.jpg)
33
TCP/IP ModelTCP/IP Model
• ApplicationApplication
• TransportTransport
• NetworkNetwork
• Data LinkData Link
• PhysicalPhysical
![Page 4: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/4.jpg)
Berkeley SocketsBerkeley Sockets
Based on a presentation originally by Josh Brock circa 1999.
![Page 5: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/5.jpg)
55
Server and ClientServer and Client
TCP/UDP
IP
Ethernet Adapter
Server
TCP/UDP
IP
Ethernet Adapter
Clients
Server and Client exchange messages over the network through a common Socket API
Socket API
hardware
kernel space
user spaceports
![Page 6: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/6.jpg)
66
UDP vs. TCPUDP vs. TCP
User Datagram Protocol (UDP)User Datagram Protocol (UDP)
– Unreliable, datagramUnreliable, datagram– Similar to communication via emailSimilar to communication via email
• Each message is an independent chunk of data.Each message is an independent chunk of data.
• Transmission Control Protocol (TCP)Transmission Control Protocol (TCP)– reliable, byte-streamreliable, byte-stream– similar to communication on the telephonesimilar to communication on the telephone
• No messages, just a continuous stream of bytes.No messages, just a continuous stream of bytes.
• Example applicationsExample applications– UDP: multimedia applicationsUDP: multimedia applications– TCP: Web, EMail, TelnetTCP: Web, EMail, Telnet
![Page 7: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/7.jpg)
77
• A socket is a file descriptor that lets an application read/write data from/to A socket is a file descriptor that lets an application read/write data from/to the networkthe network
• socketsocket returns an integer (socket descriptor) returns an integer (socket descriptor)– fd < 0 indicates that an error occurredfd < 0 indicates that an error occurred– socket descriptors are similar to file descriptorssocket descriptors are similar to file descriptors
• AF_INET: associates a socket with the Internet protocol familyAF_INET: associates a socket with the Internet protocol family• SOCK_STREAM: selects the TCP protocolSOCK_STREAM: selects the TCP protocol• SOCK_DGRAM: selects the UDP protocolSOCK_DGRAM: selects the UDP protocol
What is a Socket?What is a Socket?
int fd; /* socket descriptor */if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) }
perror(“socket”);exit(1);
}
![Page 8: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/8.jpg)
88
What is a Port? A Port What is a Port? A Port Number?Number?– Port numbers are used to identify Port numbers are used to identify
“entities” on a host“entities” on a host– Port numbers can bePort numbers can be
• well-known (port 0-1023)well-known (port 0-1023)
• dynamic or private (port 1024-65535)dynamic or private (port 1024-65535)– Servers/daemons usually use well-known Servers/daemons usually use well-known
portsports
• any client can identify the any client can identify the server/serviceserver/service
• HTTP = 80, FTP = 21, Telnet = 23, ...HTTP = 80, FTP = 21, Telnet = 23, ...
• /etc/service/etc/service defines well-known ports defines well-known ports– Clients usually use dynamic portsClients usually use dynamic ports
• assigned by the kernel at run timeassigned by the kernel at run time
TCP/UDP
IP
Ethernet Adapter
NTPdaemon
Web server
port 123 port 80
![Page 9: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/9.jpg)
99
TCP
IP
Ethernet Adapter
Web Server
TCP ServerTCP Server
Port 80
• What does a What does a serverserver need to need to do so that ado so that a client client can can connect to it?connect to it?
![Page 10: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/10.jpg)
1010
Socket I/O: socket()Socket I/O: socket()• Since web traffic uses TCP, the web server must create a socket of type Since web traffic uses TCP, the web server must create a socket of type
SOCK_STREAMSOCK_STREAM
int fd; /* socket descriptor */
if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {perror(“socket”);exit(1);
}
• socketsocket returns an integer ( returns an integer (socket descriptorsocket descriptor))– fdfd < 0 indicates that an error occurred< 0 indicates that an error occurred
• AF_INETAF_INET associates a socket with the Internet protocol family associates a socket with the Internet protocol family
• SOCK_STREAM SOCK_STREAM selects the TCP protocolselects the TCP protocol
![Page 11: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/11.jpg)
1111
#include <netinet/in.h>
/* Internet address structure */struct in_addr { u_long s_addr; /* 32-bit IPv4 address */}; /* network byte ordered */
/* Socket address, Internet style. */struct sockaddr_in {
u_char sin_family; /* Address Family */u_short sin_port; /* UDP or TCP Port# */
/* network byte ordered */struct in_addr sin_addr; /* Internet Address */char sin_zero[8]; /* unused */
};
Internet Addressing Data Internet Addressing Data StructureStructure
![Page 12: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/12.jpg)
1212
Byte OrderingByte Ordering
• Big EndianBig Endian– Sun Solaris, PowerPC, ...Sun Solaris, PowerPC, ...
• Little EndianLittle Endian– i386, alpha, ...i386, alpha, ...
• Network byte order = Big EndianNetwork byte order = Big Endian
128 2 194 95
union { u_int32_t addr; /* 4 bytes address */ char c[4];} un;
/* 128.2.194.95 */un.addr = 0x8002c25f;
/* c[0] = ? */
c[0] c[1] c[2] c[3]
95 194 2 128
![Page 13: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/13.jpg)
1313
Byte Ordering FunctionsByte Ordering Functions
• Converts between Converts between host byte order host byte order and and network byte network byte orderorder– ‘‘h’ = host byte orderh’ = host byte order– ‘‘n’ = network byte ordern’ = network byte order– ‘‘l’ = long (4 bytes), converts IP addressesl’ = long (4 bytes), converts IP addresses– ‘‘s’ = short (2 bytes), converts port numberss’ = short (2 bytes), converts port numbers
#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);unsigned short int htons(unsigned short int hostshort);unsigned long int ntohl(unsigned long int netlong);unsigned short int ntohs(unsigned short int netshort);
![Page 14: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/14.jpg)
1414
struct sockaddr_in srv;
srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);if(srv.sin_addr.s_addr == (in_addr_t) -1) {
fprintf(stderr, "inet_addr failed!\n"); exit(1);}
Converting a numerical address to a string:Converting a numerical address to a string:
Dealing with IP AddressesDealing with IP Addresses• IP Addresses are commonly written as strings IP Addresses are commonly written as strings
(“128.2.35.50”), but programs deal with IP addresses (“128.2.35.50”), but programs deal with IP addresses as integers.as integers.
struct sockaddr_in srv;char *t = inet_ntoa(srv.sin_addr);if(t == 0) {
fprintf(stderr, “inet_ntoa failed!\n”); exit(1);}
Converting strings to numerical address:
![Page 15: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/15.jpg)
1515
Socket I/O: bind()Socket I/O: bind()A A socketsocket can be bound to a can be bound to a portportint fd; /* socket descriptor */struct sockaddr_in srv; /* used by bind() */
/* create the socket */
srv.sin_family = AF_INET; /* use the Internet addr family */
srv.sin_port = htons(80); /* bind socket ‘fd’ to port 80*/
/* bind: a client may connect to any of my addresses */srv.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {perror("bind"); exit(1);
}
•Still not quite ready to communicate with a client...Still not quite ready to communicate with a client...
![Page 16: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/16.jpg)
1616
Socket I/O: listen()Socket I/O: listen()• listenlisten indicates that the server will accept a connection indicates that the server will accept a connection
int fd; /* socket descriptor */struct sockaddr_in srv; /* used by bind() */
/* 1) create the socket *//* 2) bind the socket to a port */
if(listen(fd, 5) < 0) {perror(“listen”);exit(1);
}
• Still not quite ready to communicate with a client...Still not quite ready to communicate with a client...
listen returns •0 to indicate success•-1 to indicate error
![Page 17: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/17.jpg)
1717
Socket I/O: accept()Socket I/O: accept()acceptaccept blocks waiting for a connection blocks waiting for a connection
int fd; /* socket descriptor */struct sockaddr_in srv; /* used by bind() */struct sockaddr_in cli; /* used by accept() */int newfd; /* returned by accept() */int cli_len = sizeof(cli); /* used by accept() */
/* 1) create the socket *//* 2) bind the socket to a port *//* 3) listen on the socket */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);if(newfd < 0) {
perror("accept"); exit(1);}
• acceptaccept returns a new socket ( returns a new socket (newfdnewfd) with the same ) with the same properties as the original socket (properties as the original socket (fdfd))
• newfdnewfd < 0 indicates that an error occurred < 0 indicates that an error occurred
![Page 18: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/18.jpg)
1818
Socket I/O: accept() continued...Socket I/O: accept() continued...struct sockaddr_in cli; /* used by accept() */int newfd; /* returned by accept() */int cli_len = sizeof(cli); /* used by accept() */
newfd = accept(fd, (struct sockaddr*) &cli, &cli_len);if(newfd < 0) {
perror("accept");exit(1);
}
• How does the server know which client it is?How does the server know which client it is?– cli.sin_addr.s_addrcli.sin_addr.s_addr contains the client’s contains the client’s IP addressIP address– cli.sin_portcli.sin_port contains the client’s contains the client’s port numberport number
• Now the server can exchange data with the client by using Now the server can exchange data with the client by using readread and and writewrite on the descriptor on the descriptor newfdnewfd..
• Why does Why does acceptaccept need to return a new descriptor? need to return a new descriptor?
![Page 19: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/19.jpg)
1919
TCP
IP
Ethernet Adapter
2 Web Clients
TCP ClientTCP Client
ports
• How does a How does a client client connect connect
• to a to a server server? ?
![Page 20: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/20.jpg)
2020
Socket I/O: connect()Socket I/O: connect()• connectconnect is used by the client instead of socket(), bind(), listen(), accept(). It allows a client to connect to is used by the client instead of socket(), bind(), listen(), accept(). It allows a client to connect to
a server...a server...
int fd; /* socket descriptor */struct sockaddr_in srv; /* used by connect() */
/* create the socket */
/* connect: use the Internet address family */srv.sin_family = AF_INET;
/* connect: socket ‘fd’ to port 80 */srv.sin_port = htons(80);
/* connect: connect to IP Address “128.2.35.50” */srv.sin_addr.s_addr = inet_addr(“128.2.35.50”);
if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) {perror(”connect"); exit(1);
}
![Page 21: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/21.jpg)
2121
Socket I/O: write()Socket I/O: write()
writewrite can be used with a socket can be used with a socket
int fd; /* socket descriptor */struct sockaddr_in srv; /* used by connect() */char buf[512]; /* used by write() */int nbytes; /* used by write() */
/* … */
/* Example: A client could “write” a request to a server */if((nbytes = write(fd, buf, sizeof(buf))) < 0) {
perror(“write”);exit(1);
}
![Page 22: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/22.jpg)
2222
Socket I/O: read()Socket I/O: read()• readread can be used with a socket can be used with a socket• readread blocksblocks waiting for data waiting for data
int fd; /* socket descriptor */struct sockaddr_in srv; /* used by bind() */struct sockaddr_in cli; /* used by accept() */int newfd; /* returned by accept() */int cli_len; /* used by accept() */char buf[512]; /* used by read() */int nbytes; /* used by read() */
/* … */
if((nbytes = read(newfd, buf, sizeof(buf))) < 0) {perror(“read”); exit(1);
}
![Page 23: Quick Overview](https://reader035.fdocuments.us/reader035/viewer/2022062323/56815af5550346895dc8afea/html5/thumbnails/23.jpg)
2323
Review: TCP Client-Server Review: TCP Client-Server InteractionInteraction
socket()
bind()
listen()
accept()
write()
read()
read()
TCP Server
close()
socket()
TCP Client
connect()
write()
read()
close()
connection establishment
data request
data reply
end-of-file notification
from UNIX Network Programming Volume 1, figure 4.1