Networked Applications: Sockets

Slides:



Advertisements
Similar presentations
1 Sockets Nick Feamster Computer Networking I Spring 2013.
Advertisements

Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Networked Applications: Sockets COS 461: Computer Networks Spring 2010 (MW 3:00-4:20 in CS 105) Michael Freedman Teaching Assistants: Muneeb Ali and David.
Socket Programming.
1 Networked Applications: Sockets COS 461: Computer Networks Spring 2008 (MW 1:30-2:50 in CS 105) Jennifer Rexford Teaching Assistants: Sunghwan Ihm and.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CSE 486/586 Distributed Systems Socket Programming and Android
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1 Networked Applications: Sockets COS 461: Computer Networks Spring 2006 (MW 1:30-2:50 in Friend 109) Jennifer Rexford Teaching Assistant: Mike Wawrzoniak.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
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.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
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.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
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.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
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.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
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.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
UDP Sockets Programming
Socket Programming in C
Chapter 2 Application Layer
Tutorial on Socket Programming
Internet Networking recitation #8
Presentation transcript:

Networked Applications: Sockets Assembled by Ossi Mokryn, based on Slides by Jennifer Rexford, Princeton, And on data from beej’s guide : http://beej.us/guide/bgnet

Socket programming Socket API Goal: learn how to build client/server application that communicate using sockets Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Application Layer

“Site under construction” Clients and Servers Client program Running on end host Requests service E.g., Web browser Server program Running on end host Provides service E.g., Web server GET /index.html “Site under construction”

Client-Server Communication Client “sometimes on” Initiates 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 the server’s address Server is “always on” Services requests from many client hosts E.g., Web server for the www.cnn.com Web site Doesn’t initiate contact with the clients Needs a fixed, well-known address

Client and Server Processes Program vs. process Program: collection of code Process: a running program on a host Communication between processes Same end host: inter-process communication Governed by the operating system on the end host Different end hosts: exchanging messages Governed by the network protocols Client and server processes Client process: process that initiates communication Server process: process that waits to be contacted

Socket: End Point of Communication 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 Application Programming Interface Supports the creation of network applications User process User process socket socket Operating System Operating System

Identifying the Receiving Process Sending process must identify the receiver Name or address of the receiving end host Identifier that specifies the receiving process Receiving host Destination address that uniquely identifies the host An IP address is a 32-bit quantity Receiving process Host may be running many different processes Destination port that uniquely identifies the socket A port number is a 16-bit quantity

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

Knowing What Port Number To Use Popular applications have well-known ports E.g., port 80 for Web and port 25 for e-mail Well-known ports listed at http://www.iana.org Well-known vs. ephemeral ports Server has a well-known port (e.g., port 80) Between 0 and 1023 Client picks an unused ephemeral (i.e., temporary) port Between 1024 and 65535 Uniquely identifying the traffic between the hosts Two IP addresses and two port numbers Underlying transport protocol (e.g., TCP or UDP)

Delivering the Data: Division of Labor Network Deliver data packet to the destination host Based on the destination IP address Operating system Deliver data to the destination socket Based on the protocol and destination port # Application Read data from the socket Interpret the data (e.g., render a Web page)

Windows Socket API Socket interface In UNIX, everything is like a file Originally provided in Berkeley UNIX Later adopted by all popular operating systems Simplifies porting applications to different OSes In UNIX, everything is like a file All input is like reading a file All output is like writing a file File is represented by an integer file descriptor System calls for sockets Client: create, connect, write, read, close Server: create, bind, listen, accept, read, write, close Winsock Programmer's FAQ: http://tangentsoft.net/wskfaq/newbie.html#interop

Typical Client Program Prepare to communicate Create a socket Determine server address and port number Initiate the connection to the server Exchange data with the server Write data to the socket Read data from the socket Do stuff with the data (e.g., render a Web page) Close the socket

Socket programming with UDP UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Application Layer

