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

Similar documents
Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

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

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

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

Network Programming November 3, 2008

Elementary TCP Sockets

A Client-Server Exchange

CS 640: Computer Networking

Concurrent Programming

CS 3516: Computer Networks

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Socket Programming TCP UDP

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

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Introduction to Client-Server Model

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

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

Programming with TCP/IP. Ram Dantu

Computer Systems Laboratory Sungkyunkwan University

Tutorial on Socket Programming

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduction to Socket Programming

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

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

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

Concurrent Programming

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

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

TCP: Three-way handshake

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

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

CS321: Computer Networks Introduction to Application Layer

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

Department of Computer Science

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

Network Programming / : Introduc2on to Computer Systems 21 st Lecture, April. 4, Instructors: Todd Mowry and Anthony Rowe

Unix Network Programming

Client Server Computing

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

System Programming. Sockets

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

Network Communication

Interprocess Communication Mechanisms

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Piotr Mielecki Ph. D.

Interprocess Communication Mechanisms

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

Network programming(i) Lenuta Alboaie

Network Programming /18-243: Introduc3on to Computer Systems 22 nd Lecture, 13 April Instructors: Bill Nace and Gregory Kesden

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

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

CSE 333 SECTION 8. Sockets, Network Programming

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

CS321: Computer Networks Socket Programming

CLIENT-SIDE PROGRAMMING

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

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

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

Group-A Assignment No. 6

Internetworking II: Network programming. April 20, 2000

CSE 333 SECTION 7. Client-Side Network Programming

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

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

Network Socket Programming - 2 BUPT/QMUL

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

UNIT IV- SOCKETS Part A

SOCKETS. COMP750 Distributed Systems

STUDY OF SOCKET PROGRAMMING

Application Programming Interfaces

CSE 333 Lecture 16 - network programming intro

CSE 124 Discussion Section Sockets Programming 10/10/17

Lab 0. Yvan Petillot. Networks - Lab 0 1

ECE 435 Network Engineering Lecture 2

Introduction to Socket Programming

CS4700/CS5700 Fundamentals of Computer Networking

ECE 435 Network Engineering Lecture 2

CMPSC 311- Introduction to Systems Programming Module: Network Programming

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

Communication. Sockets (Haviland Ch. 10)

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

ICT 6544 Distributed Systems Lecture 5

ECE 650 Systems Programming & Engineering. Spring 2018

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

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

Internet applications

CS307 Operating Systems Processes

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

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Transcription:

Sockets Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Dong-Yun Lee(dylee@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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

Introduction to PA #3 To implement mini shell A shell program executing following commands An application program that runs programs on behalf of the user Let s look together http://csl.skku.edu/uploads/sse2033s16/pa3.pdf PA #3 needs all the things that you learned before midterm 3

Introduction to PA #3 File I/O http://csl.skku.edu/uploads/sse2033s16/2-file.pdf Processes http://csl.skku.edu/uploads/sse2033s16/4-processes Signals IPC http://csl.skku.edu/uploads/sse2033s16/5-1signals http://csl.skku.edu/uploads/sse2033s16/6-ipc 4

Internet Connections (1) Connection Clients and servers communicate by sending streams of bytes over connections: Point-to-point, full-duplex, and reliable. A socket is an endpoint of a connection Socket address is an <IP address : port> pair A port is a 16-bit integer that identifies a process Ephemeral port: assigned automatically on client when client makes a connection request Well-known port: associated with some service provided by a server (e.g. port 80 is associated with web servers.) A connection is uniquely identified by the socket addresses of its endpoints (socket pair) <client IP:client port, server IP:server port> 5

Internet Connections (1) Connection Clients and servers communicate by sending streams of bytes over connections: Point-to-point, full-duplex, and reliable. A socket is an endpoint of a connection Socket address is an <IP address : port> pair A port is a 16-bit integer that identifies a process Ephemeral port: assigned automatically on client when client makes a connection request Well-known port: associated with some service provided by a server (e.g. port 80 is associated with web servers.) A connection is uniquely identified by the socket addresses of its endpoints (socket pair) <client IP:client port, server IP:server port> 6

Internet Connections (2) Client socket address 128.2.194.242:51213 Server socket address 208.216.181.15:80 Client Client host address 128.2.194.242 Connection socket pair (128.2.194.242:51213, 208.216.181.15:80) Server (port 80) Server host address 208.216.181.15 Note: 51213 is an ephemeral port allocated by the kernel Note: 80 is a well-known port associated with Web servers 7

Client-Server Model Most network application is based on the clientserver model: A server process and one or more client processes Clients and servers are processes running on hosts (can be the same or different hosts) Server manages some resource Server provides service by manipulating resource for clients 4. Client handles response Client process 1. Client sends request 3. Server sends response Server process 2. Server handles request Resource 8

Clients Examples of client programs Web browsers, ftp, telnet, ssh How does a client find the server? The IP address in the server socket address identifies the host. The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service Examples of well-known ports (cf. /etc/services) Port 21: ftp Port 23: telnet Port 25: mail Port 80: web 9

Using Ports Server host 128.2.194.242 Client host Client Service request for 128.2.194.242:80 (i.e., the Web server) Kernel Web server (port 80) Echo server (port 7) Client Service request for 128.2.194.242:7 (i.e., the echo server) Kernel Web server (port 80) Echo server (port 7) 10

Servers Servers are long-running processes (daemons) Created at boot-time (typically) by the init process (process 1) Run continuously until the machine is turned off. Each server waits for requests to arrive on a wellknown port associated with a particular service Port 21: ftp server Port 23: telnet server Port 25: mail server Port 80: HTTP server A machine that runs a server process is also often referred to as a server 11

Sockets (1) Sockets interface Introduced in BSD4.1 UNIX, 1981. Provides a user-level interface to the network. Explicitly created, used, released by applications. Based on client/server paradigm Two types of transport service Unreliable datagram Reliable, connection-oriented byte stream Underlying basis for all Internet applications 12

Sockets (2) What is a socket? A host-local, application-created/owned, OS-controlled interface to network (a door ) To the kernel, a socket is an endpoint of communication. To an application, a socket is a file descriptor.» Applications read/write from/to the network using the file descriptor.» Remember: All Unix I/O devices, including networks, are modeled as files. Clients and servers communicate with each by reading from and writing to socket descriptors. The main distinction between regular file I/O and socket I/O is how the application opens the socket descriptors. 13

Sockets (3) Hardware/Software organization of an Internet application Internet client host Internet server host Sockets interface (system calls) Client User code Server Hardware interface (interrupts) TCP/IP Network adapter Kernel code Hardware and firmware TCP/IP Network adapter Global IP Internet 14

Sockets (4) Connection-oriented service Server socket() Connectionless service Server socket() Client socket() connect() write() read() connection established request response bind() listen() accept() read() write() Client socket() bind() sendto() recvfrom() response request bind() recvfrom() sendto() 15

Socket Address Structure Generic socket address For address arguments to connect(), bind(), and accept() struct sockaddr { unsigned short sa_family; /* protocol family */ char sa_data[14]; /* address data. */ }; Internet-specific socket address Must cast (sockaddr_in *) to (sockaddr *) for connect(), bind(), and accept() struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ }; 16

socket() int socket (int family, int type, int protocol) socket() creates a socket descriptor. family specifies the protocol family. AF_UNIX: Local Unix domain protocols AF_INET: IPv4 Internet protocols type specifies the communication semantics. SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte streams SOCK_DGRAM: supports datagrams (connectionless, unreliable messages of a fixed maximum length) SOCK_RAW: provides raw network protocol access protocol specifies a particular protocol to be used with the socket. 17

connect() int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen) Used by a TCP client to establish a connection with a TCP server. servaddr contains <IP address, port number> of the server. The client does not have to call bind() before calling connect(). The kernel will choose both an ephemeral port and the source IP address if necessary. Client process suspends (blocks) until the connection is created. 18

