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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

A Client-Server Exchange

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

CS 640: Computer Networking

Network Programming November 3, 2008

Elementary TCP Sockets

Socket Programming TCP UDP

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

Programming with TCP/IP. Ram Dantu

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CS 3516: Computer Networks

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

Tutorial on Socket Programming

CLIENT-SIDE PROGRAMMING

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

System Programming. Sockets

TCP: Three-way handshake

Introduction to Socket Programming

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

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

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

Department of Computer Science

CSE 333 SECTION 7. Client-Side Network Programming

Network Socket Programming - 2 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

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

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

CS321: Computer Networks Introduction to Application Layer

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

Network Socket Programming - 3 BUPT/QMUL

Unix Network Programming

CSE 333 SECTION 8. Sockets, Network Programming

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

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

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

CSE 124 Discussion Section Sockets Programming 10/10/17

Network Communication

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

Internetworking II: Network programming. April 20, 2000

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

CS321: Computer Networks Socket Programming

CSE 333 SECTION 6. Networking and sockets

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

Piotr Mielecki Ph. D.

Client software design

CSE 333 Lecture 16 - network programming intro

Application Programming Interfaces

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

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

Interprocess Communication Mechanisms

ICT 6544 Distributed Systems Lecture 5

C Structures in Practice

CS4700/CS5700 Fundamentals of Computer Networking

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

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

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

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

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

Interprocess Communication Mechanisms

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

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

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

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

Introduction to Client-Server Model

ECE 435 Network Engineering Lecture 2

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

CSE 333 Lecture network programming intro

Socket Programming for TCP and UDP

Network programming(i) Lenuta Alboaie

Internet applications

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

ECE 435 Network Engineering Lecture 2

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

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

Communication. Sockets (Haviland Ch. 10)

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

SOCKETS. COMP750 Distributed Systems

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

CSE 333 SECTION 7. Client-Side Network Programming

Data and Computer Communications. Tenth Edition by William Stallings

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

CSE 333 Lecture 8 - file and network I/O

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

Introduction to Socket Programming

Chapter 2 Applications and

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

The User Datagram Protocol

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

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

STUDY OF SOCKET PROGRAMMING

Distributed programming

Transcription:

1 Sockets Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University http://nyx.skku.ac.kr

Internet Connections (1) 2 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 -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>

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

Client-Server Model 4 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

Clients 5 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

Using Ports 6 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)

Servers 7 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

Sockets 8 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.

Sockets 9 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

Sockets 10 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()

Socket Address Structure 11 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) */ };

socket() 12 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) protocol specifies a particular protocol to be used with the socket.

connect() 13 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.

Echo Client (1) 14 #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); }

Echo Client (2) 15 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);

Exercise