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

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

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

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

ICT 6544 Distributed Systems Lecture 5

Unix Network Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Introduction to Socket Programming

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

Network Communication

Socket Programming for TCP and UDP

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

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

Programming with TCP/IP. Ram Dantu

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

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

ECE 435 Network Engineering Lecture 2

ECE 435 Network Engineering Lecture 2

Elementary TCP Sockets

CS321: Computer Networks Introduction to Application Layer

CS321: Computer Networks Socket Programming

UNIX Network Programming. Overview of Socket API Network Programming Basics

Tutorial on Socket Programming

Socket Programming TCP UDP

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

Application Programming Interfaces

Department of Computer Science

CS 3516: Computer Networks

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

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Network programming(i) Lenuta Alboaie

SOCKETS. COMP750 Distributed Systems

Network Socket Programming - 3 BUPT/QMUL

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

UNIT IV- SOCKETS Part A

Network Socket Programming - 3 BUPT/QMUL

CS 640: Computer Networking

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

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

UNIX Sockets. COS 461 Precept 1

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

Socket Programming 2007/03/28

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

A Client-Server Exchange

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

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

CS4514 B08 HELP Session 1

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

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Why a Computer Network??? Stand alone Computer. For What?

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

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

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

Network Socket Programming - 2 BUPT/QMUL

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

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

Group-A Assignment No. 6

Lab 0. Yvan Petillot. Networks - Lab 0 1

Piotr Mielecki Ph. D.

Network Programming and Protocl Design. Nixu Ltd.

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

TCP: Three-way handshake

STUDY OF SOCKET PROGRAMMING

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

Introduction to Client-Server Model

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

CLIENT-SIDE PROGRAMMING

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

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

Sockets 15H2. Inshik Song

Interprocess Communication Mechanisms

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

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

Network Programming November 3, 2008

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

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

CSE 124 Discussion Section Sockets Programming 10/10/17

Interprocess Communication Mechanisms

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

Topics for this Week

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

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

CPSC 213. Introduction to Computer Systems. Inter-Process Communication. Unit 2f

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

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

The User Datagram Protocol

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

Computer Networks Prof. Ashok K. Agrawala

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

Linux Network Programming, Part 1

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

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

Transcription:

