Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Similar documents
Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Oral. Total. Dated Sign (2) (5) (3) (2)

Elementary TCP Sockets

UNIT IV- SOCKETS Part A

Group-A Assignment No. 6

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Introduction to Client-Server Model

TCP: Three-way handshake

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

STUDY OF SOCKET PROGRAMMING

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

CS321: Computer Networks Introduction to Application Layer

Chapter 6. The Transport Layer. Transport Layer 3-1

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

CS321: Computer Networks Socket Programming

Introduction to Socket Programming

Socket Programming for TCP and UDP

Network Communication

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Programming with TCP/IP. Ram Dantu

Tutorial on Socket Programming

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Ports under 1024 are often considered special, and usually require special OS privileges to use.

CSE 124 Discussion Section Sockets Programming 10/10/17

Socket Programming TCP UDP

04 Elementary. Client/Server. CEN 463 Network Programming. Dr. Mostafa Hassan Dahshan. King Saud University

Introduction to Socket Programming

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

EEC-484/584 Computer Networks

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Networked Applications: Sockets. End System: Computer on the Net

CS 3516: Computer Networks

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

CSE 333 Section 8 - Client-Side Networking

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Computer Networks Prof. Ashok K. Agrawala

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

UNIX Network Programming. Overview of Socket API Network Programming Basics

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Unix Network Programming

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Sockets 15H2. Inshik Song

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

System Programming. Sockets

UNIX Sockets. COS 461 Precept 1

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

A. Basic Function Calls for Network Communications

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

Communication. Sockets (Haviland Ch. 10)

CS 640: Computer Networking

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Server-side Programming

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Transport Layer. The transport layer is responsible for the delivery of a message from one process to another. RSManiaol

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

Server-side Programming

What s an API? Do we need standardization?

IPv4 and ipv6 INTEROPERABILITY

CSE 333 SECTION 8. Sockets, Network Programming

Network programming(i) Lenuta Alboaie

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

LAB MANUAL ( INDEX )

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Socket Programming(2/2)

Transport Layer. Gursharan Singh Tatla. Upendra Sharma. 1

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

User Datagram Protocol

Application Programming Interfaces

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Introduction for SPI mapping Service Discovery Interoperability Testing. 20, Sep PWG Fumio Nagasaka

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

We will cover in this order: 2.1, 2.7, 2.5, 2.4, 2.2

Client Server Computing

Transcription:

Review Preview Algorithms and Issues in Client Software Design Client Architecture Identifying the Location of a Parsing an Address Argument Looking Up a Domain Name Looking Up a Well-Known Port by Name The TCP Algorithm Allocating a Socket by socket() system Call Choosing a Local Protocol Port Number Connecting a TCP Socket to a by connect() Call Closing a TCP connection Packet Exchange for TCP Connection Port Numbers Port Number and Concurrent TCP output Elementary Functions for TCP Socket The socket() function The connect() function The bind() function The listen() function The accept() function The function 1 2 Closing a TCP Connection Closing a TCP Connection To terminate TCP connection, need send and receive two segments between peers ( and ). 1. One application calls close (active close), and send a FIN segment.(usually ) 2. The other end TCP (receives the FIN segment) performs the passive close. Send FIN to application (that cause close socket) and send ACK to sender s TCP. 3. Sometimes later, application close socket, it causes its TCP send a FIN. 4. A pear received the final FIN send ACK to peer Close (active close) Read return 0 (passive close) close 3 4 Packet Exchange for TCP Connection Port Numbers socket(), connect() connect() return returns ACK K+1 ACK for reply socket(), bind(), listen() accept() Connection Established returns When a want to connect to a, the must identify the with a specific service request. TCP, UDP, SCTP defines a group of well known ports to identify well-known service. (ex FTP: 20 (data), 21 (control) All three transport layers (TCP, UDP, SCTP) use 16 bit port numbers. A does not need care about port number for itself. A use a unique ephemeral port number on the host provided by system. The IANA (Internet Assigned Numbers Authority) maintains a list of port number. ACK N+1 Connection Close 5 6 1

Port Numbers The Port numbers (0 ~ 2 16-1)are divided into three ranges. 0~1023 : controlled and assigned by IANA (RFC 1700) 1024 ~ 49151: not controlled by IANA but needed to be registered for specific. Ex) port 6000 ~ 6003 : for window XP. 49152 ~ 65535: Dynamic ephemeral ports A concurrent must be able to serve more than one at a time. A concurrent create a child to take care of each. TCP can distinguish each child process in site with IP, ephemeral port number and service port number. 7 8 Socket pair information (IP:port, IP: port) Listening socket 131.129.50.239 Connection request to 131.129.50.239 Port 21 (, : 1500) Listening socket (:1500 9 10 112.133.50.333 (, : 1500) Listening socket (112.133.50.333:1500 (, : 1500) Listening socket (:1501 (, 112.133.50.333: 1500) (:1500 (, : 1501) (:1500 11 12 2

TCP Output TCP Output Every TCP socket has a send buffer, the size of buffer can be changed with the SO_SNDBUF socket option. When application calls write, the kernel copies all the data from the application buffer into the socket send buffer. If there is no sufficient space in the socket buffer, the process change state to block state until enough space become available. The kernel will not return from the write until the entire data copy from application buffer to socket buffer. application TCP IP datalink Application buffer write() User process kernel Socket send buffer MSS sized TCP segment IPv4 or IPv6 packets 13 14 TCP Output TCP TCP socket() TCP sends the data to IP in MSS-sized (or smaller) chucks prepending its TCP header for each segment. IP prepends IP header and search routing table to the destination IP address and passes data to datalink layer socket() connect() Connection established by TCP three-way handshake bind() listen() accept() 15 16 The socket() Function int socket(int family, int type, int protocol); Return socket descriptor number if Ok, -1 error To create a socket, we call the socket function. The socket() function return a file descriptor that can be used in later function calls that operate on sockets. The family argument determines the nature of the communication. AF_INET : IPv4 Internet domain AF_INET6 : IPv9 Internet domain AF_LOCAL :Unix domain protocol AF_UNIX : Unix domain protocol AF_UNSPEC: the domain is not specified. The socket() Function int socket(int family, int type, int protocol); Return socket descriptor number if Ok, -1 error The type argument determines the type of the socket. SOCK_STREAM: stream socket (TCP) SOCK_DGRAM: datagram socket (UDP) SOCK_RAM: raw socket SOCK_SEQPACKET: sequined packet socket (SCTP) The protocol argument is usually 0, to select the default protocol type. When multiple protocols are supported for the same domain and socket type, we can use the protocol argument to select a particular protocol. 17 18 3

socket() Function int socket(int family, int type, int protocol); Return socket descriptor number if Ok, -1 error The protocol argument is usually 0, to select the default protocol type. When multiple protocols are supported for the same domain and socket type, we can use the protocol argument to select a particular protocol. IPPROTO_TCP: TCP transport protocol IPPROTO_UDP: UDP transport protocol IPPROTO_SCTP: SCTP transport protocol socket() Function Not all combinations of socket family and type are valid. AF_INET AF_INET6 AF_LOCAL AF_ROUTE AF_KEY SOCK_STREAM O O O SOCK_DGRAM O O O SOCK_SEQPACKET O O O SOCK_RAW O O O O 19 20 AF_ vs. PF_ AF_: Stand for address family Used in socket address structure PF_: Stand for protocol family Used to create a socket 21 The connect() Function #include <sys/types.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); The connect function is used by a TCP to establish a connection with a TCP. Since the s kernel choose both an ephemeral port number and source IP address, the socket does not need bind before connect For a TCP socket, the connect() initiates TCP s three-way handshake. sockfd is a socket descriptor returned by socket() servaddr is a pointer to socket address structure contains the IP address and port number addrlen is size of socket address connect() returns only when the connection is established or an error occurs. Return 0 if OK, -1 on error 22 The connect() Function The connect() Function socket(), connect() connect returns socket() bind() listen() (Passive Mode) accept() Different errors for connect() The TCP receive no response to SYN, ETIMEOUT is returned. Some system (4.4BSD) send one SYN, and another 6 second later, and 24 second later. If no response is received, the error returned. If the s response to the s SYN is a RST (reset sent by other side of kernel), this indicate that no process is waiting for connection on the at the port specified (hard error). If the SYN cannot reach to destination (soft error), intermediate router send ICMP. The kernel save the message and keep sending SYN. If it is not able to reach after fixed amount of time, return EHOSTUNREACH or ENETUNREACH. 23 24 4

int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Return 0 if OK, -l on error int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Return 0 if OK, -l on error The bind() function assigns a local protocol address to a socket. The bind() function is usually used in a The local address is the combination of either 32bit (IPv4) or 128 bits (IPv6) IP address, along with a 16 bit port number. sockfd is a socket descriptor returned by socket() myaddr is a pointer to a protocol-specific socket address structure. addrlen is size of socket address With TCP, calling bind() lets us specify a port number and IP address or neither. s bind their well-known port number when they start. But, if a TCP or does not specify a port number, kernel choose an ephemeral port number for the socket when either connect or listen is called. It is unusual for a. A process can bind a specific IP to its socket. The IP address must belong to an interface on the host. For a TCP, this IP assigns the source IP address that will be used for IP datagram sent on the socket. For a TCP, this restricts the socket to receive incoming connections destined only to that IP address. 25 26 int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Return 0 if OK, -l on error Normally, a TCP does not bind and IP address to its socket. Instead, the kernel chooses the source IP address when the socket is connected, based on the outgoing interface that is used, which intern is based on the route required to reach the. If a TCP does not bind an IP address to its socket, the kernel use the destination IP address of the s SYN as the s source IP address. Process Specified IP address Port number Wild Card 0 Wild Card Nonzero 0 Nonzero Result Kernel choose IP and Port Kernel choose IP and process specifies Port Process specifies IP and kernel choose Port Process specifies IP and Port If we specify a port number of 0 kernel chooses an ephemeral port when bind() is called. But if we specify a wild card IP address, the kernel does not choose the local IP address until either the socket is connected (TCP) or a data is sent on the socket(udp). 27 28 Process Specified IP address Port number Wildcard 0 Wildcard Nonzero 0 Nonzero Result Kernel choose IP and Port Kernel choose IP and process specifies Port Process specifies IP and kernel choose Port Process specifies IP and Port Process Specified IP address Port number Wildcard 0 Wildcard Nonzero 0 Nonzero Result Kernel choose IP and Port Kernel choose IP and process specifies Port Process specifies IP and kernel choose Port Process specifies IP and Port With IPv4 (32 bit), the wildcard is specified by the constant INADDR_ANY by kernel With IPv6 (128 bit), the wildcard is specified by the constant INADDR_ANY by kernel struct sockaddr_in servaddr /*clear struct sokaddr_in for */ bzero(&servaddr, sizeof(servaddr)); /* allow s to connect using any one of the host's IP addresses */ servaddr.sin_addr.s_addr = htonl(inaddr_any); struct sockaddr_in6 servaddr /*clear struct sokaddr_in for */ bzero(&servaddr, sizeof(servaddr)); /* allow s to connect using any one of the host's IP addresses */ servaddr.sin_addr.s_addr = in6addr_any; 29 30 5

#define SA struct sockaddr /* generic socket address */ int listenfd; /* socket scriptor numberr*/ struct sockaddr_in servaddr; /* for address */...... /*create a socket for using IPv4 with TCP */ listenfd = socket(af_inet, SOCK_STREAM, 0); /*clear struct sokaddr_in for */ bzero(&servaddr, sizeof(servaddr)); /* use IPv4 */ servaddr.sin_family = AF_INET; /* allow s to connect using any one of the host's IP addresses */ servaddr.sin_addr.s_addr = htonl(inaddr_any); /* set port as 1313. htons function format port number to short*/ servaddr.sin_port = htons(1313); /* bind a space for request */ bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); int listen(int sockfd, int backlog); Return 0 if OK, -1 on error The listen function is called only by a TCP and it performs two actions. The listen() function converts an unconnected socket into a passive socket (prepared by ), indicating that the kernel should accept incoming connection requests directed to this socket. The second argument specifies the maximum number of connections the kernel should queue for this socket. 31 32 int listen(int sockfd, int backlog); Return 0 if OK, -1 on error connect() called The kernel maintain two queues for a given listening socket. An incomplete connection queue: contains an entry for each SYN that has arrived from a for which the is waiting completion of the TCP three-way handshake. A complete connection queue: contains an entry for each with whom the TCP three-way handshake has completed. connect returns Create entry on incomplete queue Entry moved from incomplete queue to completed queue, accept can return 33 34 The accept() Function When SYN J arrive from a, TCP kernel create a new entry on the incomplete queue. And then send ACK J+1 and SYN K. When ACK K+1 arrive from the (three-way handshake complete), the entry from incomplete queue to complete queue. When the process calls accept(), the first entry on the completed queue is returned to the process or if queue is empty, the process is put to sleep until an entry is placed onto the complete queue. int accept(int sockfd, const struct sockaddr *cliaddr, socklen_t *addrlen); Return non-negative descriptor if OK, -1 on error The accept is called by a TCP to return the next complete connection from the front of the completed connection queue. If the queue is empty, the process is put to sleep (block state). If accept() is successful, it returns a new scripter. This scripter refers to the TCP connection with the. 35 36 6

The Function #include <unistd.h> int close(int sockdes); /* return 0 for OK, -1 for error */ Probably one of the easiest things to do with a socket, is close it. This is done using closes the socket descriptor indicated by sockets. 37 7