UNIX Sockets. COS 461 Precept 1

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

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

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

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

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

Computer Networks Prof. Ashok K. Agrawala

Tutorial on Socket Programming

Socket Programming for TCP and UDP

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

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

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

Socket Programming TCP UDP

Network Communication

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

CSE 333 Section 8 - Client-Side Networking

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

Sockets 15H2. Inshik Song

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

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

Elementary TCP Sockets

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

What s an API? Do we need standardization?

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

STUDY OF SOCKET PROGRAMMING

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

Basics. Once socket is configured, applica6ons. Socket is an interface between applica6on and network

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

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

Unix Network Programming

Computer Network Programming

Network Software Implementations

EEC-484/584 Computer Networks

CSE 124 Discussion Section Sockets Programming 10/10/17

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

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

Group-A Assignment No. 6

A Client-Server Exchange


UNIX System Programming Lecture 19: IP Sockets

CS 640: Computer Networking

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

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

Programming with TCP/IP. Ram Dantu

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

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

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

Introduction to Socket Programming

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

CS321: Computer Networks Introduction to Application Layer

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

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

TCP: Three-way handshake

Introduction to Computer Networks

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

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

CLIENT-SIDE PROGRAMMING

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS321: Computer Networks Socket Programming

UNIX Network Programming. Overview of Socket API Network Programming Basics

Transport Layer (TCP/UDP)

UNIT IV- SOCKETS Part A

Department of Computer Science

The User Datagram Protocol

CSE 333 SECTION 8. Sockets, Network Programming

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

Socket Programming 2007/03/28

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

CS 3516: Computer Networks

Client Server Computing

Socket Programming(2/2)

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

Application Programming Interfaces

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

Internet applications

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

CS 351 Week 15. Course Review

CS 428/528 Computer Networks Lecture 01. Yan Wang

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Topics for this Week

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

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Introduction to Socket Programming

Network Socket Programming - 3 BUPT/QMUL

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

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

UDP CONNECT TO A SERVER

Client-side Networking

Network Socket Programming - 3 BUPT/QMUL

Transcription:

UNIX Sockets COS 461 Precept 1

Socket and Process Communica;on application layer User Process Socket transport layer (TCP/UDP) OS network stack network layer (IP) link layer (e.g. ethernet) Internet Internet Internet application layer User Process Socket transport layer (TCP/UDP) OS network stack network layer (IP) link layer (e.g. ethernet) The interface that the OS provides to its networking subsystem 2

Delivering the Data: Division of Labor Network Deliver data packet to the des;na;on host Based on the des;na;on IP address Opera;ng system Deliver data to the des;na;on socket Based on the des;na;on port number (e.g., 80) Applica;on Read data from and write data to the socket Interpret the data (e.g., render a Web page) 3

Socket: End Point of Communica;on Sending message from one process to another Message must traverse the underlying network Process sends and receives through a "socket" In essence, the doorway leading in/out of the house Socket as an Applica;on Programming Interface Supports the crea;on of network applica;ons User process User process socket Opera;ng System socket Opera;ng System 4

Two Types of Applica;on Processes Communica;on Datagram Socket (UDP) Collec;on of messages Best effort Connec;onless Stream Socket (TCP) Stream of bytes Reliable Connec;on- oriented 5

User Datagram Protocol (UDP): Datagram Socket UDP Single socket to receive messages No guarantee of delivery Not necessarily in- order delivery Datagram independent packets Must address each packet Postal Mail Single mailbox to receive messages Unreliable J Not necessarily in- order delivery Each LeYers leyer sent independently is independent Must address each reply Single mailbox to receive leyers Not necessarily in- order delivery Must address each mail Example UDP applica;ons Mul;media, voice over IP (Skype) 6

Transmission Control Protocol (TCP): Stream Socket TCP Reliable guarantee delivery Byte stream in- order delivery Connec;on- oriented single socket per connec;on Setup connec;on followed by data transfer Telephone Postal Mail Call Single Guaranteed mailbox delivery to receive messages Unreliable In- order delivery J Not necessarily in- order Connec;on- oriented delivery Each leyer is independent Must address each reply Setup connec;on followed by conversa;on Example TCP applica;ons Web, Email, Telnet 7

