Advanced Computer Networks: Lecture 4 - Dr. Amir Qayyum

43 16 0
Advanced Computer Networks: Lecture 4 - Dr. Amir Qayyum

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Advanced Computer Networks: Lecture 4. This lecture will cover the following: TCP connections; transmission control protocol, at osi transport layer; recall: each protocol provides service interface; per-process table of I/O channels; client-server connection; sending and receiving data;...

CS716 Advanced Computer Networks By Dr. Amir Qayyum Lecture No. 4 TCP Connections • Transmission Control Protocol, at OSI transport  layer • Recall: each protocol provides service interface Aspects of TCP Service • Transfers a stream of bytes (interpreted by  application) • Connection­oriented – set up connection before communicating – tear down connection when done • In­order delivery of data: if A sends M1  followed by M2 to B, B never receives M2  before M1 Aspects of TCP Service • Reliable – data delivered at most once – exactly once if no catastrophic  failures • Flow control – prevents senders from wasting  bandwidth – reduces global congestion problems Aspects of TCP Service • Full­duplex: send or receive data at  any time • 16­bit port space allows multiple  connections on a single host TCP Connections • TCP connection setup via 3­way handshake – J and K are sequence numbers for messages SYN J client SYN K ACK J+1 ACK K+1 server Hmmm …  RTT is  important! TCP Connections • TCP connection teardown (4 steps) (either client  or server can initiate connection teardown) active close FIN J ACK J+1 passive close closes connection client FIN K ACK K+1 server Hmmm …  Latency   matters! UDP Services • User Datagram Protocol, at OSI transport  layer • Thin layer over IP UDP ­ Aspects of Services • Unit of transfer is a datagram (variable  length packet) • Unreliable, drops packets silently • No ordering guarantees • No flow control • 16­bit port space (distinct from TCP ports)  allows multiple recipients on a single host 10 Welcome a Connection ­ accept() int accept (int sockfd, struct sockaddr* cliaddr, int*  addrlen); Accept a new connection (first one of the queue of pending  connections). Returns file descriptor or ­1. Also sets  errno sockfd : socket file descriptor (returned from socket ) cliaddr : IP address and port number of client (returned from  call) addrlen : length of address structure = pointer to int set to  sizeof (struct sockaddr_in) – addrlen is a value­result argument: the caller passes the size of  the address structure, the kernel returns the size of the client’s  address (the number of bytes written) 29 Sending and Receiving data 30 Send the Data ­ write() int write (int sockfd, char* buf, size_t nbytes); Write data to a stream (TCP) or “connected” datagram (UDP)  socket. Returns number of bytes written or ­1. Also sets errno  on failure sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to write • some reasons for failure or partial writes: – process received interrupt or signal – kernel resources unavailable (e.g., buffers) int send (int sockfd, char* buf, size_t nbytes , int flags); 31 Receive the Data ­ read() int read (int sockfd, char* buf, size_t nbytes); Read data from a stream (TCP) or “connected” datagram (UDP)  socket. Returns number of bytes read or ­1. Also sets errno on  failure. Returns 0 if socket closed sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read int recv (int sockfd, char* buf, size_t nbytes , int flags); 32 Send Data to Someone ­ sendto() int sendto (int sockfd, char* buf, size_t nbytes, int  flags, struct sockaddr* destaddr, int addrlen); Send a datagram to another UDP socket. Returns number of  bytes written or ­1. Also sets errno on failure sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read flags : see man page for details; typically use 0 destaddr : IP address and port number of destination socket addrlen : length of address structure = sizeof (struct  sockaddr_in) 33 Receive Data from Someone ­ recvfrom() int recvfrom (int sockfd, char* buf, size_t nbytes, int  flags, struct sockaddr* srcaddr, int* addrlen); Read a datagram from a UDP socket. Returns number of  bytes read (0 is valid) or ­1. Also sets errno on failure sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read flags : see man page for details; typically use 0 srcaddr : IP address and port number of sending socket  (returned from call) addrlen : length of address structure = pointer to int set to  sizeof (struct sockaddr_in) 34 Tearing Down a Connection 35 Good Bye ­ close() int close (int sockfd); Closes a socket and deletes descriptor from system tables.  Returns 0 on success, ­1 and sets errno on failure sockfd : socket file descriptor (returned from socket ) • Closes communication on socket in both directions. All  data sent before close are delivered to other side (although  this aspect can be overridden) • After close() , sockfd is not valid for reading or writing 36 Close in My Way ­ shutdown() int shutdown (int sockfd, int howto); Force termination of communication across a socket in one or  both directions. Returns 0 on success, ­1 and sets errno on  failure sockfd : socket file descriptor (returned from socket ) howto : – SHUT_RD to stop reading – SHUT_WR to stop writing – SHUT_RDWR to stop both • shutdown() overrides the usual rules regarding duplicated  sockets, in which TCP teardown does not occur until all  copies have closed the socket 37 Advanced Sockets • Managing multiple connections – fork()/exec(): multiple server processes – pthread_create(): multi­threaded server process – (no calls): event­based server process • Detecting data arrival – select() and poll() functions • Synchronous vs. asynchronous connections • Other socket options 38 Example of Use • Taken from Beej’s Guide to Network  Programming (see the course web page) • Client­server example using TCP • For each client – Server forks new process to handle connection – Sends “Hello, world” 39 TCP Connection socket bind socket connect client connect  completes listen SYN J SYN K ACK J+1 ACK K+1 connection added to  incomplete queue server connection moved  to complete queue accept 40 TCP Connection socket connect client write read close socket bind listen server accept read write close 41 UDP Connection socket socket sendto client bind server recvfrom sendto recvfrom close 42 Food for Thought Framing messages on a byte stream … ? • Problem – pass logical messages using a TCP connection – read() may return partial or multiple messages – how can receiver identify the end of a message? • Try to come up with two or three methods • Hints – string storage in C and Pascal – format strings with printf() 43 ... ftp://ftp.isi.edu/in­notes/iana/assignments/port­numbers • 1-5 12 standard services (see /etc/services); super­user  only • 51 3-1 023 registered and controlled, also used for  identity verification; super­user only • 10 2 4- 49151 registered services/ephemeral ports... e.g., mail.yahoo.com, www.vu.edu.pk  (FQDN) • IP addresses: easily handled by routers/computers – fixed length – tied (loosely) to geography • e.g., 128.93.0 .4? ? or 212.0.0.1 11 Endianness • Machines on Internet have different .. .Lecture? ?No.? ?4 TCP Connections • Transmission Control Protocol, at OSI transport  layer • Recall: each protocol provides service interface

Ngày đăng: 05/07/2022, 12:04

Mục lục

    Aspects of TCP Service

    UDP - Aspects of Services

    Basic UNIX Concepts – I/O

    Socket Creation and Setup

    Creating Sockets - socket()

    Naming and Identifying Sockets - bind()

    TCP and UDP Port Namespaces

    Waiting for Connections - listen()

    Contact the Peer - connect()

    Welcome a Connection - accept()

Tài liệu cùng người dùng

Tài liệu liên quan