TCP: Three-way handshake

Similar documents
Communication. Sockets (Haviland Ch. 10)

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

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

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

Socket Programming TCP UDP

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

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

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

Tutorial on Socket Programming

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Network Communication

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

CLIENT-SIDE PROGRAMMING

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

STUDY OF SOCKET PROGRAMMING

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

Unix Network Programming

Introduction to Socket Programming

Elementary TCP Sockets

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

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

CS 640: Computer Networking

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

EEC-484/584 Computer Networks

System Programming. Sockets

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

Group-A Assignment No. 6

Socket Programming for TCP and UDP

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Programming with TCP/IP. Ram Dantu

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

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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Network programming(i) Lenuta Alboaie

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

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

Introduction to Socket Programming

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

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

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

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. Overview of Socket API Network Programming Basics

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

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

CSE 333 SECTION 8. Sockets, Network Programming

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

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

Internet applications

Socket Programming(2/2)

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Lecture 7. Berkeley Socket Programming

CSE 333 Section 8 - Client-Side Networking

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

UNIT IV- SOCKETS Part A

CSE 333 SECTION 7. Client-Side Network Programming

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

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

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

Sockets 15H2. Inshik Song

CS321: Computer Networks Introduction to Application Layer

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

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

Computer Networks Prof. Ashok K. Agrawala

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?

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

CS321: Computer Networks Socket Programming

A. Basic Function Calls for Network Communications

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Communication Networks ( ) / Fall 2013 The Blavatnik School of Computer Science, Tel-Aviv University

Lecture 8. Berkeley Socket Programming

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

Network Socket Programming - 3 BUPT/QMUL

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

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

A Client-Server Exchange

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

Department of Computer Science

CSE 333 SECTION 6. Networking and sockets

Network Socket Programming - 3 BUPT/QMUL

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

What s an API? Do we need standardization?

CS 428/528 Computer Networks Lecture 01. Yan Wang

Client Server Computing

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

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

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

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

Network Software Implementations

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

CS 351 Week 15. Course Review

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Transcription:

Sockets in C 1

Sockets in C The slides by themselves will not be sufficient to learn how to write socket code. If you did not attend class, then you will want to review the relevant chapters in Kerrisk (ch 56, 59, 60, 61.1 63.1, 63.2)) The provided example code is also a good starting point. 2

TCP: Three-way handshake sequence number = J socket connect (blocks) client SYN J server sequence number = K socket,bind,listen accept(blocks) connect returns SYN K, ack J+1 ack K+1 accept returns 27

TCP Server socket() bind() listen() accept() block until connection from client read() write() Connection establishment (3-way handshake) data transfer TCP Client socket() connect() write() read() close() end-of-file notification close() 28

Connection-Oriented Server Create a socket: socket() Assign a name to a socket: bind() Establish a queue for connections: listen() Get a connection from the queue: accept() Client Create a socket: socket() Initiate a connection: connect() 29

Socket Types Two main categories of sockets UNIX domain: both processes on the same machine INET domain: processes on different machines Three main types of sockets: SOCK_STREAM: the one we will use SOCK_DGRAM: for connectionless sockets SOCK_RAW 30

try nslookup Addresses and Ports A socket pair is the two endpoints of the connection. An endpoint is identified by an IP address and a port. IPv4 addresses are 4 8-bit numbers: 128.100.31.198 = greywolf 128.100.31.203 = redwolf Ports because multiple processes can communicate with a single machine we need another identifier. 31

More on Ports www.iana.org Well-known ports: 0-1023 80 = http 22 = ssh 23 = telnet 21 = ftp 25 = smtp (mail) 194 = irc Registered ports: 1024-49151 2709 = supermon 26000 = quake 3724 = world of warcraft Dynamic (private) ports: 49152-65535 32

TCP Server socket() bind() listen() accept() Connection establishment (3-way handshake) block until connection from client data transfer TCP Client socket() connect() write() read() write() read() close() end-of-file notification close() 33

Server side int socket(int family, int type, int protocol); family specifies protocol family: PF_INET IPv4 PF_LOCAL Unix domain type SOCK_STREAM, SOCK_DGRAM, SOCK_RAW protocol set to 0 except for RAW sockets returns a socket descriptor 34

bind to a name int bind(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); sockfd returned by socket struct sockaddr_in { short sin_family; /*PF_INET */ u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; /*filling*/ }; sin_addr can be set to INADDR_ANY to communicate on any network interface. 35

Set up queue in kernel int listen(int sockfd, int backlog) after calling listen, a socket is ready to accept connections prepares a queue in the kernel where partially completed connections wait to be accepted. backlog is the maximum number of partially completed connections that the kernel should queue. 36

Complete the connection int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); blocks waiting for a connection (from the queue) returns a new descriptor which refers to the TCP connection with the client sockfd is the listening socket cliaddr is the address of the client reads and writes on the connection will use the socket returned by accept 37

Client side socket() same as server, to say how we are going to talk int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); the kernel will choose a dynamic port and source IP address. returns 0 on success and -1 on failure setting errno. initiates the three-way handshake. 38

Byte order Big-endian 91,329 = Little-endian 91,329 = A A+1 A+2 A+3 00 01 64 C1 A+3 A+2 A+1 A 00 01 64 C1 Intel is little-endian, and Sparc is big-endian 42

Network byte order To communicate between machines with unknown or different endian-ness we convert numbers to network byte order (big-endian) before we send them. There are functions provided to do this: unsigned long htonl(unsigned long) unsigned short htons(unsigned short) unsigned long ntohl(unsigned long) unsigned short ntohs(unsigned short) 43

Sending and Receiving Data read and write calls work on sockets, but sometimes we want more control ssize_t send(int fd, const void *buf, size_t len, int flags); works like write if flags==0 flags: MSG_OOB, MSG_DONTROUTE, MSG_DONTWAIT ssize_t recv(int fd, void *buf, size_t len, int flags); flags: MSG_OOB, MSG_WAITALL, MSG_PEEK 44