90
Introduction to Unix Network Programming Reference: Stevens Unix Network Programming

Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

Introduction to Unix NetworkProgramming

Reference: Stevens UnixNetwork Programming

Page 2: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 2

How do we Communicate? Send a mail from Alice to Bob

Alice in Champaign, Bob in Hollywood

Example: US Postal Service

Bob

Champaign, Illinois

Hollywood, California

Alice

Page 3: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 3

What does Alice do?

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

Bob100 Santa Monica Blvd.Hollywood, CA 90028

Alice200 Cornfield Rd.Champaign, IL 61820

Page 4: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 4

What does Bob do?

Install a mailbox Receive the mail Get rid of envelope Read the message

Bob100 Santa Monica Blvd.Hollywood, CA 90028

Alice200 Cornfield Rd.Champaign, IL 61820

Page 5: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 5

What about sending a TCP/IPpacket?

Very similar to Alice-mailing-to-Bob Different terminologies – very

confusing We have to remember

Different technologies Suppose to be better (faster, more

reliable, cheaper, …)

Page 6: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 6

Two simplest networkingprograms

Alice the sending process Alice’s address:

128.174.246.177 (IP addr) Alice’s name: 12345 (port #)

Bob the receiving process Bob’s address:

216.52.167.132 (IP addr) Bob’s name: 23456 (port #)

int main () { int sockfd; struct sockaddr_in bob_addr, alice_addr;

bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456;

// do the same for alice_addr …

sockfd = socket(AF_INET, SOCK_DGRAM, 0); sendto(sockfd, “hi”, strlen(“hi”), 0, &bob_addr, sizeof(bob_addr));}

int main () { int sockfd, n; struct sockaddr_in bob_addr, alice_addr; char mesg[100];

bzero(&bob_addr, sizeof(bob_addr)); bob_addr.sin_family = AF_INET; bob_addr.sin_addr.s_addr = 0xD834A784; bob_addr.sin_port = 23456;

sockfd = socket(AF_INET, SOCK_DGRAM, 0); bind(sockfd, &bob_addr, sizeof(bob_addr));

n= recvfrom(sockfd, mesg, 100, 0, &alice_addr, sizeof(alice_addr));}

Page 7: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 7

What are the problems?

Message may be lost Network is congested Receiver is congested

Message may be duplicated, corrupted Multiple messages: re-ordered Concurrent connections …

Page 8: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 8

Direction and Principles

Physical

Transport

Data Link

Network

Page 9: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 8

Direction and Principles

Physical

Transport

Data Link

Network

Programming

learn to use Internet forcommunication (with focus onimplementation of networkingconcepts)

Page 10: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 8

Direction and Principles

Physical

Transport

Data Link

Network

Programming

learn to use Internet forcommunication (with focus onimplementation of networkingconcepts)

Principles andConcepts

learn to build network from groundup

Page 11: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 9

Sockets

process

TCP withbuffers,variables

socket

host orserver

process

TCP withbuffers,variables

socket

host orserver

Internet

controlled byapp developer

process sends/receivesmessages to/from itssocket

socket analogous tomailbox sending process relies

on transportinfrastructure whichbrings message tosocket at receivingprocess

Page 12: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 10

Network Programming withSockets

Page 13: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 10

Network Programming withSockets

Reading: Stevens 2nd ed., Ch. 1-6 or 1st ed., Ch. 1-3, 6

Page 14: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 10

Network Programming withSockets

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 sockets”

Page 15: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 11

Outline

Client-Sever Model TCP/UDP Overview Addresses and Data Sockets API Example

Page 16: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 12

Client-Server Model

Asymmetric Communication Client sends requests Server sends replies

Server/Daemon Well-known name (e.g., IP address +

port) Waits for contact Processes requests, sends replies

Client Initiates contact Waits for response

Client

Server

Client

Client

Client

Page 17: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 13

Socket Communication

Client process Server process

Clientsocket

3-wayhandshaking Listening

socket

Connectionsocket

Page 18: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 14

Client-Server CommunicationModel

Service Model Concurrent:

Server processes multiple clients’ requestssimultaneously

Sequential: Server processes only one client’s requests at a time

Hybrid: Server maintains multiple connections, but processes

responses sequentially Client and server categories are not disjoint

A server can be a client of another server A server can be a client of its own client

Page 19: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 15

TCP Connections

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

Page 20: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 16

TCP Service

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

Page 21: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 17

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 asingle host

Page 22: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 18

UDP Services

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

Page 23: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 19

Addresses and Data

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

Page 24: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 20

Byte Ordering

Big Endian vs. Little Endian Little Endian (Intel, DEC):

Least significant byte of word is stored in the lowestmemory 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

Page 25: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 21

Byte Ordering Functions

16- and 32-bit conversion functions (for platformindependence)

Examples:

int m, n;short int s,t;

m = ntohl (n) net-to-host long (32-bit) translations = ntohs (t) net-to-host short (16-bit) translationn = htonl (m) host-to-net long (32-bit) translationt = htons (s) host-to-net short (16-bit) translation

Page 26: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 22

Socket Address Structure

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

Page 27: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 23

Address Access/ConversionFunctions

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”)

Page 28: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 24

Structure: hostent

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 informationstruct 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 */};