Introduction to Unix Network Programming Reference: Stevens Unix Network Programming How do we Communicate? Send a mail from Alice to Bob Bob Alice in Champaign, Bob in Hollywood Example: US Postal Service Alice Hollywood, California Champaign, Illinois 8/30/06 UIUC - CS/ECE 438, Fall 2006 2 What does Alice do? What does Bob do? Alice 200 Cornfield Rd. Champaign, IL 61820 Alice 200 Cornfield Rd. Champaign, IL 61820 Bob 100 Santa Monica Blvd. Hollywood, CA 90028 Bob 100 Santa Monica Blvd. Hollywood, CA 90028 Bob s address (to a mailbox) Bob s name in case people share mailbox Postage have to pay! Alice s own name and address in case Bob wants to return a message Install a mailbox Receive the mail Get rid of envelope Read the message 8/30/06 UIUC - CS/ECE 438, Fall 2006 3 8/30/06 UIUC - CS/ECE 438, Fall 2006 4 What about sending a TCP/IP packet? Two simplest networking programs Very similar to Alice-mailing-to-Bob Different terminologies Different technologies Suppose to be better (faster, more reliable, cheaper, ) 8/30/06 UIUC - CS/ECE 438, Fall 2006 5 Alice the sending process Alice s address: 128.174.246.177 (IP addr) Alice s name: 12345 (port #) int main () { int main () { int sockfd; int sockfd, n; struct sockaddr_in bob_addr, alice_addr; struct sockaddr_in bob_addr, alice_addr; char mesg[100]; bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_family = AF_INET; bob_addr.sin_port = 23456; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456; // do the same for alice_addr sockfd = (AF_INET, SOCK_DGRAM, 0); sockfd = (AF_INET, SOCK_DGRAM, 0); bind(sockfd, &bob_addr, sizeof(bob_addr)); sendto(sockfd, hi, strlen( hi ), 0, &bob_addr, sizeof(bob_addr)); n= recvfrom(sockfd, mesg, 100, 0, &alice_addr, sizeof(alice_addr)); 8/30/06 UIUC - CS/ECE 438, Fall 2006 6 Bob the receiving process Bob s address: 216.52.167.132 (IP addr) Bob s name: 23456 (port #) 1

What are the problems? Direction and Principles Message may be lost Network is congested Receiver is congested Message may be duplicated, corrupted Multiple messages: re-ordered Concurrent connections Transport Network Data Link Physical Programming Principles and Concepts learn to use Internet for communication (with focus on implementation of networking concepts) learn to build network from ground up 8/30/06 UIUC - CS/ECE 438, Fall 2006 7 8/30/06 UIUC - CS/ECE 438, Fall 2006 8 Sockets Network Programming with Sockets process sends/receives messages to/from its analogous to mailbox sending process relies on transport infrastructure which brings message to at receiving process host or process TCP with buffers, variables controlled by app developer Internet host or process TCP with buffers, variables Reading: Stevens 2nd ed., Ch. 1-6 or 1st ed., Ch. 1-3, 6 Sockets API: A transport layer service interface Introduced in 1981 by BSD 4.1 Implemented as library and/or system calls Similar interfaces to TCP and UDP Can also serve as interface to IP (for super-user); known as raw s 8/30/06 UIUC - CS/ECE 438, Fall 2006 9 8/30/06 UIUC - CS/ECE 438, Fall 2006 10 Outline -Server Model -Sever Model TCP/UDP Overview Addresses and Data Sockets API Example Server Asymmetric Communication sends requests Server sends replies Server/Daemon Well-known name (e.g., IP address + port) Waits for contact Processes requests, sends replies Initiates contact Waits for response 8/30/06 UIUC - CS/ECE 438, Fall 2006 11 8/30/06 UIUC - CS/ECE 438, Fall 2006 12 2

Socket Communication -Server Communication Model process 3-way handshaking Server process Listening Connection Service Model Concurrent: Server processes multiple s requests simultaneously Sequential: Server processes only one s requests at a time Hybrid: Server maintains multiple connections, but processes responses sequentially and categories are not disjoint A can be a of another A can be a of its own 8/30/06 UIUC - CS/ECE 438, Fall 2006 13 8/30/06 UIUC - CS/ECE 438, Fall 2006 14 TCP Connections TCP Service Transmission Control Protocol (TCP) Service OSI Transport Layer Service Model Reliable byte stream (interpreted by application) 16-bit port space allows multiple connections on a single host Connection-oriented Set up connection before communicating Tear down connection when done Reliable Data Transfer Guarantees delivery of all data Exactly once if no catastrophic failures Sequenced Data Transfer Guarantees in-order delivery of data If A sends M1 followed by M2 to B, B never receives M2 before M1 Regulated Data Flow Monitors network and adjusts transmission appropriately Prevents senders from wasting bandwidth Reduces global congestion problems Data Transmission Full-Duplex byte stream 8/30/06 UIUC - CS/ECE 438, Fall 2006 15 8/30/06 UIUC - CS/ECE 438, Fall 2006 16 UDP Services UDP Services User Datagram Protocol Service OSI Transport Layer Provides a thin layer over IP 16-bit port space (distinct from TCP ports) allows multiple recipients on a single host Unit of Transfer Datagram (variable length packet) Unreliable No guaranteed delivery Drops packets silently Unordered No guarantee of maintained order of delivery Unlimited Transmission No flow control 8/30/06 UIUC - CS/ECE 438, Fall 2006 17 8/30/06 UIUC - CS/ECE 438, Fall 2006 18 3

Addresses and Data Byte Ordering Internet domain names Human readable Variable length Ex: sal.cs.uiuc.edu IP addresses Easily handled by routers/computers Fixed length Somewhat geographical Ex: 128.174.252.217 Big Endian vs. Little Endian Little Endian (Intel, DEC): Least significant byte of word is stored in the lowest memory address Big Endian (Sun, SGI, HP): Most significant byte of word is stored in the lowest memory address Network Byte Order = Big Endian Allows both sides to communicate Must be used for some data (i.e. IP Addresses) Good form for all binary data 8/30/06 UIUC - CS/ECE 438, Fall 2006 19 8/30/06 UIUC - CS/ECE 438, Fall 2006 20 Byte Ordering Functions Socket Address Structure 16- and 32-bit conversion functions (for platform independence) Examples: int m, n; short int s,t; m = ntohl (n) s = ntohs (t) n = htonl (m) t = htons (s) net-to-host long (32-bit) translation net-to-host short (16-bit) translation host-to-net long (32-bit) translation host-to-net short (16-bit) translation IP address: struct in_addr { in_addr_t s_addr; /* 32-bit IP address */ ; TCP or UDP address: struct sockaddr_in { short sin_family; /* e.g., AF_INET */ ushort sin_port; /* TCP/UDP port */ struct in_addr; /* IP address */ ; all but sin_family in network byte order 8/30/06 UIUC - CS/ECE 438, Fall 2006 21 8/30/06 UIUC - CS/ECE 438, Fall 2006 22 Address Access/Conversion Functions Structure: hostent All binary values are network byte ordered struct hostent* gethostbyname (const char* hostname); Translate English host name to IP address (uses DNS) struct hostent* gethostbyaddr (const char* addr, size_t len, int family); Translate IP address to English host name (not secure) char* inet_ntoa (struct in_addr inaddr); Translate IP address to ASCII dotted-decimal notation (e.g., 128.32.36.37 ) 8/30/06 UIUC - CS/ECE 438, Fall 2006 23 The hostent data structure (from /usr/include/netdb.h) canonical domain name and aliases list of addresses associated with machine also address type and length information struct hostent { char* h_name; /* official name of host */ char** h_aliases; /* NULL-terminated alias list */ int h_addrtype /* address type (AF_INET) */ int h_length; /* length of addresses (4B) */ char** h_addr_list; /* NULL-terminated address list */ #define h_addr h_addr_list[0];/* backward-compatibility */ ; 8/30/06 UIUC - CS/ECE 438, Fall 2006 24 4

Address Access/Conversion Functions Sockets API in_addr_t inet_addr (const char* strptr); Translate dotted-decimal notation to IP address; returns -1 on failure, thus cannot handle broadcast value 255.255.255.255 int inet_aton (const char *strptr, struct in_addr *inaddr); Translate dotted-decimal notation to IP address; returns 1 on success, 0 on failure int gethostname (char* name, size_t namelen); Read host s name (use with gethostbyname to find local IP) Basic Unix Concepts Creation and Setup Establishing a Connection (TCP) Sending and Receiving Data Tearing Down a Connection (TCP) Advanced Sockets 8/30/06 UIUC - CS/ECE 438, Fall 2006 25 8/30/06 UIUC - CS/ECE 438, Fall 2006 26 UDP Connection Example Functions: sendto sendto recvfrom close bind recvfrom sendto int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen); Send a datagram to another UDP. Returns number of bytes written or -1. Also sets errno on failure. sockfd: file descriptor (returned from ) 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 addrlen: length of address structure = sizeof (struct sockaddr_in) 8/30/06 UIUC - CS/ECE 438, Fall 2006 27 8/30/06 UIUC - CS/ECE 438, Fall 2006 28 Functions: recvfrom int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen); Read a datagram from a UDP. Returns number of bytes read (0 is valid) or -1. Also sets errno on failure. sockfd: file descriptor (returned from ) 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 (returned from call) addrlen: length of address structure = pointer to int set to sizeof (struct sockaddr_in) Socket Functions TCP () connect() write() Well-known port TCP three-way handshaking data (request) TCP Server () bind() listen() accept() blocks until connection from read() 8/30/06 UIUC - CS/ECE 438, Fall 2006 29 process request 8/30/06 UIUC - CS/ECE 438, Fall 2006 30 5

Socket Functions TCP Connection Example TCP () connect() write() TCP three-way handshaking data (request) blocks until connection from read() TCP Server connect bind listen accept read() data (reply) process request write() write read close read write read close() read() close close() 8/30/06 UIUC - CS/ECE 438, Fall 2006 31 8/30/06 UIUC - CS/ECE 438, Fall 2006 32 Socket Creation and Setup Include file <sys/.h> Create a int (int family, int type, int protocol); Returns file descriptor or -1. Bind a to a local IP address and port number int bind (int sockfd, struct sockaddr* myaddr, int addrlen); Put into passive state (wait for connections rather than initiate a connection). int listen (int sockfd, int backlog); Accept connections int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen); Returns file descriptor or -1. Functions: int (int family, int type, int protocol); Create a. 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 ), 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 8/30/06 UIUC - CS/ECE 438, Fall 2006 33 8/30/06 UIUC - CS/ECE 438, Fall 2006 34 Function: bind TCP and UDP Ports int bind (int sockfd, struct sockaddr* myaddr, int addrlen); Bind a to a local IP address and port number. Returns 0 on success, -1 and sets errno on failure. sockfd: file descriptor (returned from ) 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) Allocated and assigned by the Internet Assigned Numbers Authority see RFC 1700 or ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers 1-512 513-1023 1024-49151 49152-65535 standard services (see /etc/services) super-user only registered and controlled, also used for identity verification super-user only registered services/ephemeral ports private/ephemeral ports 8/30/06 UIUC - CS/ECE 438, Fall 2006 35 8/30/06 UIUC - CS/ECE 438, Fall 2006 36 6

