02-Networking

OSI 7 layers Physical/data link layers (layer 1 & 2) Network layer – IP (layer 3) Transport layer – TCP UDP (layer 4) Application layer – RSH (layer 7) Socket examples
展开查看详情

1.CSS434 Networking Textbook Ch3 Professor: Munehiro Fukuda CSS434 Networking 1

2. Outline  OSI 7 layers  Physical/data link layers (layer 1 & 2)  Network layer – IP (layer 3)  Transport layer – TCP UDP (layer 4)  Application layer – RSH (layer 7)  Socket examples CSS434 Networking 2

3. OSI 7 Layers Site Site A B Application protocol 7 Application Application rsh, ftp, Telnet Presentation Presentation protocol Dealing with heterogeneity 6 Presentation And cryptography Session protocol Dialog control 5 Session Session (rarely supported) Transport protocol 4 Transport Transport UDP, TCP Network protocol 3 Network Network IP Data link protocol IEEE802.2 2 Data link Data link connection or connectionless Physical protocol Ethernet 1 Physical Physical Network CSS434 Networking 3

4. OSI Protocol Summary Layer Description Examples Application Protocols that are designed to meet the communication requirements of HTTP,FTP , SMTP, specific applications, often defining the interface to a service. CORBA IIOP Presentation Protocols at this level transmit data in a network representation that is Secure Sockets independent of the representations used in individual computers, which may (SSL),CORBA Data differ. Encryption is also performed in this layer, if required. Rep. Session At this level reliability and adaptation are performed, such as detection of failures and automatic recovery. Transport This is the lowest level at which messages (rather than packets) are handled. TCP, UDP Messages are addressed to communication ports attached to processes, Protocols in this layer may be connection-oriented or connectionless. Network Transfers data packets between computers in a specific network. In a WAN IP, ATM virtual or an internetwork this involves the generation of a route passing through circuits routers. In a single LAN no routing is required. Data link Responsible for transmission of packets between nodes that are directly Ethernet MAC, connected by a physical link. In a WAN transmission is between pairs of ATM cell transfer, routers or between routers and hosts. In a LAN it is between any pair of hosts. PPP Physical The circuits and hardware that drive the network. It transmits sequences of Ethernet base- band binary data by analogue signalling, using amplitude or frequency modulation signalling, ISDN of electrical signals (on cable circuits), light signals (on fibre optic circuits) or other electromagnetic signals (on radio and microwave circuits). CSS434 Networking 4

5. Physical/Data Link Layer Example: CSMA/CD and Token Ring  IEEE802.3: CSMA/CD (Carrier sense multiple access with collision detection) 1. Listening to the shared medium 1 listen 3. detect 2. Transmitting a data packet Ⅹ 2 transmit 3. Detecting collision on the medium 4. Deferring and retransmitting a packet in 2k–time base collision window  IEEE802.5: Token Ring 1. Receiving a free token from my (left) neighbor 1. Free token 2. Attaching a data packet to the token 3. Forwarding the token to my (right) neighbor 2. Attach 4. Detaching a packet from the token 4. Detach if it is addressed here 3. busy token CSS434 Networking 5

6. Network Layer Example: IP Transportation layer Datagram Class A fragmentation reassembly 0 Net # Host # Octet 1 Octet 2 – 4 0-127 (1,677,716) SDL SDL Class B IP packet ID and size IP packet ID and size 1 0 Net # Host # Destination IP address Destination IP address Octet 1 Octet 3 – 4 Octet 2 Source IP address Source IP address 128-191 (65,536) Class C 110 Net # Host# Data link layer Octet 1 Octet 2 – 3 Octet 4 128-191 (256) Best-effort deliver semantics Class D: for broadcasting CSS434 Networking 6

7. Transport Layer: Example1: UDP client server  User Datagram Protocol  Connectionless socket() Create a sock descriptor socket()  May be lost bind() Bind it to an IP address bind()  No FIFO order  Multicast feature recvfrom()  Unix datagram sendto()  Example: TFTP, rwho Blocks until data received recvfrom() sendto() CSS434 Networking 7

8. Transport Layer: Example2: TCP client server socket() Create a sock descriptor socket()  Transport Control Protocol  Connection-oriented Bind it to an IP address bind()  Reliable  FIFO order Declare this is connection-orientedliseten()  No Multicast feature  Unix stream socket Wait for a connection accept()  Example: ftp, http, rsh Connection established Blocks until connection established all major applications connect() write() read() read() wrte() CSS434 Networking 8

9. Application Layer Example: RSH Client Server shell inetd TCP connection request Command rshd rsh ls- l shell TCP connection Inherited all the way To a child Command ls -l CSS434 Networking 9

