CS 3700 Networks and Distributed Systems

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

CS 4700 / CS 5700 Network Fundamentals
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
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.
Sockets COS 518: Advanced Computer Systems Michael Freedman Fall
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.
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.
Fall 2000Datacom 11 Lecture 4 Socket Interface Programming: Service Interface between Applications and TCP.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Lecture 4: Networked Applications & Sockets Reading: Chapter 1 ? CMSC 23300/33300 Computer Networks
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
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.
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
UNIX Internet Socket API
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
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.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
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.
Sockets API Developing Applications using the Sockets API.
Sockets and Beginning Network Programming
CS 3700 Networks and Distributed Systems
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP Sockets Programming
Chapter 2 Application Layer
Tutorial on Socket Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Presentation transcript:

CS 3700 Networks and Distributed Systems Christo Wilson 8/22/2012 CS 3700 Networks and Distributed Systems Crash Course in C Sockets (Prepare yourself for Project 1) Revised 8/19/15 Defense

Socket Programming Goal: familiarize yourself with socket programming Why am I presenting C sockets? Because C sockets are the de-facto standard for networking APIs Project 1: Implement a semi-trivial protocol We will have a server set up for you There may be chances for extra credit ;)

C Sockets Socket API since 1983 Original interface of TCP/IP Berkeley Sockets BSD Sockets (debuted with BSD 4.2) Unix Sockets (originally included with AT&T Unix) Posix Sockets (slight modifications) Original interface of TCP/IP All other socket APIs based on C sockets

Outline High-level Design Server API Client API + Name resolution Other Considerations

Clients and Servers A fundamental problem: rendezvous One or more parties want to provide a service One or more parties want to use the service How do you get them together? Solution: client-server architecture Client: initiator of communication Server: responder At least one side has to wait for the other Service provider (server) sits and waits Clients locates servers, initiates contact Use well-known semantic names for location (DNS)

Key Differences Clients Servers Clients Servers Execute on-demand Unprivileged Simple (Usually) sequential Not performance sensitive Clients Servers Clients Servers Always-on Privileged Complex (Massively) concurrent High performance Scalable

Similarities Share common protocols Application layer Transport layer Network layer Both rely on APIs for network access

Sockets Basic network abstraction: the socket Socket: an object that allows reading/writing from a network interface In Unix, sockets are just file descriptors read() and write() both work on sockets Caution: socket calls are blocking

C Socket API Overview Clients gethostbyname() socket() connect() write() / send() read() / recv() close() Servers socket() bind() listen() while (whatever) { accept() read() / recv() write() / send() close() }

int socket(int, int, int) Most basic call, used by clients and servers Get a new socket Parameters int domain: a constant, usually PF_INET int type: a constant, usually SOCK_STREAM or SOCK_DGRAM SOCK_STREAM means TCP SOCK_DGRAM means UDP int protocol: usually 0 (zero) Return: new file descriptor, -1 on error Many other constants are available Why so many options? The C socket API is extensible. The Internet isn’t the only network domain TCP/UDP aren’t the only transport protocols In theory, transport protocols may have different dialects

int bind(int, struct sockaddr *, int) Used by servers to associate a socket to a network interface and a port Why is this necessary? Parameters: int sockfd: an unbound socket struct sockaddr * my_addr: the desired IP address and port int addrlen: sizeof(struct sockaddr) Return: 0 on success, -1 on failure Why might bind() fail? Each machine may have multiple network interfaces Example: Wifi and Ethernet in your laptop Example: Cellular and Bluetooth in your phone Each network interface has its own IP address We’ll talk about ports next…

TCP/UDP port field is 16-bits wide Port Numbers Basic mechanism for multiplexing applications per host 65,535 ports available Why? Ports <1024 are reserved Only privileged processes (e.g. superuser) may access Does this cause security issues? “I tried to open a port and got an error” Port collision: only one app per port per host Dangling sockets… TCP/UDP port field is 16-bits wide In olden times, all important apps used low port numbers Examples: IMAP, POP, HTTP, SSH, FTP This rule is no longer useful

