Copyright © University of Illinois CS 241 Staff1 Network Programming.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
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()
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
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.
EECC694 - Shaaban #1 lec #14 Spring The Application Layer Client/Server Computing, Basic Approaches: –Passing Messages. Example: Communication.
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.
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Sirak Kaewjamnong Computer Network Systems
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,
 Wind River Systems, Inc Chapter - 13 Network Programming.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
Chapter 2 Applications and Layered Architectures Sockets.
Remote Shell CS230 Project #4 Assigned : Due date :
Network Programming with Sockets Reading: Stevens 3rd ed., Ch. 3-6, or 2 nd ed. Beej's Guide to Network Programming 1.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
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,
1 CS716 Advanced Computer Networks By A. Wahid Shaikh.
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.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Copyright © University of Illinois CS 241 Staff1 Sockets: send, recv Network Applications: HTTP.
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 2: Introduction to Unix Network Programming Reference: Stevens Unix Network Programming Spring 20121Copyright ©: CS 438 Staff, University of Illinois.
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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
UNIX Sockets COS 461 Precept 1.
Introduction to Unix Network Programming
Advanced Computer Networks
Week 13 - Friday CS222.
Socket Programming in C
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
UDP Sockets Programming
Socket Programming in C
TCP Sockets Programming
Internet Networking recitation #8
Presentation transcript:

Copyright © University of Illinois CS 241 Staff1 Network Programming

Announcements MP7  Extra example: “WikiTalk”  Date: (Beginning – Jan. 3, 2008) Every single interaction between users on the “talk” pages of Wikipedia 2,394,385 users 5,021,410 pairs of users “talking” Copyright © University of Illinois CS 241 Staff2

Network Programming As an Internet user… you already know a lot about the Internet! Copyright © University of Illinois CS 241 Staff3

Terminology google.com facebook.com illinois.edu Copyright © University of Illinois CS 241 Staff4 Domain Names

Terminology Copyright © University of Illinois CS 241 Staff5 Uniform Resource Locators (URLs)

Terminology Copyright © University of Illinois CS 241 Staff6 Protocol Hypertext Transfer Protocol (HTTP)

Terminology google.com  facebook.com  illinois.edu  Copyright © University of Illinois CS 241 Staff7 Internet Protocol (IP) Addresses

Terminology google.com  facebook.com  illinois.edu  How are these addresses translated? Copyright © University of Illinois CS 241 Staff8 Domain Name System (DNS) via Domain Name Servers

Client-Server Model Server: google Client: you (and everyone else) Copyright © University of Illinois CS 241 Staff9 google.com user

Client-Server Model Properties?  Client:  Server: Copyright © University of Illinois CS 241 Staff10 server client

Client-Server Model Properties?  Client: Initiates contact Waits for server’s response  Server: Well-known name Waits for contact Processes requests, sends replies Copyright © University of Illinois CS 241 Staff11 server client

How? Client-Server Model Properties?  Client: Initiates contact Waits for server’s response  Server: Well-known name Waits for contact Processes requests, sends replies Copyright © University of Illinois CS 241 Staff12 server client How?

Network Socket All communications across a network happen over a network socket. Properties: Copyright © University of Illinois CS 241 Staff13

Network Socket All communications across a network happen over a network socket. Properties:  A form of Inner-Process Communications  Bi-directional  Connection made via a socket address Copyright © University of Illinois CS 241 Staff14

Socket Address A socket address is:  IP Address  Port Number A socket must also bind to a specific transport-layer protocol.  TCP  UDP Copyright © University of Illinois CS 241 Staff15

Port Number? IP Addresses  Get a packet to the destination computer Port Numbers  Get a packet to the destination process Copyright © University of Illinois CS 241 Staff16 client/server

Port Numbers A port number is…  An 16-bit unsigned integer  A unique resource shared across the entire system Two processes cannot both utilize port 80.  Ports below 1024 are reserved Requires elevated privileges on many OSs Widely used applications have their own port number. Copyright © University of Illinois CS 241 Staff17

Application Port Numbers When we connect to google.com, what port on google.com are we connecting to? Copyright © University of Illinois CS 241 Staff18 We are connected to an HTTP server. Public HTTP servers always listen for new connections on port 80.

Initializing a socket… Two ways to initialize a socket: 1. To listen for an incoming connection  Often called a “Server Socket” 2. To connect to a “server socket” Copyright © University of Illinois CS 241 Staff19