Socket Iden;fica;on Receiving host Des;na;on address that uniquely iden;fies host Process" A" Process" B" IP address: 32- bit quan;ty port X! port Y! Port " Number Receiving socket Host may be running many different processes Des;na;on port that uniquely iden;fies socket Port number: 16- bits TCP/UDP" IP" Ethernet Adapter" Protocol Host " Address 8

Client- Server Communica;on Client "some;mes on" Ini;ates a request to the server when interested E.g., Web browser on your laptop or cell phone Doesn't communicate directly with other clients Needs to know server's address Server is "always on" Handles services requests from many client hosts E.g., Web server for the www.cnn.com Web site Doesn't ini;ate contact with the clients Needs fixed, known address 9

Knowing What Port Number To Use Popular applica;ons have well- known ports E.g., port 80 for Web and port 25 for e- mail See hyp://www.iana.org/assignments/port- numbers Well- known vs. ephemeral ports Server has a well- known port (e.g., port 80) Between 0 and 1023 (requires root to use) Client picks an unused ephemeral (i.e., temporary) port Between 1024 and 65535 "5 tuple" uniquely iden;fies traffic between hosts Two IP addresses and two port numbers + underlying transport protocol (e.g., TCP or UDP) 10

Using Ports to Iden;fy Services Client host" Client" Service request for" 128.2.194.242:80" (i.e., the Web server)" Server host 128.2.194.242" OS" Web server" (port 80)" Echo server" (port 7)" Client" Service request for" 128.2.194.242:7" (i.e., the echo server)" OS" Web server" (port 80)" Echo server" (port 7)" 11

UNIX Socket API In UNIX, everything is like a file All input is like reading a file All output is like wri;ng a file File is represented by an integer file descriptor API implemented as system calls E.g., connect, send, recv, close, 12

Client- Server Communica;on Stream Sockets (TCP): Connec;on- oriented Server socket() Create a socket bind() Bind the socket (what port am I on?) Client listen() accept() recv() Listen for client (Wait for incoming connec;ons) Accept connec;on Receive Request establish connec;on data (request) Create a socket Connect to server Send the request socket() connect() send() send() Send response data (reply) Receive response recv() 13

Client- Server Communica;on Datagram Sockets (UDP): Connec;onless socket() Server Create a socket Client Create a socket socket() bind() Bind the socket Bind the socket bind() recvfrom() Receive Request data (request) Send the request sendto() sendto() Send response data (reply) Receive response recvfrom() 14

Client: Learning Server Address/Port Server typically known by name and service E.g., "www.cnn.com" and "hyp" Need to translate into IP address and port # E.g., "64.236.16.20" and "80" Get address info with given host name and service int getaddrinfo(!char *node,!!!!!!!!!char *service,!!struct addrinfo *hints,!!struct addrinfo **result)! *node: host name (e.g., "www.cnn.com") or IP address *service: port number or service listed in /etc/services (e.g. lp) hints: points to a struct addrinfo with known informa;on 15

