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

Similar documents
The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 6. Networking and sockets

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

TCP. Networked Systems (H) Lecture 13

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSE 333 Section 8 - Client-Side Networking

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

CSE 333 Lecture 16 - network programming intro

last time redo logging copy-on-write filesystems / snapshots distributed systems motivation, etc.

Computer Network Programming

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

CS 3516: Computer Networks

UNIX System Programming Lecture 19: IP Sockets

UNIX Network Programming. Overview of Socket API Network Programming Basics

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 333 Lecture network programming intro

CSE 124 Discussion Section Sockets Programming 10/10/17

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

System Programming. Sockets

ECE 435 Network Engineering Lecture 2

Network programming(i) Lenuta Alboaie

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

Introduction to Berkeley Sockets

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

Tutorial on Socket Programming

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

Elementary TCP Sockets

Client-side Networking

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

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

Category: Informational J. Bound J. McCann Hewlett-Packard W. Stevens February 2003

CLIENT-SIDE PROGRAMMING

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

Introduction to Berkeley Sockets

Introduction to Berkeley Sockets

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

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Unix Network Programming

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

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

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

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

Socket Programming TCP UDP

Programming with TCP/IP. Ram Dantu

Socket Programming for TCP and UDP

IPv6 Porting Applications

CSE 333 Lecture 8 - file and network I/O

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

Azblink API for Sending XMPP Messages via HTTP POST

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

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

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

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

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

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

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

Group-A Assignment No. 6

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

A Client-Server Exchange

TCP: Three-way handshake

C Structures in Practice

Introduction to Computer Networks

Network Socket Programming - 3 BUPT/QMUL

TCP Network Programming in C

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

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

Network Socket Programming - 3 BUPT/QMUL

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

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

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Introduction to Client-Server Model

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

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

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

CS 640: Computer Networking

ECE 435 Network Engineering Lecture 2

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

2007 Microsoft Corporation. All rights reserved.

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

Server-side Programming

CSCI 4061: Sockets and Network Programming

Application Programming Interfaces

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

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

CMPSC 311- Introduction to Systems Programming Module: Network Programming

Sockets 15H2. Inshik Song

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

IBM i Version 7.3. Programming Socket programming IBM

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

Lecture 7 Overview. IPv6 Source: Chapter 12 of Stevens book Chapter 31 of Comer s book

Transcription:

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

The Berkeley Sockets API Widely used low-level C networking API First introduced in 4.3BSD Unix Now available on most platforms: Linux, MacOS X, Windows, FreeBSD, Solaris, etc. Largely compatible cross-platform Recommended reading: Stevens, Fenner, and Rudoff, Unix Network Programming volume 1: The Sockets Networking API, 3rd Edition, Addison-Wesley, 2003. 2

Concepts Application Socket Network Sockets provide a standard interface between network and application Two types of socket: Stream provides a virtual circuit service Datagram delivers individual packets Independent of network type: Commonly used with TCP/IP and UDP/IP, but not specific to the Internet protocols Only discuss TCP/IP sockets today 3

What is a TCP/IP Connection? A reliable byte-stream connection between two computers Most commonly used in a client-server fashion: The server listens on a well-known port The port is a 16-bit number used to distinguish servers E.g. web server listens on port 80, email server on port 25 The client connects to that port Once connection is established, either side can write data into the connection, where it becomes available for the other side to read The Sockets API represents the connection using a file descriptor 4

TCP/IP Connection Client Server fd fd connfd Socket Network? Socket int fd = socket() int fd = socket() bind(fd,, ) listen(fd, ) connect(fd,, ) connfd = accept(fd, ) write(fd, data, datalen) read(connfd, buffer, buflen) read(fd, buffer, buflen) write(connfd, data, datalen) close(fd) 5 close(connfd)

TCP/IP Connection Server fd = socket( ); Client bind(fd, ); Specify well-known port fd = socket( ); listen(fd, ); Begin listening connect(fd, ); write(fd, ); read(fd, ); TCP/IP connection established Send request Wait for response connfd = accept(fd, ); read(connfd, ); write(connfd, ); Block until connection established close(fd, ); TCP/IP connection shutdown read(connfd, ); EOF read 6 close(connfd, );

Creating a socket #include <sys/socket.h> <sys/types.h> #include <sys/socket.h> int fd; fd = socket(family, type, protocol); if (fd == -1) { // Error: unable to create socket AF_INET for IPv4 AF_INET6 for IPv6 SOCK_STREAM for TCP SOCK_DGRAM for UDP 0 (not used for Internet sockets) Create an unbound socket, not connected to network; can be used as either a client or a server 7

