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

Similar documents
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

TCP: Three-way handshake

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

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

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

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

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

Socket Programming for TCP and UDP

Elementary TCP Sockets

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

UNIT IV- SOCKETS Part A

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

Programming with TCP/IP. Ram Dantu

Group-A Assignment No. 6

Tutorial on Socket Programming

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

STUDY OF SOCKET PROGRAMMING

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

EEC-484/584 Computer Networks

Introduction to Socket Programming

Unix Network Programming

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Network Communication

Socket Programming TCP UDP

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

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

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

CLIENT-SIDE PROGRAMMING

Communication. Sockets (Haviland Ch. 10)

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Socket Programming(2/2)

Network programming(i) Lenuta Alboaie

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

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

System Programming. Sockets

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

CS 640: Computer Networking

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CS 3516: Computer Networks

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

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

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

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

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

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

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

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

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

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

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

UNIX Sockets. COS 461 Precept 1

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

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

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

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

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

CS321: Computer Networks Introduction to Application Layer

Computer Networks Prof. Ashok K. Agrawala

A Client-Server Exchange

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

CSE 333 Section 8 - Client-Side Networking

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

Network programming(ii) Lenuta Alboaie

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

Network Socket Programming - 3 BUPT/QMUL

CS321: Computer Networks Socket Programming

Client Server Computing

Network Socket Programming - 3 BUPT/QMUL

CSE 333 SECTION 7. Client-Side Network Programming

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

CSE 333 SECTION 8. Sockets, Network Programming

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

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

Topics for this Week

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

The User Datagram Protocol

Introduction to Socket Programming

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

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

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

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

Content 1. SDK Library Overview API Library Introduction API Data Structure HAL Driver Function Type LIB

Internet applications

Sockets 15H2. Inshik Song

Network Programming and Protocl Design. Nixu Ltd.

UDP CONNECT TO A SERVER

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

What s an API? Do we need standardization?

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

A. Basic Function Calls for Network Communications

CS4700/CS5700 Fundamentals of Computer Networking

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

SOCKETS. COMP750 Distributed Systems

Transcription:

Outline Distributed Computing Systems Sockets Socket basics Socket details (TCP and UDP) Socket options Final notes Socket Basics (1 of 2) An end-point for an Internet network connection what application layer plugs into User Application Socket Operating System Transport Layer Network Layer User sees descriptor - integer index or object handle like: FILE *, or file index from open() returned by call (more later) programmer cares about Application Programming Interface (API) Socket Basics (2 of 2) End point determined by two things: Host address: IP address is Network Layer Port number: is Transport Layer Two end-points determine a connection socket pair c1: 206.62.226.35,p21 + 198.69.10.2,p1500 c2: 206.62.226.35,p21 + 198.69.10.2,p1499 1

Ports Numbers (below is typical, since vary by OS): 0-1023 reserved, must be root 1024-5000 ephemeral, temporary use Above 5000 for general use (50,000 is specified max) Well-known, reserved services (see /etc/servicesin Unix). E.g., FTP 21 HTTP 80 IMAP 220 World of Warcraft 1119& 3724 Transport Layer UDP: User Datagram Protocol no acknowledgements no retransmissions out of order, duplicates possible connectionless TCP: Transmission Control Protocol reliable (in order, all arrive, no duplicates) flow control connection-based Note, TCP ~95% of all flows and packets on Internet (What applications don t (always) use UDP?) (What protocol for distributed shell?) Outline Socket Details Mini-Outline Socket basics Socket details (TCP and UDP) Socket options Final notes (done) (next) Unix Network Programming, W. Richard Stevens, 2nd edition, 1998, Prentice Hall Beej's Guide to Network Programming Project 2 Includes links to samples TCP Server and TCP Client (both in C) Addresses and Sockets Examples (talk-tcp, listen-tcp, ) Misc stuff setsockopt(), getsockopt() fcntl() 2

