Communication. Sockets (Haviland Ch. 10)

Similar documents
TCP: Three-way handshake

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

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

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

Socket Programming TCP UDP

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Network Communication

CLIENT-SIDE PROGRAMMING

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

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

Tutorial on Socket Programming

Introduction to Socket Programming

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

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

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Elementary TCP Sockets

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

Unix Network Programming

CSE 333 SECTION 8. Sockets, Network Programming

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

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

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

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

Socket Programming(2/2)

Group-A Assignment No. 6

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

CS 640: Computer Networking

Network programming(i) Lenuta Alboaie

Programming with TCP/IP. Ram Dantu

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

STUDY OF SOCKET PROGRAMMING

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

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

System Programming. Sockets

Socket Programming for TCP and UDP

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

UNIT IV- SOCKETS Part A

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

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

Introduction to Socket Programming

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

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

CS321: Computer Networks Introduction to Application Layer

EEC-484/584 Computer Networks

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

CSE 333 SECTION 7. Client-Side Network Programming

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

CSE 333 Section 8 - Client-Side Networking

CS321: Computer Networks Socket Programming

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

Sockets 15H2. Inshik Song

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)

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

Lecture 7. Berkeley Socket Programming

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

CS 3516: Computer Networks

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

Internet applications

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

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

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

Computer Networks Prof. Ashok K. Agrawala

Client software design

Department of Computer Science

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

A. Basic Function Calls for Network Communications

Sockets Sockets Communication domains

CS4700/CS5700 Fundamentals of Computer Networking

Lab 0. Yvan Petillot. Networks - Lab 0 1

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

A Client-Server Exchange

CSE 333 SECTION 6. Networking and sockets

Network Socket Programming - 3 BUPT/QMUL

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

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Network Socket Programming - 3 BUPT/QMUL

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

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

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

ECE 435 Network Engineering Lecture 2

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

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

ICT 6544 Distributed Systems Lecture 5

Lecture 8. Berkeley Socket Programming

Application Programming Interfaces

Socket Programming 2007/03/28

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

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

Transcription:

Communication Sockets (Haviland Ch. 10) 1

Simple Web Request 5LFKDUG V+RPH3DJH &RXUVHV 5HVHDUFK 2

How do we find the server? Every computer on the Internet has an Internet address. Called an IP address (Internet Protocol) An IP address is four 8-bit numbers separated by dots. www.eecg.toronto.edu = 128.100.10.235 3

Domain Name Servers ZZZHHFJ XWRURQWRFD" ZZZHHFJ XWRURQWRFD" EURZVHU URRW QDPHVHUYHU ZZZHHFJ XWRURQWRFD" FD VHUYHU ORFDO QDPHVHUYHU XWRURQWR VHUYHU ³FD QDPHVHUYHU ³HHFJ QDPHVHUYHU HHFJ ZZZHHFJ VHUYHU XWRURQWRFD" ³XWRURQWR QDPHVHUYHU ZZZHHFJ XWRURQWRFD" 4

This is getting complicated! EURZVHU VHUYHU ORFDO QDPHVHUYHU ³HHFJ QDPHVHUYHU 1XPEHURI PHVVDJHV" URRW QDPHVHUYHU ³FD QDPHVHUYHU ³XWRURQWR QDPHVHUYHU 5