Functions: listen int listen (int sockfd, int backlog); Put into passive state (wait for connections rather than initiate a connection). Returns 0 on success, -1 and sets errno on failure. sockfd: file descriptor (returned from ) backlog: bound on length of unaccepted connection queue (connection backlog); kernel will cap, thus better to set high Functions: accept int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen); Accept a new connection. Returns file descriptor or -1. Also sets errno on failure. sockfd: file descriptor (returned from ) cliaddr: IP address and port number of (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 s address (the number of bytes written) 8/30/06 UIUC - CS/ECE 438, Fall 2006 37 8/30/06 UIUC - CS/ECE 438, Fall 2006 38 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/.h> #include <sys/wait.h> #define PORT 3490 /* well-known port */ #define BACKLOG 10 /* how many pending connections queue will hold */ main() { 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 = (AF_INET, SOCK_STREAM, 0))==- 1){ perror(""); 8/30/06 UIUC - CS/ECE 438, Fall 2006 39 8/30/06 UIUC - CS/ECE 438, Fall 2006 40 bzero(&my_addr, sizeof(struct sockaddr)); /* zero the struct */ 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); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); 8/30/06 UIUC - CS/ECE 438, Fall 2006 41 if (listen(sockfd, BACKLOG) == -1) { perror("listen"); while(1) { /* main accept() loop */ sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr*) &their_addr,&sin_size)) == -1) { perror("accept"); continue; printf(": got connection from %s\n", inet_ntoa(their_addr.sin_addr)); 8/30/06 UIUC - CS/ECE 438, Fall 2006 42 7

Establishing a Connection Functions: connect Include file <sys/.h> int connect (int sockfd, struct sockaddr* servaddr, int addrlen); Connect to another. int connect (int sockfd, struct sockaddr* servaddr, int addrlen); Connect to another. Returns 0 on success, -1 and sets errno on failure. sockfd: file descriptor (returned from ) servaddr: IP address and port number of addrlen: length of address structure = sizeof (struct sockaddr_in) 8/30/06 UIUC - CS/ECE 438, Fall 2006 43 8/30/06 UIUC - CS/ECE 438, Fall 2006 44 if ((sockfd = (AF_INET, SOCK_STREAM, 0)) == -1) { perror ( ); 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 ); Sending and Receiving Data int write (int sockfd, char* buf, size_t nbytes); Write data to a stream (TCP) or connected datagram (UDP). Returns number of bytes written or -1. int read (int sockfd, char* buf, size_t nbytes); Read data from a stream (TCP) or connected datagram (UDP). Returns number of bytes read or -1. 8/30/06 UIUC - CS/ECE 438, Fall 2006 45 8/30/06 UIUC - CS/ECE 438, Fall 2006 46 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. 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. Returns number of bytes read or -1. Functions: write int write (int sockfd, char* buf, size_t nbytes); Write data to a stream (TCP) or connected datagram (UDP). Returns number of bytes written or -1. Also sets errno on failure. sockfd: file descriptor (returned from ) buf: data buffer nbytes: number of bytes to try to write Some reasons for failure or partial writes: process received interrupt or signal kernel resources unavailable (e.g., buffers) 8/30/06 UIUC - CS/ECE 438, Fall 2006 47 8/30/06 UIUC - CS/ECE 438, Fall 2006 48 8

Functions: read int read (int sockfd, char* buf, size_t nbytes); Read data from a stream (TCP) or connected datagram (UDP). Returns number of bytes read or -1. Also sets errno on failure. Returns 0 if closed. sockfd: file descriptor (returned from ) buf: data buffer nbytes: number of bytes to try to read Tearing Down a Connection int close (int sockfd); Close a. Returns 0 on success, -1 and sets errno on failure. int shutdown (int sockfd, int howto); Force termination of communication across a in one or both directions. Returns 0 on success, -1 and sets errno on failure. 8/30/06 UIUC - CS/ECE 438, Fall 2006 49 8/30/06 UIUC - CS/ECE 438, Fall 2006 50 Functions: close int close (int sockfd); Close a. Returns 0 on success, -1 and sets errno on failure. sockfd: file descriptor (returned from ) Closes communication on 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. Functions: shutdown int shutdown (int sockfd, int howto); Force termination of communication across a in one or both directions. Returns 0 on success, -1 and sets errno on failure. sockfd: file descriptor (returned from ) howto: SHUT_RD to stop reading SHUT_WR to stop writing SHUT_RDWR to stop both shutdown overrides the usual rules regarding duplicated s, in which TCP teardown does not occur until all copies have closed the. 8/30/06 UIUC - CS/ECE 438, Fall 2006 51 8/30/06 UIUC - CS/ECE 438, Fall 2006 52 Advanced Sockets Examples Managing Multiple Connections fork/exec: multiple processes pthread_create: multi-threaded process (no calls): event-based process Detecting Data Arrival select and poll functions Synchronous vs. Asynchronous Connections Other Socket Options 8/30/06 UIUC - CS/ECE 438, Fall 2006 53 Taken from Beej s Guide to Network Programming: http://beej.us/guide/bgnet/ -Server example using TCP For each forks new process to handle connection sends Hello, world 8/30/06 UIUC - CS/ECE 438, Fall 2006 54 9

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/.h> #include <sys/wait.h> #define PORT 3490 /* well-known port */ #define BACKLOG 10 /* how many pending connections queue will hold */ 8/30/06 UIUC - CS/ECE 438, Fall 2006 55 main() { 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 = (AF_INET, SOCK_STREAM, 0))==- 1){ perror(""); 8/30/06 UIUC - CS/ECE 438, Fall 2006 56 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 (w/o Beej s bug) */ bzero(&(my_addr.sin_zero), 8); /* zero the struct */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); 8/30/06 UIUC - CS/ECE 438, Fall 2006 57 if (listen(sockfd, BACKLOG) == -1) { perror("listen"); while(1) { /* main accept() loop */ sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr*) &their_addr,&sin_size)) == -1) { perror("accept"); continue; printf(": got connection from %s\n", inet_ntoa(their_addr.sin_addr)); 8/30/06 UIUC - CS/ECE 438, Fall 2006 58 if (!fork()) { /* this is the child process */ if (send(new_fd,"hello, world!\n", 14, 0) == -1) perror("send"); close(new_fd); exit(0); close(new_fd); /* parent doesn't need this */ /* clean up all child processes */ while(waitpid(-1,null,wnohang) > 0); #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/.h> #define PORT 3490 /* well-known port */ #define MAXDATASIZE 100 /* max number of bytes we can get at once */ 8/30/06 UIUC - CS/ECE 438, Fall 2006 59 8/30/06 UIUC - CS/ECE 438, Fall 2006 60 10

int main (int argc, char* argv[]){ int sockfd, numbytes; char buf[maxdatasize + 1]; struct hostent* he; struct sockaddr_in their_addr; /* connector s address information */ if (argc!= 2) { fprintf (stderr, usage: hostname\n ); if ((he = gethostbyname (argv[1])) == NULL) { /* get the host info */ perror ( gethostbyname ); if ((sockfd = (AF_INET, SOCK_STREAM, 0)) == -1) { perror ( ); 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 ); 8/30/06 UIUC - CS/ECE 438, Fall 2006 61 8/30/06 UIUC - CS/ECE 438, Fall 2006 62 if ((numbytes = recv (sockfd, buf, MAXDATASIZE, 0)) == -1) { perror ( recv ); buf[numbytes] = \0 ; printf ( Received: %s, buf); close (sockfd); return 0; 8/30/06 UIUC - CS/ECE 438, Fall 2006 63 11