10. Summary of TCP/IP Layers Message Layers Application Messages (UDP) or Streams (TCP) Transport UDP or TCP packets Internet IP datagrams Network interface Network-specific frames Underlying network CSS434 Networking 10

11.Socket Programming: Socket.h #include <iostream> extern "C" { #include <sys/types.h> // for sockets #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> // for gethostbyname( ) #include <unistd.h> // for close( ) #include <string.h> // for bzero( ) } #define NULL_FD -1 #define MAXSIZE 20 class Socket { public: Socket( int ); ~Socket( ); int getClientSocket( char[] ); int getServerSocket( ); private: int port; int clientFd; int serverFd; }; CSS434 Networking 11

12. Socket Programming: Socke t.cpp (Client) #include "Socket.h" // Fill in the structure "sendSockAddr" with the server address. sockaddr_in sendSockAddr; Socket::Socket( int port ) bzero( (char*)&sendSockAddr, sizeof( sendSockAddr ) ); : port( port ), clientFd( NULL_FD ), sendSockAddr.sin_family = AF_INET; //Address Family Internet serverFd( NULL_FD ) { sendSockAddr.sin_addr.s_addr = } inet_addr( inet_ntoa( *(struct in_addr*)*host->h_addr_list ) ); sendSockAddr.sin_port = htons( port ); Socket::~Socket( ) { if ( clientFd != NULL_FD ) // Open a TCP socket (an Internet strem socket). close( clientFd ); if( ( clientFd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) { if ( serverFd != NULL_FD ) cerr << "Cannot open a client TCP socket." << endl; close( serverFd ); return NULL_FD; } } int Socket::getClientSocket( char ipName[] ) { // Connect to the server. while ( connect( clientFd, (sockaddr*)&sendSockAddr, // Get the host entry corresponding to ipName sizeof( sendSockAddr ) ) < 0 ); struct hostent* host = gethostbyname( ipName ); if( host == NULL ) { // Connected cerr << "Cannot find hostname." << endl; return clientFd; return NULL_FD; } } CSS434 Networking 12

13.Socket Programming: Socke t.cpp (Server) int Socket::getServerSocket( ) { if ( serverFd == NULL_FD ) { // Server not ready sockaddr_in acceptSockAddr; // Open a TCP socket (an internet stream socket). if( ( serverFd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) { cerr << "Cannot open a server TCP socket." << endl; return NULL_FD; } // Bind our local address so that the client can send to us bzero( (char*)&acceptSockAddr, sizeof( acceptSockAddr ) ); acceptSockAddr.sin_family = AF_INET; // Address Family Internet acceptSockAddr.sin_addr.s_addr = htonl( INADDR_ANY ); acceptSockAddr.sin_port = htons( port ); if( bind( serverFd, (sockaddr*)&acceptSockAddr, sizeof( acceptSockAddr ) ) < 0 ) { cerr << "Cannot bind the local address to the server socket." << endl; return NULL_FD; } listen( serverFd, 5 ); } // Read to accept new requests int newFd = NULL_FD; sockaddr_in newSockAddr; socklen_t newSockAddrSize = sizeof( newSockAddr ); if( ( newFd = accept( serverFd, (sockaddr*)&newSockAddr, &newSockAddrSize ) ) < 0 ) { cerr << "Cannot accept from another host." << endl; return NULL_FD; } return newFd; } CSS434 Networking 13

14.Socket Programming: Main #include "Socket.h" #define PORT 10000 // You are given a specific pot from the instructor int main( int argc, char *argv[] ) { Socket sock( PORT ); int fd; if ( argc == 1 ) { // I'm a server while ( true ) { if ( ( fd = sock.getServerSocket( ) ) == NULL_FD ) return -1; char recvMessage[MAXSIZE]; read( fd, recvMessage, MAXSIZE ); cout << recvMessage << endl; close( fd ); } } if ( argc == 2 ) { // I'm a client if ( ( fd = sock.getClientSocket( argv[1] ) ) == NULL_FD ) return -1; char sendMessage[MAXSIZE]; cin >> sendMessage; write( fd, sendMessage, MAXSIZE ); } return 0; } CSS434 Networking 14

15. Exercises (No turn-in) 1. Why do we need layered network protocols? 2. When implementing TCP with datagram, what do we hav e to take care of? 3. Compare UDP and TCP for the implementation of each of the following application-level or presentation-level pr otocols (textbook p142, Q3.7): 1. Telnet 2. FTP 3. Rwho, finger 4. HTTP 5. RPC or RMI CSS434 Networking 15