Client-Server Model Copyright © University of Illinois CS 241 Staff20 serverclient Server:  Creates a socket to listen for incoming connections.  Must listen on a specific protocol/port. TCP/80

Client-Server Model Copyright © University of Illinois CS 241 Staff21 serverclient Client:  Creates a socket to connect to a remote computer. TCP/80

Client-Server Model Copyright © University of Illinois CS 241 Staff22 serverclient Client:  Requests a connection to TCP port 80 on TCP/80

Client-Server Model Copyright © University of Illinois CS 241 Staff23 serverclient Server:  Accepts the connection. TCP/80

Client-Server Model Copyright © University of Illinois CS 241 Staff24 serverclient Server:  Spawns a new socket to communicate directly with the newly connected client.  Allows other clients to connect. TCP/80 Two way communications

The sockaddr structure Earlier… a socket address is:  IP Address  Port Number This is represented in a special struct in C called a sockaddr. Copyright © University of Illinois CS 241 Staff25

Address Access/Conversion Functions #include int getaddrinfo(const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res); Parameters  node : host name or IP address to connect to  service : a port number (“80“) or the name of a service (found /etc/services: “http”)  hints : a filled out struct addrinfo CS 241Copyright ©: University of Illinois CS 241 Staff26

Example: Server int status; struct addrinfo hints; struct addrinfo *servinfo; // point to the results memset(&hints, 0, sizeof hints); // empty struct hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets hints.ai_flags = AI_PASSIVE; // fill in my IP for me if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } // servinfo now points to a linked list of 1 or more struct addrinfos //... do everything until you don't need servinfo anymore.... freeaddrinfo(servinfo); // free the linked-list CS 241Copyright ©: University of Illinois CS 241 Staff27

Example: Client int status; struct addrinfo hints; struct addrinfo *servinfo; // will point to the results memset(&hints, 0, sizeof hints); // make sure the struct is empty hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets // get ready to connect status = getaddrinfo(" "3490", &hints, &servinfo); // servinfo now points to a linked list of 1 or more struct addrinfos // etc. CS 241Copyright ©: University of Illinois CS 241 Staff28

Creating a “Server Socket” socket():Creates a new socket for a specific protocol (eg: TCP) bind():Binds the socket to a specific port (eg: 80) listen():Moves the socket into a state of listening for incoming connections. accept():Accepts an incoming connection. Copyright © University of Illinois CS 241 Staff29

Creating a “Client Socket” socket():Creates a new socket for a specific protocol (eg: TCP) connect(): Makes a network connection to a specified IP address and port. Copyright © University of Illinois CS 241 Staff30

CS 241Copyright ©: University of Illinois CS 241 Staff31 Functions: socket int socket (int family, int type, int protocol); Create a socket.  Returns file descriptor or -1. Also sets errno on failure.  family : address family (namespace) AF_INET for IPv4 other possibilities: AF_INET6 (IPv6), AF_UNIX or AF_LOCAL (Unix socket), AF_ROUTE (routing)  type : style of communication SOCK_STREAM for TCP (with AF_INET ) SOCK_DGRAM for UDP (with AF_INET )  protocol : protocol within family typically 0

CS 241Copyright ©: University of Illinois CS 241 Staff32 Example: socket int sockfd, new_fd;/* listen on sock_fd, new connection on new_fd */ struct sockaddr_in my_addr; /* my address */ struct sockaddr_in their_addr; /* connector addr */ int sin_size; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1){ perror("socket"); exit(1); }

CS 241Copyright ©: University of Illinois CS 241 Staff33 Function: bind int bind (int sockfd, struct sockaddr* myaddr, int addrlen); Bind a socket to a local IP address and port number  Returns 0 on success, -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  myaddr : includes IP address and port number IP address: set by kernel if value passed is INADDR_ANY, else set by caller port number: set by kernel if value passed is 0, else set by caller  addrlen : length of address structure = sizeof (struct sockaddr_in)

CS 241Copyright ©: University of Illinois CS 241 Staff34 Example: bind my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT);// short, network // byte order my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // automatically fill with my IP bzero(&(my_addr.sin_zero), 8); // zero struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); }