Client: Learning Server Address/Port (cont.) Data structure to host address informa;on struct addrinfo {!!!int!!!!!ai_flags;!!!!!!int!!!!!ai_family;!//e.g. AF_INET for IPv4!!!int!!!!!ai_socketype;!//e.g. SOCK_STREAM for TCP!!!int!!!!!ai_protocol;!//e.g. IPPROTO_TCP!!!size_t!!!!ai_addrlen;!!!!!char!!!!*ai_canonname;!!!struct!sockaddr!*ai_addr;!// point to sockaddr struct!!!struct!addrinfo!*ai_next;! }! Example hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6! hints.ai_socktype = SOCK_STREAM; // TCP stream sockets! int status = getaddrinfo("www.cnn.com", "80", &hints, &result);! // result now points to a linked list of 1 or more addrinfos! // etc.! 16

Client: Crea;ng a Socket Crea;ng a socket int socket(int domain, int type, int protocol)! Returns a file descriptor (or handle) for the socket Domain: protocol family PF_INET for IPv4 PF_INET6 for IPv6 Type: seman;cs of the communica;on SOCK_STREAM: reliable byte stream (TCP) SOCK_DGRAM: message- oriented service (UDP) Protocol: specific protocol UNSPEC: unspecified (PF_INET and SOCK_STREAM already implies TCP) Example sockfd = socket( result->ai_family,!!!!!!!!result->ai_socktype,!!!!!!!!result->ai_protocol);! 17

Client: Connec;ng Socket to the Server Client contacts the server to establish connec;on Associate the socket with the server address/port Acquire a local port number (assigned by the OS) Request connec;on to server, who hopefully accepts connect is blocking Establishing the connec;on int connect( int sockfd,!! struct sockaddr *server_address,!!! socketlen_t addrlen!)!! Args: socket descriptor, server address, and address size Returns 0 on success, and - 1 if an error occurs E.g. connect(!sockfd,!!!!!!!result->ai_addr,!!!!!!!result->ai_addrlen);! 18

Client: Sending Data Sending data int send( int sockfd, void *msg,!!!!!! size_t len, int flags)!! Arguments: socket descriptor, pointer to buffer of data to send, and length of the buffer Returns the number of bytes wriyen, and - 1 on error send is blocking: return only aler data is sent Write short messages into a buffer and send once 19

Client: Receiving Data Receiving data int recv( int sockfd, void *buf,!!!!!! size_t len, int flags)! Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer Returns the number of characters read (where 0 implies "end of file"), and - 1 on error Why do you need len? What happens if buf's size < len? recv is blocking: return only aler data is received 20

Network byte order Big Endian Host byte order Byte Order Big Endian (IBM mainframes, Sun SPARC) or LiYle Endian (x86) Func;ons to deal with this htons() & htonl() (host to network short and long) ntohs() & ntohl() (network to host short and long) When to worry? puqng data onto the wire pulling data off the wire 21

Server: Server Preparing its Socket Server creates a socket and binds address/port Server creates a socket, just like the client does Server associates the socket with the port number Create a socket int socket( int domain,!!!!!!! int type, int protocol!)!! Bind socket to the local address and port number int bind( int sockfd,!!!!!! struct sockaddr *my_addr,!!!!!! socklen_t addrlen!)! 22

Server: Allowing Clients to Wait Many client requests may arrive Server cannot handle them all at the same ;me Server could reject the requests, or let them wait Define how many connec;ons can be pending int listen(int sockfd, int backlog)! Arguments: socket descriptor and acceptable backlog Returns a 0 on success, and - 1 on error Listen is non- blocking: returns immediately What if too many clients arrive? Some requests don't get through The Internet makes no promises And the client can always try again 23

Server: Accep;ng Client Connec;on Now all the server can do is wait Waits for connec;on request to arrive Blocking un;l the request arrives And then accep;ng the new request Accept a new connec;on from a client int accept( int sockfd,!!!!! struct sockaddr *addr,!!!! socketlen_t *addrlen)! Arguments: sockfd, structure that will provide client address and port, and length of the structure Returns descriptor of socket for this new connec;on 24

Client and Server: Cleaning House Once the connec;on is open Both sides and read and write Two unidirec;onal streams of data In prac;ce, client writes first, and server reads then server writes, and client reads, and so on Closing down the connec;on Either side can close the connec;on using the int close(int sockfd)! What about the data s;ll "in flight" Data in flight s;ll reaches the other end So, server can close() before client finishes reading 25

Server: One Request at a Time? Serializing requests is inefficient Server can process just one request at a ;me All other clients must wait un;l previous one is done What makes this inefficient? May need to ;me share the server machine Alternate between servicing different requests Do a liyle work on one request, then switch when you are wai;ng for some other resource (e.g., reading file from disk) "Nonblocking I/O" Or, use a different process/thread for each request Allow OS to share the CPU(s) across processes Or, some hybrid of these two approaches 26

Handle Mul;ple Clients using fork() Steps to handle mul;ple clients Go to a loop and accept connec;ons using accept() Aler a connec;on is established, call fork() to create a new child process to handle it Go back to listen for another socket in the parent process close() when you are done. Want to know more? Checkout out Beej's guide to network programming 27