Protocols,QYRLFH &XVWRPHU-RKQ'RH 2UGHU1R 4W\8QLW3ULFH7RWDO $WKDORQ 0% 6XEWRWDO 7D[ 727$/ -RKQ'RH)HE 3D\DEOHWR&386DUHXV )LYHKXQGUHGQLQHW\VL[ BBBBBBBBBBBB -RKQ'RH &386DUHXV -RKQ'RH 'HSWRI&RPSXWHU6FLHQFH 8QLYHUVLW\RI7RURQWR &386DUHXV &ROOHJH6WUHHW 7RURQWR2QWDULR06* We deliver! Courier 6

TCP/IP Transmission Control Protocol. Tells us how to package up the data. VRXUFHDGGUHVV GHVWDGGUHVV E\WHV DFN SRUW GDWD 7

TCP Connection +L &RQQHFWLRQSRUW" +L /HW VWDON ZD\ KDQGVKDNH 6<1 RND\ 6HQGPHDILOH *RWLW DFN +HUH VVRPHGDWD +HUH VVRPHPRUH *RWLW DFN, PGRQH ILQ, PGRQHWRR ILQ 8

Routing GLVWRURQWR IHEHOOQH[[LDQHW +6(7RURQWR SSSV\PSDWLFRFD EURZVHU GLVWRURQWR SRVEHOOQH[[LDQHW RQHWJZGLVWRURQWR EHOOQH[[LDQHW XWRUJZERUGHULI RQHWRQFD VIEEXSJZ XWRURQWRFD VHUYHU 9

Putting it together GLVWRURQWR IHEHOOQH[[LDQHW EURZVHU +6(7RURQWR SSSV\PSDWLFRFD GLVWRURQWR SRVEHOOQH[[LDQHW RQHWJZGLVWRURQWR EHOOQH[[LDQHW XWRUJZERUGHULI RQHWRQFD ORFDO QDPHVHUYHU ³HHFJ QDPHVHUYHU VIEEXSJZ XWRURQWRFD URRW QDPHVHUYHU ³FD QDPHVHUYHU ³XWRURQWR QDPHVHUYHU VHUYHU 10

How many messages? It depends on the size of the web page we retrieve. If the web page is 75 Kbytes (small!) it will be broken up into 103 IP packets. Remember DNS took 10 messages [KRSV PHVVDJHV 11

The Big Picture Client Process network Server Process Client-Server model: a client process wants to talk to a server process Client must find server - DNS lookup Client must find process on server - ports Finally establish a connection so two processes can talk 12

Sockets One form of communication between processes. Similar to pipes, except sockets can be used between processes on different machines. Use file descriptors to refer to sockets. Built on top of TCP layer 13

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 14

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() 15

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() 16

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 17

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.200 = werewolf 128.100.31.201 = seawolf 128.100.31.202 = skywolf Ports because multiple processes can communicate with a single machine we need another identifier. 18

More on Ports www.iana.org Well-known ports: 0-1023 80 = web 22 = ssh 23 = telnet 21 = ftp 25 = smtp (mail) 194 = irc Registered ports: 1024-49151 2709 = supermon 26000 = quake Dynamic (private) ports: 49152-65535 You should pick ports in this range to avoid overlap 19

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

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 21

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; /*AF_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 with any host 22

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. 23

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 24

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. 25

int soc; struct hostent *hp; struct sockaddr_in peer; inetclient.c most error checking is omitted in this example peer.sin_family = AF_INET; peer.sin_port = htons(port); /* fill in peer address */ hp = gethostbyname(argv[1]); peer.sin_addr = *((struct in_addr *)hp->h_addr); /* create socket */ soc = socket(pf_inet, SOCK_STREAM, 0); /* request connection to server */ if (connect(soc, (struct sockaddr *)&peer, sizeof(peer)) == -1) { perror("client:connect"); close(soc); exit(1); } write(soc, "Hello Internet\n", 16); read(soc, buf, sizeof(buf)); printf("server SAID: %s\n", buf); close(soc); 26

inetserver.c struct sockaddr_in peer; struct sockaddr_in self; int soc, ns, k; int peer_len = sizeof(peer); full code for this example is on the webpage self.sin_family = AF_INET; self.sin_port = htons(port); self.sin_addr.s_addr = INADDR_ANY; bzero(&(self.sin_zero), 8); peer.sin_family = AF_INET; /* set up listening socket soc */ soc = socket(pf_inet, SOCK_STREAM, 0); if (soc < 0) { perror("server:socket"); exit(1); } if (bind(soc, (struct sockaddr *)&self, sizeof(self))== -1){ perror("server:bind"); close(soc); exit(1); } listen(soc, 1); 27...

inetserver.c (concluded) /*... repeated from previous slide... soc = socket(pf_inet, SOCK_STREAM, 0); bind(soc, (struct sockaddr *)&self, sizeof(self))== -1){ perror("server:bind"); close(soc); exit(1); } listen(soc, 1);... and now continuing... */ /* accept connection request */ ns = accept(soc, (struct sockaddr *)&peer, &peer_len); if (ns < 0) { perror("server:accept"); close(soc); exit(1); } /* data transfer on connected socket ns */ k = read(ns, buf, sizeof(buf)); printf("server RECEIVED: %s\n", buf); write(ns, buf, k); close(ns); close(soc); 28

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 29

Network byte order To communicate between machines with unknown or different endian-ness we convert numbers to network byte order (bigendian) 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) 30

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 31