CS 241Copyright ©: University of Illinois CS 241 Staff35 Reserved Ports Keyword Decimal Description /tcp Reserved 0/udp Reserved tcpmux 1/tcp TCP Port Service tcpmux 1/udp TCP Port Service echo 7/tcp Echo echo 7/udp Echo systat 11/tcp Active Users systat 11/udp Active Users daytime 13/tcp Daytime (RFC 867) daytime 13/udp Daytime (RFC 867) qotd 17/tcp Quote of the Day qotd 17/udp Quote of the Day chargen 19/tcp Character Generator chargen 19/udp Character Generator ftp-data 20/tcp File Transfer Data ftp-data 20/udp File Transfer Data ftp 21/tcp File Transfer Ctl ftp 21/udp File Transfer Ctl ssh 22/tcp SSH Remote Login ssh 22/udp SSH Remote Login telnet 23/tcp Telnet telnet 23/udp Telnet smtp 25/tcp Simple Mail Transfer smtp 25/udp Simple Mail Transfer Keyword Decimal Description time 37/tcp Time time 37/udp Time name 42/tcp Host Name Server name 42/udp Host Name Server nameserver 42/tcp Host Name Server nameserver 42/udp Host Name Server nicname 43/tcp Who Is nicname 43/udp Who Is domain 53/tcp Domain Name Server domain 53/udp Domain Name Server whois++ 63/tcp whois++ whois++ 63/udp whois++ gopher 70/tcp Gopher gopher 70/udp Gopher finger 79/tcp Finger finger 79/udp Finger http 80/tcp World Wide Web HTTP http 80/udp World Wide Web HTTP www 80/tcp World Wide Web HTTP www 80/udp World Wide Web HTTP www-http 80/tcp World Wide Web HTTP www-http 80/udp World Wide Web HTTP kerberos 88/tcp Kerberos kerberos 88/udp Kerberos

CS 241Copyright ©: University of Illinois CS 241 Staff36 Functions: listen int listen (int sockfd, int backlog); Put socket into passive state (wait for connections rather than initiate a connection)  Returns 0 on success, -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  backlog : bound on length of unaccepted connection queue (connection backlog); kernel will cap, thus better to set high  Example: if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); }

CS 241Copyright ©: University of Illinois CS 241 Staff37 Establishing a Connection Include file int connect (int sockfd, struct sockaddr* servaddr, int addrlen);  Connect to another socket. int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen);  Accept a new connection. Returns file descriptor or -1.

CS 241Copyright ©: University of Illinois CS 241 Staff38 Functions: connect int connect (int sockfd, struct sockaddr* servaddr, int addrlen); Connect to another socket.  Returns 0 on success, -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  servaddr : IP address and port number of server  addrlen : length of address structure = sizeof (struct sockaddr_in) Can use with UDP to restrict incoming datagrams and to obtain asynchronous errors

CS 241Copyright ©: University of Illinois CS 241 Staff39 Example: connect their_addr.sin_family = AF_INET; /* interp’d by host */ their_addr.sin_port = htons (PORT); their_addr.sin_addr = *((struct in_addr*)he->h_addr); bzero (&(their_addr.sin_zero), 8); /* zero rest of struct */ if (connect (sockfd, (struct sockaddr*)&their_addr, sizeof (struct sockaddr)) == -1) { perror (“connect”); exit (1); }

CS 241Copyright ©: University of Illinois CS 241 Staff40 Functions: accept int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen); Block waiting for a new connection  Returns file descriptor or -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  cliaddr : IP address and port number of client (returned from call)  addrlen : length of address structure = pointer to int set to sizeof (struct sockaddr_in) addrlen is a value-result argument  the caller passes the size of the address structure, the kernel returns the size of the client’s address (the number of bytes written)

CS 241Copyright ©: University of Illinois CS 241 Staff41 Example: accept sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr*) &their_addr, &sin_size)) == -1) { perror("accept"); continue; } How does the server know which client it is?  their_addr.sin_addr contains the client’s IP address  their_addr.port contains the client’s port number printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));

Functions: accept Notes  After accept() returns a new socket descriptor, I/O can be done using read() and write()  Why does accept() need to return a new descriptor? CS 241Copyright ©: University of Illinois CS 241 Staff42

CS 241Copyright ©: University of Illinois CS 241 Staff43 Sending and Receiving Data int send(int sockfd, const void * buf, size_t nbytes, int flags);  Write data to a stream (TCP) or “connected” datagram (UDP) socket. Returns number of bytes written or -1. int recv(int sockfd, void *buf, size_t nbytes, int flags);  Read data from a stream (TCP) or “connected” datagram (UDP) socket. Returns number of bytes read or -1.