Dangling Sockets Common error: bind fails with “already in use” error OS kernel keeps sockets alive in memory after close() Usually a one minute timeout Why? Allowing socket reuse int yes=1; if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } Closing a TCP socket is a multi-step process Involves contacting the remote machine “Hey, this connection is closing” Remote machine must acknowledge the closing All this book keeping takes time

struct sockaddr Structure for storing naming information But, different networks have different naming conventions Example: IPv4 (32-bit addresses) vs. IPv6 (64-bit addresses) In practice, use more specific structure implementation struct sockaddr_in my_addr; memset(&my_addr, 0, sizeof(sockaddr_in)); my_addr.sin_family = htons(AF_INET); my_addr.sin_port = htons(MyAwesomePort); my_addr.sin_addr.s_addr = inet_addr("10.12.110.57");

htons(), htonl(), ntohs(), ntohl() Little Endian vs. Big Endian Not a big deal as long as data stays local What about when hosts communicate over networks? Network byte order Standardized to Big Endian Be careful: x86 is Little Endian Functions for converting host order to network order h to n s – host to network short (16 bits) h to n l – host to network long (32 bits) n to h * – the opposite

Binding Shortcuts If you don’t care about the port my_addr.sin_port = htons(0); Chooses a free port at random This is rarely the behavior you want If you don’t care about the IP address my_addr.sin_addr.s_addr = htonl(INADDR_ANY); INADDR_ANY == 0 Meaning: don’t bind to a specific IP Traffic on any interface will reach the server Assuming its on the right port This is usually the behavior you want

int listen(int, int) Put a socket into listen mode Parameters Used on the server side Wait around for a client to connect() Parameters int sockfd: the socket int backlog: length of the pending connection queue New connections wait around until you accept() them Just set this to a semi-large number, e.g. 1000 Return: 0 on success, -1 on error

int accept(int, void *, int *) Accept an incoming connection on a socket Parameters int sockfd: the listen()ing socket void * addr: pointer to an empty struct sockaddr Clients IP address and port number go here In practice, use a struct sockaddr_in int * addrlen: length of the data in addr In practice, addrlen == sizeof(struct sockaddr_in) Return: a new socket for the client, or -1 on error Why? You don’t want to consume your listen() socket Otherwise, how would you serve more clients? Closing a client connection shouldn’t close the server

close(int sockfd) Close a socket shutdown(int sockfd, int how) No more sending or receiving shutdown(int sockfd, int how) Partially close a socket how = 0; // no more receiving how = 1; // no more sending how = 2; // just like close() Note: shutdown() does not free the file descriptor Still need to close() to free the file descriptor

C Socket API Overview Clients gethostbyname() socket() connect() write() / send() read() / recv() close() Servers socket() bind() listen() while (whatever) { accept() read() / recv() write() / send() close() }

struct * gethostbyname(char *) Returns information about a given host Parameters const char * name: the domain name or IP address of a host Examples: “www.google.com”, “10.137.4.61” Return: pointer to a hostent structure, 0 on failure Various fields, most of which aren’t important struct hostent * h = gethostname(“www.google.com”); struct sockaddr_in my_addr; memcpy(&my_addr.sin_addr.s_addr, h->h_addr, h->h_length);

int connect(int, struct sockaddr *, int) Connect a client socket to a listen()ing server socket Parameters int sockfd: the client socket struct sockaddr * serv_addr: address and port of the server int addrlen: length of the sockaddr structure Return: 0 on success, -1 on failure Notice that we don’t bind() the client socket Why?

write() and send() ssize_t write(int fd, const void *buf, size_t count); fd: file descriptor (ie. your socket) buf: the buffer of data to send count: number of bytes in buf Return: number of bytes actually written int send(int sockfd, const void *msg, int len, int flags); First three, same as above flags: additional options, usually 0 Do not assume that count / len == the return value! Why might this happen?

read() and recv() ssize_t read(int fd, void *buf, size_t count); Fairly obvious what this does int recv(int sockfd, void *buf, int len, unsigned int flags); Seeing a pattern yet? Return values: -1: there was an error reading from the socket Usually unrecoverable. close() the socket and move on >0: number of bytes received May be less than count / len 0: the sender has closed the socket

More Resources Beej’s famous socket tutorial https://beej.us/guide/bgnet/html/single/bgnet.html