Page 29: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 25

Address Access/ConversionFunctions

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, structin_addr *inaddr); Translate dotted-decimal notation to IP address; returns 1 on

success, 0 on failure

int gethostname (char* name, size_tnamelen); Read host’s name (use with gethostbyname to find local IP)

Page 30: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 26

Sockets API

Basic Unix Concepts Creation and Setup Establishing a Connection (TCP) Sending and Receiving Data Tearing Down a Connection (TCP) Advanced Sockets

Page 31: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

Page 32: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socket

Page 33: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socket

bind

Page 34: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

bind

Page 35: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

Page 36: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

Page 37: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

recvfrom

Page 38: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

recvfrom

sendto

Page 39: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

recvfrom

sendto

Page 40: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

recvfrom

sendto

recvfrom

Page 41: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 27

UDP Connection Example

client server

socketsocket

sendtobind

recvfrom

sendto

recvfrom

close

Page 42: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 28

Functions: sendto

int sendto (int sockfd, char* buf, size_t nbytes,int flags, struct sockaddr* destaddr, intaddrlen);

Send a datagram to another UDP socket. Returns number of bytes written or -1. Also 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)

Page 43: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 29

Functions: recvfrom

int recvfrom (int sockfd, char* buf, size_tnbytes, int flags, struct sockaddr* srcaddr,int* addrlen);

Read a datagram from a UDP socket. Returns number of bytes read (0 is valid) or -1. Also 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)

Page 44: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket FunctionsTCP Client

TCP Server

Page 45: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

TCP Client

TCP Server

Page 46: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

bind()Well-knownport

TCP Client

TCP Server

Page 47: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

bind()Well-knownport

TCP Client

TCP Server

Page 48: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()

bind()Well-knownport

TCP Client

TCP Server

Page 49: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()

bind()Well-knownport

blocks until connection from client

TCP Client

TCP Server

Page 50: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

TCP Client

TCP Server

Page 51: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()

TCP Client

TCP Server

Page 52: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()TCP three-way handshaking

TCP Client

TCP Server

Page 53: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()TCP three-way handshaking

read()

TCP Client

TCP Server

Page 54: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()

write()

TCP three-way handshaking

read()

TCP Client

TCP Server

Page 55: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()

write()

TCP three-way handshaking

data (request)read()

TCP Client

TCP Server

Page 56: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 30

Socket Functionssocket()

listen()

accept()socket()

bind()Well-knownport

blocks until connection from client

connect()

write()

TCP three-way handshaking

data (request)read()

process request

TCP Client

TCP Server

Page 57: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 31

Socket Functionssocket()

blocks until connection from client

connect()

write()

TCP three-way handshaking

data (request)read()

process request

TCP ServerTCP Client

write()

read()data (reply)

close() read()

close()

Page 58: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 32

TCP Connection Example

client server

socket

socket

connect

bind

listenaccept

write

readwrite

read

close

close

read

Page 59: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 33

Socket Creation and Setup Include file <sys/socket.h> Create a socket

int socket (int family, int type, int protocol);

Returns file descriptor or -1.

Bind a socket to a local IP address and port number int bind (int sockfd, struct sockaddr* myaddr, int

addrlen);

Put socket into passive state (wait for connections rather thaninitiate a connection). int listen (int sockfd, int backlog);

Accept connections int accept (int sockfd, struct sockaddr*

cliaddr, int* addrlen); Returns file descriptor or -1.

Page 60: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 34

Functions: socket

int socket (int family, int type, intprotocol);

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

Page 61: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 35

Function: bind

int bind (int sockfd, struct sockaddr*myaddr, int addrlen);

Bind a socket to a local IP address and portnumber. 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 bycaller

addrlen: length of address structure = sizeof (struct sockaddr_in)

Page 62: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 36

TCP and UDP Ports

Allocated and assigned by the Internet AssignedNumbers Authority see RFC 1700 orftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers

Page 63: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 36

TCP and UDP Ports

Allocated and assigned by the Internet AssignedNumbers Authority see RFC 1700 orftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers

private/ephemeral ports49152-65535

registered services/ephemeral ports1024-49151

registered and controlled, also used for identityverification super-user only

513-1023

standard services (see /etc/services) super-user only

1-512

Page 64: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 37

Functions: listen

int listen (int sockfd, int backlog);

Put socket into passive state (wait for connectionsrather 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 toset high

Page 65: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 38

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: 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 byteswritten)

Page 66: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 39

server

#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#define PORT 3490 /* well-known port */#define BACKLOG 10 /* how many pending

connections queue will hold */

Page 67: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 40

server

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 = socket(AF_INET, SOCK_STREAM, 0))==-1){

perror("socket");exit(1);

}

Page 68: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 41

server

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");exit(1);

}

Page 69: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 42

server

if (listen(sockfd, BACKLOG) == -1) {perror("listen");exit(1);

}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("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));

Page 70: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 43

Establishing a Connection

Include file <sys/socket.h>

int connect (int sockfd, structsockaddr* servaddr, int addrlen); Connect to another socket.

Page 71: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 44

Functions: connect

int connect (int sockfd, structsockaddr* 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)

Page 72: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 45

clientif ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {

perror (“socket”);exit (1);

}

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);

}

Page 73: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 46

Sending and Receiving Dataint write (int sockfd, char* buf, size_t

nbytes); Write data to a stream (TCP) or “connected”

datagram (UDP) socket. Returns number of bytes written or -1.

int read (int sockfd, char* buf, size_tnbytes); Read data from a stream (TCP) or “connected”

datagram (UDP) socket. Returns number of bytes read or -1.

Page 74: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 47

Sending and Receiving Data

int sendto (int sockfd, char* buf,size_t nbytes, int flags, structsockaddr* 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, structsockaddr* srcaddr, int* addrlen); Read a datagram from a UDP socket.

Returns number of bytes read or -1.

Page 75: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 48

Functions: write

int write (int sockfd, char* buf, size_tnbytes);

Write data to a stream (TCP) or “connected”datagram (UDP) socket. Returns number of bytes written or -1. Also sets errno on

failure. sockfd: socket file descriptor (returned from socket) 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)

Page 76: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 49

Functions: read

int read (int sockfd, char* buf, size_tnbytes);

Read data from a stream (TCP) or “connected”datagram (UDP) socket. Returns number of bytes read or -1. Also 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

Page 77: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 50

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.

Page 78: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 51

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.

Page 79: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 52

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 duplicatedsockets, in which TCP teardown does not occur until all copieshave closed the socket.

Page 80: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 53

Advanced Sockets

Managing Multiple Connections fork/exec: multiple server processes pthread_create: multi-threaded server

process (no calls): event-based server process

Detecting Data Arrival select and poll functions

Synchronous vs. AsynchronousConnections

Other Socket Options

Page 81: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 54

Examples

Taken from Beej’s Guide to NetworkProgramming:http://beej.us/guide/bgnet/

Client-Server example using TCP For each client

server forks new process to handleconnection

sends “Hello, world”

Page 82: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 55

server

#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/wait.h>#define PORT 3490 /* well-known port */#define BACKLOG 10 /* how many pending

connections queue will hold */

Page 83: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 56

server

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 = socket(AF_INET, SOCK_STREAM, 0))==-1){

perror("socket");exit(1);

}

Page 84: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 57

server

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");exit(1);

}

Page 85: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 58

server

if (listen(sockfd, BACKLOG) == -1) {perror("listen");exit(1);

}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("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));

Page 86: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 59

server

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);

}}

Page 87: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 60

client

#include <stdlib.h>#include <errno.h>#include <string.h>#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/socket.h>#define PORT 3490 /* well-known port */#define MAXDATASIZE 100 /* max number of bytes we

can get at once */

Page 88: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 61

clientint 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: client hostname\n”);exit (1);}if ((he = gethostbyname (argv[1])) == NULL) {

/* get the host info */perror (“gethostbyname”);exit (1);}

Page 89: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 62

clientif ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {

perror (“socket”);exit (1);

}

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);

}

Page 90: Introduction to Unix Network Programmingnikita/courses/ece438-fa06/slides/CS... · 2017. 10. 15. · Programming Reference: Stevens Unix Network Programming. 8/30/06 UIUC - CS/ECE

8/30/06 UIUC - CS/ECE 438, Fall 2006 63

client

if ((numbytes = recv (sockfd, buf, MAXDATASIZE, 0))== -1) {

perror (“recv”);exit (1);

}

buf[numbytes] = ‘\0’;printf (“Received: %s”, buf);close (sockfd);return 0;

}