Lecture 018

Network Programming

Hierarchy

Server and Client

Server and Client

Network Adapter on I/O Bus

Network Adapter on I/O Bus

Hierarchy of Network

Ethernet

Ethernet

Ethernet: many hosts are connected to one centralized hub by Ethernet cable

Bridged Eternet

Bridged Eternet

Bridged Ethernet:

internet

internet

Protocol

Protocol: send bits across incompatible LANs and WANs

routes

routes

Issues: (in computer networking)

Global IP Internet

TCP/IP Protocol Family

IP (Internet Protocol): naming scheme, unreliable delivery capability (datagrams)

UDP (Unreliable Datagram Protocol): real time

TCP (Transmission Control Protocol): crucial

interface

interface

IP Address

IP address example:

IPv4: 32-bit IPv6: 128-bit

IP Address:

Domain Naming System (DNS)

domain tree

domain tree

DNS: distributed database that map domain names to IP address

Connection:

Socket: an endpoint of a connection (IP:port)

Port: 16 bit integer to identify a process

service port
echo 7
ftp 21
ssh 22
smtp 25
http 80
minecraft 25565

ports

ports

ports and service

ports and service

Sockets (Interface)

sockets

sockets

sockets: Set of system-level functions used in conjunction with Unix I/O to build network applications.

Sample Program

complete transmission structure

complete transmission structure

client

client

server

server

APIs

socket structure

socket structure

IPv4 socket structure

IPv4 socket structure

getaddrinfo(char *host, char *port, struct addrinfo *hints, struct addrinfo *result): convert from name to address (allocate addinfo)

void freeaddrinfo(struct addrinfo *result): free linked list const char *gai_strerror(int errcode): return error message

struct addrinfo {
  int ai_flags;             /* Hints argument flags */
  int ai_family;            /* First arg to socket function */
  int ai_socktype;          /* Second arg to socket function */
  int ai_protocol;          /* Third arg to socket function */
  char *ai_canonname;       /* Canonical host name */
  size_t ai_addrlen;        /* Size of ai_addr struct */
  struct sockaddr *ai_addr; /* Ptr to socket address structure */
  struct addrinfo *ai_next; /* Ptr to next item in linked list */
};

Example:

getaddrinfo() example 1

getaddrinfo() example 1

getaddrinfo() example 2

getaddrinfo() example 2

int getnameinfo(const SA *sa, socklen_t salen, /* In: socket addr */
  char *host, size_t hostlen,                  /* Out: host */
  char *serv, size_t servlen,                  /* Out: service */
  int flags);                                  /* optional flags */

int socket(int domain, int type, int protocol)

int bind(int sockfd, SA *addr, socklen_t addrlen)

int listen(int sockfd, int backlog)

int accept(int listenfd, SA *addr, int *addrlen)

int connect(int clientfd, SA *addr, socklen_t addrlen)

connect and accept

connect and accept

See: Here for code

connection

connection

telnet

for testing web server (with no security)

Web Server

HTTP: HyperText Transfer Protocol

http

http

Example MIME (Multipurpose Internet Mail Extensions) types

HTTP responses:

Universal Resource Locator (URL):

HTTP Request

http request: request line + zero or more request header

Request line: <method> <uri> <version>

Request header (can be none): <header name>: <header data>

HTTP Response

http response: request line + zero or more request header and possibly content

Response line: <version> <status code> <status msg>

Response header (can be none): <header name>: <header data>

http transaction example

http transaction example

https transaction example

https transaction example

Sending Static Content

static content

static content

Sending Dynamic Content

dynamic roadmap

dynamic roadmap
Dynamic: any request GET /cgi-bin/env.pl HTTP/1.1

Common Gateway Interface (CGI)

CGI workflow

CGI workflow

more info:

Table of Content