Addresses and Sockets Structure to hold address information Functions pass address from user to OS sendto() Functions pass address from OS to user recvfrom() Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* length of structure */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* TCP/UDP Port num*/ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* unused */ }; are also generic and IPv6 socket structures Server well-known port TCP Client-Server int socket(int family, int type, int protocol); Create a socket, giving access to transport layer service listen() (Block until connection) Handshake Data (request) Data (reply) End-of-File Client familyis one of AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), AF_ROUTE (access to routing tables), AF_KEY (for encryption) typeis one of SOCK_STREAM (TCP), SOCK_DGRAM (UDP) SOCK_RAW (for special IP packets, PING, etc. Must be root) setuid bit (-rwsr-xr-x root 2014 /sbin/ping*) protocol is 0 (used for some raw socket options) upon success returns socket descriptor Integer, like file descriptor Return -1 if failure 3

int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign local protocol address ( name ) to socket sockfd is socket descriptor from myaddr is pointer to address struct with: port numberand IP address if port is 0, then host will pick ephemeral port not usually for server (exception RPC port-map) IP address == INADDR_ANY(unless multiple nics) addrlen is length of structure returns 0 if ok, -1 on error EADDRINUSE( Address already in use ) listen() int listen(int sockfd, int backlog); Change socket state (to passive) for TCP server sockfd is socket descriptor from backlog is maximum number of incomplete connections historically 5 rarely above 15 on a even moderately busy Web server! sockets default to active (for client) change to passive so OS will accept connection int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); Return next completed connection. sockfd is socket descriptor from cliaddrand addrlenreturn protocol address from client returns brand new descriptor, created by OS note, if create new process or thread, can create concurrent server int close(int sockfd); Close socket for use. sockfd is socket descriptor from closes socket for reading/writing returns (doesn t block) attempts to send any unsent data socket option SO_LINGER block until data sent or discard any remaining data returns -1 if error 4

Server listen() (Block until connection) well-known port TCP Client-Server Handshake Data (request) Data (reply) Client int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server. sockfd is socket descriptor from servaddr is pointer to structure with: port number and IP address must be specified (unlike ) addrlen is length of structure client doesn t need OS will pick ephemeral port returns socket descriptor if ok, -1 on error End-of-File Sending and Receiving Server UDP Client-Server int recv(int sockfd, void *buff, size_t mbytes, int flags); int send(int sockfd, void *buff, size_t mbytes, int flags); Same as read()and write()but for flags MSG_DONTWAIT (this send non-blocking) MSG_OOB (out of band data, 1 byte sent ahead) MSG_PEEK (look, but don t remove) MSG_WAITALL (don t return less than mbytes) MSG_DONTROUTE (bypass routing table) recvfrom() (Block until receive datagram) sendto() well-known port - No handshake - No simultaneous close Data (reply) Data (request) Client sendto() recvfrom() 5

Sending and Receiving int recvfrom(int sockfd, void *buff, size_t mbytes, int flags, struct sockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_t mbytes, int flags, const struct sockaddr *to, socklen_t addrlen); Same as and but for addr recvfromfills in address of where packet came from sendtorequires address of where sending packet to with UDP Record address and port of peer datagrams to/from others are not allowed does not do three way handshake, or connection connect a misnomer, here. Should be setpeername() Use instead of sendto() Use instead of recvfrom() Can change connect or unconnectby repeating call (Can do similar with on receiver) Why use connected UDP? Send two datagrams unconnected: connect the socket output first dgram unconnect the socket connect the socket ouput second dgram unconnect the socket Send two datagrams connected: connect the socket output first dgram ouput second dgram Socket Options setsockopt(), getsockopt() SO_LINGER upon close, discard data or block until sent SO_RCVBUF, SO_SNDBUF change buffer sizes for TCP is pipeline, for UDP is discard SO_RCVLOWAT, SO_SNDLOWAT how much data before readable via select() SO_RCVTIMEO, SO_SNDTIMEO timeouts 6

Outline Socket basics Socket details (TCP and UDP) Socket options Final notes (done) (done) (next) TCP_KEEPALIVE Socket Options (TCP) idle time before close (2 hours, default) TCP_MAXRT set timeout value TCP_NODELAY disable Nagle Algorithm won t buffer data for larger chunk, but sends immediately fcntl() Final Notes Distributed Shell File control but used for sockets, too Signal driven sockets Set socket owner Get socket owner Set socket non-blocking flags = fcntl(sockfd, F_GETFL, 0); flags = O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); Beware not getting flags before setting! TCP (not UDP) Doesneed to handle more than one client at a time (a concurrent server) Refer to sample code online (talk, listen) Recommendation: Develop shell independently of sockets 7