CS 241Copyright ©: University of Illinois CS 241 Staff44 Functions: send int send(int sockfd, const void * buf, size_t nbytes, int flags); Send data un a stream (TCP) or “connected” datagram (UDP) socket  Returns number of bytes written or -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  buf : data buffer  nbytes : number of bytes to try to write  flags : control flags MSG_PEEK: get data from the beginning of the receive queue without removing that data from the queue

CS 241Copyright ©: University of Illinois CS 241 Staff45 Functions: send int send(int sockfd, const void * buf, size_t nbytes, int flags); Example len = strlen(msg); bytes_sent = send(sockfd, msg, len, 0);

CS 241Copyright ©: University of Illinois CS 241 Staff46 Functions: recv int recv(int sockfd, void *buf, size_t nbytes, int flags); Read data from a stream (TCP) or “connected” datagram (UDP) socket  Returns number of bytes read or -1, sets errno on failure  Returns 0 if socket closed  sockfd : socket file descriptor (returned from socket )  buf : data buffer  nbytes : number of bytes to try to read  flags : see man page for details; typically use 0

CS 241Copyright ©: University of Illinois CS 241 Staff47 Functions: recv int recv(int sockfd, char* buf, size_t nbytes); Notes  read blocks waiting for data from the client but does not guarantee that sizeof(buf) is read  Example if((r = read(newfd, buf, sizeof(buf))) < 0) { perror(“read”); exit(1); }

Sending and Receiving Data Datagram sockets aren't connected to a remote host  What piece of information do we need to give before we send a packet?  The destination/source address! CS 241Copyright ©: University of Illinois CS 241 Staff48

CS 241Copyright ©: University of Illinois CS 241 Staff49 Sending and Receiving Data int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen);  Send a datagram to another UDP socket. Returns number of bytes written or -1. int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen);  Read a datagram from a UDP socket. Returns number of bytes read or -1.

CS 241Copyright ©: University of Illinois CS 241 Staff50 Functions: sendto int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen); Send a datagram to another UDP socket  Returns number of bytes written or -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  buf : data buffer  nbytes : number of bytes to try to read  flags : see man page for details; typically use 0  destaddr : IP address and port number of destination socket  addrlen : length of address structure = sizeof (struct sockaddr_in)

CS 241Copyright ©: University of Illinois CS 241 Staff51 Functions: sendto int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen); Example n = sendto(sock, buf, sizeof(buf), 0,(struct sockaddr *) &from,fromlen); if (n < 0) perror("sendto"); exit(1); }

CS 241Copyright ©: University of Illinois CS 241 Staff52 Functions: recvfrom int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen); Read a datagram from a UDP socket.  Returns number of bytes read (0 is valid) or -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  buf : data buffer  nbytes : number of bytes to try to read  flags : see man page for details; typically use 0  srcaddr : IP address and port number of sending socket (returned from call)  addrlen : length of address structure = pointer to int set to sizeof (struct sockaddr_in)

CS 241Copyright ©: University of Illinois CS 241 Staff53 Functions: recvfrom int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen); Example n = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&from,&fromlen); if (n < 0) { perror("recvfrom"); exit(1); }

CS 241Copyright ©: University of Illinois CS 241 Staff54 Tearing Down a Connection int close (int sockfd);  Close a socket. Returns 0 on success, -1 and sets errno on failure. int shutdown (int sockfd, int howto);  Force termination of communication across a socket in one or both directions. Returns 0 on success, -1 and sets errno on failure.

CS 241Copyright ©: University of Illinois CS 241 Staff55 Functions: close int close (int sockfd); Close a socket  Returns 0 on success, -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket ) Closes communication on socket in both directions  All data sent before close are delivered to other side (although this aspect can be overridden) After close, sockfd is not valid for reading or writing

CS 241Copyright ©: University of Illinois CS 241 Staff56 Functions: shutdown int shutdown (int sockfd, int howto); Force termination of communication across a socket in one or both directions  Returns 0 on success, -1 and sets errno on failure  sockfd : socket file descriptor (returned from socket )  howto : SHUT_RD to stop reading SHUT_WR to stop writing SHUT_RDWR to stop both shutdown overrides the usual rules regarding duplicated sockets, in which TCP teardown does not occur until all copies have closed the socket

Note on close vs. shutdown close() : closes the socket but the connection is still open for processes that shares this socket  The connection stays opened both for read and write shutdown() : breaks the connection for all processes sharing the socket  A read will detect EOF, and a write will receive SIGPIPE  shutdown() has a second argument how to close the connection: 0 means to disable further reading 1 to disable writing 2 disables both CS 241Copyright ©: University of Illinois CS 241 Staff57