Echo Client (1) #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, cfd; struct hostent *h; struct sockaddr_in saddr; char buf[maxline]; char *host = argv[1]; int port = atoi(argv[2]); if ((cfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf("socket() failed.\n"); exit(1); } 19

Echo Client (2) if ((h = gethostbyname(host)) == NULL) { printf("invalid hostname %s\n", host); exit(2); } bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; bcopy((char *)h->h_addr, (char *)&saddr.sin_addr.s_addr, h->h_length); saddr.sin_port = htons(port); } if (connect(cfd,(struct sockaddr *)&saddr,sizeof(saddr)) < 0) { printf("connect() failed.\n"); exit(3); } while ((n = read(0, buf, MAXLINE)) > 0) { write(cfd, buf, n); n = read(cfd, buf, MAXLINE); write(1, buf, n); } close(cfd); 20

bind() int bind (int sockfd, struct sockaddr *myaddr, socklen_t addrlen) bind() gives the socket sockfd the local address myaddr. myaddr is addrlen bytes long. Servers bind their well-known port when they start. If a TCP server binds a specific IP address to its socket, this restricts the socket to receive incoming client connections destined only to that IP address. Normally, a TCP client let the kernel choose an ephemeral port and a client IP address. 21

listen() int listen (int sockfd, int backlog) listen() converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests. When a socket is created, it is assumed to be an active socket, that is, a client socket that will issue a connect(). backlog specifies the maximum number of connections that the kernel should queue for this socket. Historically, a backlog of 5 was used, as that was the maximum value supported by 4.2BSD. Busy HTTP servers must specify a much larger backlog, and newer kernels must support larger values. 22

accept() (1) int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) accept() blocks waiting for a connection request. accept() returns a connected descriptor with the same properties as the listening descriptor. The kernel creates one connected socket for each client connection that is accepted. Returns when the connection between client and server is created and ready for I/O transfers. All I/O with the client will be done via the connected socket. The cliaddr and addrlen arguments are used to return the address of the connected peer process (the client) 23

accept() (2) Client clientfd listenfd(3) Server 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd. Client clientfd Connection request listenfd(3) Server 2. Client makes connection request by calling and blocking in connect. Client clientfd listenfd(3) Server connfd(4) 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd. 24

accept() (3) Listening descriptor End point for client connection requests Created once and exists for lifetime of the server Connected descriptor End point of the connection between client and server A new descriptor is created each time the server accepts a connection request from a client. Exists only as long as it takes to service client. Why the distinction? Allows for concurrent servers that can communicate over many client connections simultaneously. 25

Echo Server (1) #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <arpa/inet.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, listenfd, connfd, caddrlen; struct hostent *h; struct sockaddr_in saddr, caddr; char buf[maxline]; int port = atoi(argv[1]); if ((listenfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf("socket() failed.\n"); exit(1); } 26

Echo Server (2) bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(inaddr_any); saddr.sin_port = htons(port); if (bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { printf("bind() failed.\n"); exit(2); } if (listen(listenfd, 5) < 0) { printf("listen() failed.\n"); exit(3); } while (1) { caddrlen = sizeof(caddr); if ((connfd = accept(listenfd, (struct sockaddr *)&caddr, &caddrlen)) < 0) { printf ("accept() failed.\n"); continue; } 27

Echo Server (3) h = gethostbyaddr((const char *)&caddr.sin_addr.s_addr, sizeof(caddr.sin_addr.s_addr), AF_INET); printf("server connected to %s (%s)\n", h->h_name, inet_ntoa(*(struct in_addr *)&caddr.sin_addr)); // echo while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ("got %d bytes from client.\n", n); write(connfd, buf, n); } } } printf("connection terminated.\n"); close(connfd); 28

Echo Server (4) Client socket Server socket bind connect Connection request listen accept write read close EOF read write read Await connection request from next client close 29