Handling Errors Socket functions return -1 and set the global integer variable errno on failure fd = socket(family, type, protocol); if (fd == -1) { // Error occurred; look at // errno to determine what // to do. The Unix man pages list possible errors that can occur for each function E.g. do man 2 socket in a terminal, and read the ERRORS section 8

Binding a Server Socket Bind a socket to a port on a network interface Needed to run servers on a well-known port with addr specified as INADDR_ANY Not generally used on clients, since typically don t care which port used #include <sys/types.h> #include <sys/socket.h> if (bind(fd, addr, addrlen) == -1) { // Error: unable to bind 9

Listening for Connections #include <sys/types.h> #include <sys/socket.h> if (listen(fd, backlog) == -1) { // Error Tell the socket to listen for new connections The backlog is the maximum number of connections the socket will queue up, each waiting to be accept() ed 10

Connecting to a Server #include <sys/types.h> #include <sys/socket.h> if (connect(fd, addr, addrlen) == -1) { // Error: unable to open connection Pointer to a struct sockaddr Size of the struct in bytes Tries to open a connection to the server Times out after 75 seconds if no response 11

Specifying Addresses & Ports Must specify the address and port when calling bind() or connect() The address can be either IPv4 or IPv6 Could be modelled in C as a union, but the designers of the sockets API chose to use a number of structs, and abuse casting instead 12

struct sockaddr Addresses specified via struct sockaddr Has a data field big enough to hold the largest address of any family Plus sa_len and sa_family to specify the length and type of the address Treats the address as an opaque binary string struct sockaddr { uint8_t sa_family_t char ; sa_len; sa_family; sa_data[22]; 13

struct sockaddr_in Two variations exist for IPv4 and IPv6 addresses Use struct sockaddr_in to hold an IPv4 address Has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address struct in_addr { in_addr_t ; s_addr; struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_pad[16]; ; 14

struct sockaddr_in6 Two variations exist for IPv4 and IPv6 addresses Use struct sockaddr_in6 to hold an IPv6 address Has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address struct in6_addr { uint8_t s6_addr[16]; ; struct sockaddr_in6 { uint8_t sin6_len; sa_family_t sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct in6_addr sin6_addr; ; 15

Working with Addresses Work with either struct sockaddr_in or struct sockaddr_in6 Cast it to a struct sockaddr before calling the socket routines struct sockaddr_in addr; // Fill in addr here if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { 16

Creating an Address: Manually (Client) #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> inet_pton() to convert address htons() to convert port struct sockaddr_in addr; inet_pton(af_inet, 130.209.240.1, &addr.sin_addr); addr.sin_family = AF_INET; addr.sin_port = htons(80); if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 17

Creating an Address: Manually (Server) #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> Usually specify INADDR_ANY htons() to convert port struct sockaddr_in addr; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_family = AF_INET; addr.sin_port = htons(80); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 18

Creating an Address: DNS Prefer using DNS names to raw IP addresses Use getaddrinfo() to look-up name in DNS Returns a linked list of struct addrinfo values, representing addresses of the host struct addrinfo { int ai_flags; // input flags int ai_family; // AF_INET, AF_INET6, int ai_socktype; // IPPROTO_TCP, IPPROTO_UDP int ai_protocol; // SOCK_STREAM, SOCK_DRAM, socklen_t ai_addrlen; // length of socket-address struct sockaddr *ai_addr; // socket-address for socket char *ai_canonname; // canonical name of host struct addrinfo *ai_next; // pointer to next in list ; 19

Connecting via a DNS Query struct addrinfo int i; hints, *ai, *ai0; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((i = getaddrinfo( www.google.com, "80", &hints, &ai0))!= 0) { printf("unable to look up IP address: %s", gai_strerror(i)); for (ai = ai0; ai!= NULL; ai = ai->ai_next) { fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd == -1) { perror("unable to create socket"); continue; if (connect(fd, ai->ai_addr, ai->ai_addrlen) == -1) { perror("unable to connect"); close(fd); continue; success, use the connection break; if (ai == NULL) { // Connection failed, handle the failure 20

Accepting Connections #include <sys/types.h> #include <sys/socket.h> int connfd; struct sockaddr_in cliaddr; socklen_t cliaddrlen = sizeof(cliaddr); connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen); if (connfd == -1) { // Error Accepts a connection, returns new file descriptor for the connection (connfd) and client address (cliaddr) 21

Accepting Connections A TCP/IP server may have multiple connections outstanding Can accept() connections one at a time, handling each request in series Can accept() connections and start a new thread for each, allowing it to process several in parallel Each call to accept() returns a new file descriptor 22

Reading and Writing Data #define BUFLEN 1500 ssize_t i; ssize_t rcount; char buf[buflen]; rcount = read(fd, buf, BUFLEN); if (rcount == -1) { // Error has occurred for (i = 0; i < rcount; i++) { printf( %c, buf[i]); Read up to BUFLEN bytes of data from connection; blocks until data available Returns actual number of bytes read, or -1 on error Data is not null terminated 23

Handling Multiple Sockets #include <sys/select.h> int fd1, fd2; fd_set rfds; struct timeval timeout; timeout.tv_sec = 1; // 1 second timeout timeout.tv_usec = 0; FD_ZERO(&rfds); FD_SET(fd1, &rfds); FD_SET(fd2, &rfds); The select() call tells you which of a group of sockets has data available to read int rc = select(max(fd1, fd2) + 1, &rfds, NULL, NULL, &timeout); if (rc == 0) // timeout if (rc > 0) { if (FD_ISSET(fd1, &rfds)) { // Data available to read on fd1 if (FD_ISSET(fd2, &rfds)) { // Data available to read on fd2 if (rc < 0) // error 24

Reading and Writing Data char data[] = Hello, world! ; int datalen = strlen(data); if (write(fd, data, datalen) == -1) { // Error has occurred Send data on a TCP/IP connection; blocks until all data can be written Returns actual number of bytes written, or -1 on error 25

Reading and Writing Data #include <stdio.h> #include <stdlib.h> #include <string.h> What gets printed? int main() { char x[] = "Hello, world!"; char *y = malloc(14); sprintf(y, "Hello, world!"); printf("x = %s\n", x); printf("y = %s\n", y); printf("sizeof(x) = %d\n", sizeof(x)); printf("sizeof(y) = %d\n", sizeof(y)); Why? printf("strlen(x) = %d\n", strlen(x)); printf("strlen(y) = %d\n", strlen(y)); return 0; 26

Closing a Socket #include <unistd.h> close(fd); Close and destroy a socket Close the file descriptor for each connection, then the file descriptor for the underlying socket 27

Questions? 28