Client/server socket interaction: UDP Server (running on hostid) create socket, clientSocket = DatagramSocket() Client Create, address (hostid, port=x, send datagram request using clientSocket create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket close clientSocket read reply from clientSocket write reply to serverSocket specifying client host address, port number Application Layer

Creating a Socket: socket() Operation to create a socket SOCKET WSAAPI socket ( int af, int type, int protocol); af An address family specification. only format currently supported is PF_INET, which is the ARPA Internet address format. Type: semantics of the communication SOCK_STREAM: reliable byte stream SOCK_DGRAM: message-oriented service Protocol: specific protocol 0: unspecified (PF_INET and SOCK_STREAM already implies TCP, PF_INET and SOCK_DGRAM implies UDP).

Establishing the server’s name and port - history struct sockaddr_in server; server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr(IPstring); server.sin_port = htons(ServerPort);

Establishing the server’s name and port – today (from http://beej int getaddrinfo(const char *node, or IP const char *service, const struct addrinfo *hints, struct addrinfo **res); You give this function three input parameters, and it gives you a pointer to a linked-list, res, of results. The node parameter is the host name to connect to, or an IP address. e.g. "www.example.com" Next is the parameter service, which can be a port number, like "80", or the name of a particular service (found in The IANA Port List or the /etc/services file on your Unix machine) like "http" or "ftp" or "telnet" or "smtp" or whatever. Finally, the hints parameter points to a struct addrinfo that you've already filled out with relevant information.

Establishing the Connection int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen) Arguments: socket descriptor, server address, and address size Returns 0 on success, and -1 if an error occurs Slides by Jennifer Rexford

Sending and Receiving Data Sending data ssize_t write(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer of data to send, and length of the buffer Returns the number of characters written, and -1 on error Receiving data ssize_t read(int sockfd, void *buf, size_t len) 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 Closing the socket int close(int sockfd)

Sending Data s A descriptor identifying a (possibly connected) socket. int WSAAPI sendto ( SOCKET s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen ); s A descriptor identifying a (possibly connected) socket. buf A buffer containing the data to be transmitted. len The length of the data in buf. flags Specifies the way in which the call is made. to An optional pointer to the address of the target socket. tolen The size of the address in to. Slides by Jennifer Rexford

Receiving Data int WSAAPI recvfrom ( SOCKET s, char FAR* buf, int len, int flags, struct sockaddr FAR* from, int FAR* fromlen ); s A descriptor identifying a bound socket. buf A buffer for the incoming data. len The length of buf. flags Specifies the way in which the call is made. from An optional pointer to a buffer which will hold the source address upon return. fromlen An optional pointer to the size of the from buffer. Slides by Jennifer Rexford

Byte Ordering: Little and Big Endian Hosts differ in how they store data E.g., four-byte number (byte3, byte2, byte1, byte0) Little endian (“little end comes first”)  Intel PCs!!! Low-order byte stored at the lowest memory location Byte0, byte1, byte2, byte3 Big endian (“big end comes first”) High-order byte stored at lowest memory location Byte3, byte2, byte1, byte 0 IP is big endian (aka “network byte order”) Use htons() and htonl() to convert to network byte order Use ntohs() and ntohl() to convert to host order

Why Can’t Sockets Hide These Details? Dealing with endian differences is tedious Couldn’t the socket implementation deal with this … by swapping the bytes as needed? No, swapping depends on the data type Two-byte short int: (byte 1, byte 0) vs. (byte 0, byte 1) Four-byte long int: (byte 3, byte 2, byte 1, byte 0) vs. (byte 0, byte 1, byte 2, byte 3) String of one-byte charters: (char 0, char 1, char 2, …) in both cases Socket layer doesn’t know the data types Sees the data as simply a buffer pointer and a length Doesn’t have enough information to do the swapping

Servers Differ From Clients Passive open Prepare to accept connections … but don’t actually establish one … until hearing from a client Hearing from multiple clients Allow a backlog of waiting clients ... in case several try to start a connection at once Create a socket for each client Upon accepting a new client … create a new socket for the communication

Typical Server Program Prepare to communicate Create a socket Associate local address and port with the socket Wait to hear from a client (passive open) In TCP: Indicate how many clients-in-waiting to permit Accept an incoming connection from a client Exchange data with the client over new socket Receive data from the socket Do stuff to handle the request (e.g., get a file) Send data to the socket Close the socket Repeat with the next connection request

Server Preparing its Socket Bind socket to the local address and port number (Associate a local address with a socket) #include <winsock2.h> int WSAAPI bind ( SOCKET s, struct sockaddr* name, int namelen); Arguments: socket descriptor, server address, address length  s A descriptor identifying an unbound socket. name  The address to assign to the socket. Often, you bound your listening socket to the special IP address INADDR_ANY. This allows your program to work without knowing the IP address of the machine it was running on, or, in the case of a machine with multiple network interfaces, it allows your server to receive packets destined to any of the interfaces. When sending, a socket bound with INADDR_ANY binds to the default IP address, which is that of the lowest- numbered interface. Namelen The length of the name. Returns 0 on success, and -1 if an error occurs For TCP: Define how many connections can be pending int listen(int sockfd, int backlog) Arguments: socket descriptor and acceptable backlog Returns 0 on success, and -1 on error

Putting it All Together Server socket() bind() Client listen() socket() establish connection accept() connect() block send request write() read() process request send response write() read()

Serving One Request at a Time? Serializing requests is inefficient Server can process just one request at a time All other clients must wait until previous one is done Need to time share the server machine Alternate between servicing different requests Do a little work on one request, then switch to another Small tasks, like reading HTTP request, locating the associated file, reading the disk, transmitting parts of the response, etc. Or, start a new process to handle each request Allow the operating system to share the CPU across processes Or, some hybrid of these two approaches

TCP info for later in the course

Socket-programming using TCP Socket: a door between application process and end-end- transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by application developer process TCP with buffers, variables socket process TCP with buffers, variables socket controlled by operating system controlled by operating system internet host or server host or server Application Layer

Putting it All Together Server socket() bind() Client listen() socket() establish connection accept() connect() block send request write() read() process request send response write() read()

TCP info for later in the course

TCP: Accepting a New Client Connection Accept a new connection from a client int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen) Arguments: socket descriptor, structure that will provide client address and port, and length of the structure Returns descriptor for a new socket for this connection Questions What happens if no clients are around? The accept() call blocks waiting for a client What happens if too many clients are around? Some connection requests don’t get through … But, that’s okay, because the Internet makes no promises

Serving One Request at a Time? Serializing requests is inefficient Server can process just one request at a time All other clients must wait until previous one is done Need to time share the server machine Alternate between servicing different requests Do a little work on one request, then switch to another Small tasks, like reading HTTP request, locating the associated file, reading the disk, transmitting parts of the response, etc. Or, start a new process to handle each request Allow the operating system to share the CPU across processes Or, some hybrid of these two approaches

Blocking and non blocking "block" is a techie jargon for "sleep“ Lots of functions block. accept() blocks. All the recv() functions block. Why? Because we programmed them this way: When you first create the socket with socket(), the kernel sets it to blocking. Can we set the socket to be non-blocking? What does it mean? Yes. It means you have to poll it for data (check on it) If there’s no data yet, you get -1 (err) and have to try again. Isn’t that CPU intensive? Yes! So, you can use select()…

Reading or writing to multiple sockets A server wants to listen for incoming connections as well as keep reading from the connections it has. Select – a way to monitor several sockets at the same time, and know their situation. select()—Synchronous I/O Multiplexing Manipulates sets of sockets and lets you know: which ones are ready for reading which are ready for writing How? By creating and handling of sets of sockets, and obtaining additional information for each.

Manipulating Sets for the select() func. FD_SET(int fd, fd_set *set); Add fd to the set. FD_CLR(int fd, fd_set *set); Remove fd from the set. FD_ISSET(int fd, fd_set *set); Return true if fd is in the set. FD_ZERO(fd_set *set); Clear all entries from the set. Note: fd in Windows in the Socket structure

Additional select() info What happens if a socket in the read set closes the connection? Well, in that case, select() returns with that socket descriptor set as "ready to read". When you actually do recv() from it, recv() will return 0. That's how you know the client has closed the connection. One more note of interest about select(): if you have a socket that is listen()ing, you can check to see if there is a new connection by putting that socket's file descriptor in the readfds set.

Wanna See Real Clients and Servers? Apache Web server Open source server first released in 1995 Name derives from “a patchy server” ;-) Software available online at http://www.apache.org Mozilla Web browser http://www.mozilla.org/developer/ Sendmail http://www.sendmail.org/ BIND Domain Name System Client resolver and DNS server http://www.isc.org/index.pl?/sw/bind/ …

A final note: what is Peer-to-Peer Communication No always-on server at the center of it all Hosts can come and go, and change addresses Hosts may have a different address each time Example: peer-to-peer file sharing Any host can request files, send files, query to find where a file is located, respond to queries, and forward queries Scalability by harnessing millions of peers Each peer acting as both a client and server