Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets IMGD Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Tutorial 8 Socket Programming
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Introduction to Project 1 Web Client and Server Jan 2006.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Socket Programming Lab 1 1CS Computer Networks.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Today’s topic: UDP Reliable communication over UDP.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Sockets API Developing Applications using the Sockets API.
Socket Option.
Operating Systems Sockets ENCE 360.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Distributed Computing Systems Sockets

Outline 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 socket() 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: ,p ,p1500 – c2: ,p ,p1499

Ports Numbers (below is typical, since vary by OS): – “reserved”, must be root – “ephemeral”, temporary use – Above 5000 for general use ( 50,000 is specified max) Well-known, reserved services (see /etc/services in Unix). E.g., FTP 21 HTTP80 IMAP220 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 basics(done) Socket details (TCP and UDP)(next) Socket options Final notes

Socket Details Mini-Outline 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()

Addresses and Sockets Structure to hold address information Functions pass address from user to OS bind() connect() sendto() Functions pass address from OS to user accept() 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

TCP Client-Server socket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection ) “Handshake” recv() send() Data (request) Data (reply) close() End-of-File recv() close() “well-known” port

socket() int socket(int family, int type, int protocol); Create a socket, giving access to transport layer service family is one of – AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), – AF_ROUTE (access to routing tables), AF_KEY (for encryption) type is 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

bind() sockfd is socket descriptor from socket() myaddr is pointer to address struct with: – port number and 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”) int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign local protocol address (“name”) to socket

listen() sockfd is socket descriptor from socket() 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 listen(int sockfd, int backlog); Change socket state (to passive) for TCP server

accept() sockfd is socket descriptor from socket() cliaddr and addrlen return protocol address from client returns brand new descriptor, created by OS note, if create new process or thread, can create concurrent server int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); Return next completed connection.

close() sockfd is socket descriptor from socket() 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 int close(int sockfd); Close socket for use.

TCP Client-Server socket() bind() listen() accept() Server socket() connect() send() recv() Client (Block until connection ) “Handshake” recv() send() Data (request) Data (reply) close() End-of-File recv() close() “well-known” port

connect() sockfd is socket descriptor from socket() servaddr is pointer to structure with: – port number and IP address – must be specified (unlike bind() ) addrlen is length of structure client doesn’t need bind() – OS will pick ephemeral port returns socket descriptor if ok, -1 on error int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server.

Sending and Receiving 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 )

UDP Client-Server socket() bind() recvfrom() Server socket() sendto() recvfrom() Client (Block until receive datagram) sendto() Data (request) Data (reply) close() “well-known” port - No “handshake” - No simultaneous close

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 recv() and send() but for addr – recvfrom fills in address of where packet came from – sendto requires address of where sending packet to

connect() 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 send () instead of sendto () Use recv () instead of recvfrom () Can change connect or unconnect by repeating connect () call (Can do similar with bind() 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

Outline Socket basics(done) Socket details (TCP and UDP)(done) Socket options(next) Final notes

Socket Options (TCP) TCP_KEEPALIVE – 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() ‘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!

Final Notes – Distributed Shell TCP (not UDP) Does need 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