Computer Network Programming. Intro to Sockets. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Similar documents
Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

Tutorial on Socket Programming

Unix Network Programming

Socket Programming. Srinidhi Varadarajan

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Porting applications & DNS issues. socket interface extensions for IPv6. Eva M. Castro. ecastro@dit.upm.es. dit. Porting applications & DNS issues UPM

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Introduction to Socket programming using C

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

Network Programming with Sockets. Process Management in UNIX

Implementing Network Software

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

The POSIX Socket API

Programmation Systèmes Cours 9 UNIX Domain Sockets

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

NS3 Lab 1 TCP/IP Network Programming in C

UNIX Sockets. COS 461 Precept 1

TCP/IP - Socket Programming

Socket Programming in C/C++

Lab 4: Socket Programming: netcat part

Computer Networks Network architecture

Networks. Inter-process Communication. Pipes. Inter-process Communication

VMCI Sockets Programming Guide VMware ESX/ESXi 4.x VMware Workstation 7.x VMware Server 2.0

Communication Networks. Introduction & Socket Programming Yuval Rochman

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Overview. Socket Programming. Using Ports to Identify Services. UNIX Socket API. Knowing What Port Number To Use. Socket: End Point of Communication

Networks class CS144 Introduction to Computer Networking Goal: Teach the concepts underlying networks Prerequisites:

Lecture 28: Internet Protocols

CSE 333 SECTION 6. Networking and sockets

DESIGN AND IMPLEMENT AND ONLINE EXERCISE FOR TEACHING AND DEVELOPMENT OF A SERVER USING SOCKET PROGRAMMING IN C

Programming guidelines on transition to IPv6

Operating Systems Design 16. Networking: Sockets

Review of Previous Lecture

Network Programming TDC 561

Transport Layer Protocols

Basic Networking Concepts. 1. Introduction 2. Protocols 3. Protocol Layers 4. Network Interconnection/Internet

What is CSG150 about? Fundamentals of Computer Networking. Course Outline. Lecture 1 Outline. Guevara Noubir noubir@ccs.neu.

Concurrent Server Design Alternatives

Application Architecture

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

Hostnames. HOSTS.TXT was a bottleneck. Once there was HOSTS.TXT. CSCE515 Computer Network Programming. Hierarchical Organization of DNS

Objectives of Lecture. Network Architecture. Protocols. Contents

UNIX. Sockets. mgr inż. Marcin Borkowski

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

System calls. Problem: How to access resources other than CPU

Chapter 3. Internet Applications and Network Programming

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

Chapter 11. User Datagram Protocol (UDP)

Computer Networks - Xarxes de Computadors

Transport and Network Layer

CPS221 Lecture: Layered Network Architecture

Protocols and Architecture. Protocol Architecture.

Transport Layer. Chapter 3.4. Think about

Lecture 7: Introduction to Sockets

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

TCP/IP Fundamentals. OSI Seven Layer Model & Seminar Outline

Writing a C-based Client/Server

Course Overview: Learn the essential skills needed to set up, configure, support, and troubleshoot your TCP/IP-based network.

TCP/IP Programming. Joel Snyder, Opus1 Geoff Bryant, Process Software

Networking Test 4 Study Guide

Cape Girardeau Career Center CISCO Networking Academy Bill Link, Instructor. 2.,,,, and are key services that ISPs can provide to all customers.

Introduction to Computer Networks

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

BSD Sockets Interface Programmer s Guide

Network Programming with Sockets. Anatomy of an Internet Connection

presentation DAD Distributed Applications Development Cristian Toma

Writing Client/Server Programs in C Using Sockets (A Tutorial) Part I. Session Greg Granger grgran@sas. sas.com. SAS/C & C++ Support

Building Applications With Sockets

Data Communication Networks. Lecture 1

Network Models and Protocols

Packet Sniffing and Spoofing Lab

ICOM : Computer Networks Chapter 6: The Transport Layer. By Dr Yi Qian Department of Electronic and Computer Engineering Fall 2006 UPRM

Programming with TCP/IP Best Practices

Ethernet. Ethernet. Network Devices

IBM i Version 7.2. Programming Socket programming IBM

Note! The problem set consists of two parts: Part I: The problem specifications pages Part II: The answer pages

Client / Server Programming with TCP/IP Sockets

Generalised Socket Addresses for Unix Squeak

41376 UDP performing get device status Command Workstation (CWS), Harmony, Bi-directional Driver TCP/UDP

Internet Concepts. What is a Network?

Indian Institute of Technology Kharagpur. TCP/IP Part I. Prof Indranil Sengupta Computer Science and Engineering Indian Institute of Technology

EITF25 Internet Techniques and Applications L5: Wide Area Networks (WAN) Stefan Höst

Understanding TCP/IP. Introduction. What is an Architectural Model? APPENDIX

KATRAGADDA INNOVATIVE TRUST FOR EDUCATION NETWORK PROGRAMMING. Notes prepared by D. Teja Santosh, Assistant Professor, KPES, Shabad, R.R. District.

Overview of TCP/IP. TCP/IP and Internet

Applications and Services. DNS (Domain Name System)

RARP: Reverse Address Resolution Protocol

8.2 The Internet Protocol

Virtual Server and DDNS. Virtual Server and DDNS. For BIPAC 741/743GE

Protocol Data Units and Encapsulation

The exam has 110 possible points, 10 of which are extra credit. There is a Word Bank on Page 8. Pages 7-8 can be removed from the exam.

IP - The Internet Protocol

Overview of Computer Networks

q Connection establishment (if connection-oriented) q Data transfer q Connection release (if conn-oriented) q Addressing the transport user

Lecture 21: Networking & Protocol Stacks

1 Introduction: Network Applications

TCP/IP and Encryption. CIT304 University of Sunderland Harry R. Erwin, PhD

Fundamentals of Symbian OS. Sockets. Copyright Symbian Software Ltd.

Mathatma Gandhi University

Transcription:

Computer Network Programming Intro to Sockets Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Intro to Sockets The Client/Server Model Layered Network Structure Sockets Internet Addressing Protocol Port Numbers Socket Programming Network Byte Order Connectionless/Connection-oriented Examples 2

The Client/Server Model Server response request Client Client Starts and initializes. Starts and initializes. Goes to sleep waiting for client to connect. When contacted by client, calls/creates a handler to handle. (Goes back to sleep: concurrent) Handles client request, sends back reply Closes client connection. (Goes back to sleep: iterative). Waits for user input. Upon receiving user request, contacts server, sends request on user s behalf. Waits for server reply, sends results to user, sends another user request Closes server connection. 3

Client/Server on an LAN 4

Client/Server via a WAN 5

OSI vs. Internet Protocol Layers 6

Protocol Data and Headers Socket Layer (Application) Socket Struct User Data TCP or UDP Layer (Transport) TCP/UDP Header TCP/UDP Data IP Layer (Internet/Network) IP Header IP Data Network Layer (Link) Frame Header Network Frame Data Frame Tail 7

What Is A Socket? (1/2) A socket is a communication end point. Is equivalent to a computer's network (hardware) interface. Allows a network application to "plug into" the network (not physically, but metaphorically). 8

What Is A Socket? (2/2) Is a network programming interface. It is used for interprocess communication over the network. It is used by a process to communicate with a remote system via a transport protocol. It needs an IP address and a port number. 9

Sockets Came From Berkeley UNIX Sockets were first introduced in Berkeley UNIX. An extension of the UNIX abstraction of file I/O concepts. Now are commonly supported in almost all modern operating systems for intersystems communications. 10

Popular in Client/Server Computing Sockets are popularly used in client/server computing. Provides two major types of services: Connection-oriented Connectionless 11

Connection-Oriented Services (1/2) Implemented on TCP Short for Transmission Control Protocol. A connection-oriented protocol. Data transfer unit is known as segment. An end-to-end connection is established before data exchange can happen. Similar to our phone system. 12

Connection-Oriented Services (2/2) Data bytes are delivered in sequence. Delivery of data is guaranteed. Connection is terminated when finished. There are two modes: Iterative (synchronous) Concurrent (asynchronous) 13

Connectionless Services (1/2) Implemented on UDP Short for User Datagram Protocol. A connectionless protocol. Data transfer unit is known as datagram. No connection is required before data transfer. Similar to our postal system. 14

Connectionless Services (2/2) Data bytes may be missing or delivered out-of-order. There are also two modes: Iterative (synchronous) Concurrent (asynchronous) 15

Sockets Are Bi-directional A socket provides a bi-directional communication mechanism. Two way simultaneously. Also know as full duplex (FDX) communication. 16

Internet Addressing A means to identify hosts on the Internet. There are two popular ways: Using IP addresses. Using the domain name system (DNS). 17

IP Addresses (1/2) IP is short for Internet Protocol. Each host on the Internet is assigned a 32-bit unique address (in current IPv4). An IP address is assigned to a single host only. A host may have more than one IP address (multi-homed host). 18

IP Addresses (2/2) Dotted representation Internet addresses are represented in the form of four integers separated by decimal points known as dotted representation. Examples: 131.91.96.108 131.91.128.73 For readability by human. 19

The Domain Name System (1/2) A high-level naming scheme A sequence of characters grouped into sections delimited by decimal points. Labeled in a meaningful way. Examples: earth.cse.fau.edu www.fau.edu 20

The Domain Name System (2/2) The DNS naming convention is hierarchical. Written in the local-most level first and the top-most level last fashion. It is much easier to deal with DNS names than with IP addresses. 21

Mapping DNS to IP Addresses Delivery of information across the Internet is done by using IP addresses. Need to map DNS names to IP addresses before delivery. Three ways: Done at system startup. Via a local table lookup. Going through a nameserver 22

Port Numbers A (protocol) port is an abstraction used by TCP/UDP to distinguish applications on a given host. A port is identified by a 16-bit integer known as the port number. Three ranges of port numbers: Well-known ports Registered ports Dynamic ports 23

Well-known Ports Port numbers ranging from 0 to 1,023. A set of pre-assigned port numbers for specific uses. Port numbers are managed by ICANN. Short for the Internet Corporation for Assigned Names and Numbers (ICANN) Used to be controlled solely by IANA (Internet Assigned Numbers Authority). 24

Some Well-known Ports Port Keyword Description 0 Reserved 7 ECHO Echoes a received datagram to the sender 13 DAYTIME Returns the date and the time 20 FTP-DATA File Transfer Protocol (data) 21 FTP File Transfer Protocol (control) 22 SSH Secure Shell 23 TELNET Terminal Connection 25 SMTP Simple Mail Transport Protocol 53 DNS Domain Name Server 67 BOOTP Bootstrap Protocol 79 FINGER Finger 80 HTTP HyperText Transfer Protocol 101 HOSTNAME NIC Host Name Server 103 X400 X.400 Mail Service 110 POP3 Post Office Protocol Vers. 3 25

Registered Ports Port numbers ranging from 1,024 to 49,151. Not assigned or controlled by ICANN; however their uses need to be registered via an ICANN-accredited registrar to prevent duplications. 26

Dynamic Ports Port numbers ranging from 49,152 to 65,535. Neither assigned or registered. They can be used by anyone. These are ephemeral ports. Also known as private ports. 27

Socket Programming To use a socket, one needs a structure to hold address and its associated port number information. A generic socket format: (address family, address in the family) Another name for family is domain. 28

Generic Socket Address Structure struct sockaddr { sa_family_t sa_family; /* address family */ char sa_data[14]; /* socket address */ } Note: This generic socket structure is primarily for declaring variables. cast is needed in the actual use of a socket address structure. 29

A Popular BSD-derived Socket Implementation (1/3) struct sockaddr_in { sa_family_t sin_family; /* address family: AF_XXX */ in_port_t sin_port; /* 16-bit protocol port number */ struct in_addr sin_addr; /* IP addr in NW byte order */ char sin_zero[8]; /* unused, set to zero */ } Note: One may encounter PF_XXX occasionally. It is the same as AF_XXX at present, but is expected to be phased out later. 30

A Popular BSD-derived Socket Implementation (2/3) Where sa_family_t sin_family usually holds the value either AF_INET or AF_UNIX. in_port_t sin_port is a 16-bit TCP or UDP port number. In need of htons() to convert to network byte order. 31

A Popular BSD-derived Socket Implementation (3/3) in_addr sin_addr contains a 32-bit IPv4 address. Structure for in_addr: struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* in network byte order */ } In need of htonl() to convert to network byte order. 32

Network Byte Order Different systems may store numbers in different byte orders internally. For example, Sparc machines is big-endian, and i386 is little-endian. Taking 1 as an example, MSB LSB Big-endian: Little-endian 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 Network byte order uses big-endian ordering. 33

Socket Types Family SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET SOCK_RAW Description stream socket (TCP) datagram socket (UDP) sequenced packet socket (SCTP) raw socket (talk to IP directly) 34

Two Examples A connectionless example Algorithms for server and client. An implementation in C. A connection-oriented example Algorithms for server and client. An implementation in C. 35

Example 1: Connectionless To illustrate a simple connectionless client/server example. One server (iterative), multiple clients. The server echoes back requests from clients, one client request at a time. A client sends user request to server and displays response received from server. Programs: echo_seru.c & echo_cliu.c It is implemented on UDP. 36

Server Algorithm (connectionless) a) Create a socket. b) Bind to a predefined address for the service desired. c) Wait for a datagram to arrive from a client. d) Send response back to originating client. e) Return to c) for next client. 37

Client Algorithm (connectionless) a) Create a socket. b) Send a datagram to server. c) Wait for response from server. d) Return to b) for more datagrams. e) Close the socket when no more datagram to send. 38

Example 2: Connection-oriented To illustrate a simple connection-oriented client/server example. Similar to the previous one: The server echoes back requests from clients, and a client displays server response. However, a connection is established before data exchange can happen. Programs: echo_ser.c & echo_cli.c It is implemented on TCP. 39

Server Algorithm (connection-oriented) a) Create a socket. b) Bind to a predefined address for the service desired. c) Place the socket in passive mode. d) Accept the next connection request from a client. e) Read a request, process the request, and send back the results. f) Close the connection when done with a client. g) Return to d) for next client. 40

Client Algorithm (connection-oriented) a) Create a socket. b) Connect the socket to the desired server. c) Send a request, and wait for the response. d) Repeat c) until done. e) Notify server of intention to terminate. May close R/W end either separately or together at the same time. f) Close the socket (connection) when done. 41

Connectionless: Functions Used Server response request Client Client socket() socket() bind() bind() optional recvfrom() sendto() blocks sendto() recvfrom() blocks 42

Connection-oriented: Functions Used Server response request Client Client socket() socket() bind() connect() listen() blocks accept() blocks read() write() write() read() close() close() 43

R&W on a Closed TCP Socket In a TCP connection, a write to a disconnected socket will generate SIGPIPE. This can be dealt with a proper signal handler. A read from socket will return 0 if the socket is closed. 44

A close() Call for TCP/UDP If the socket is for SOCK_STREAM The kernel will deliver all data received before terminating the connection. close() will block until all outstanding data delivered to the receiving process. If the socket is for SOCK_DGRAM The socket is closed immediately. 45

TCP Client/Server Socket Functions Create a socket TCP Server socket() Assign IP addr/port # to the socket bind() Establish a queue for connections listen() TCP Client socket() connect() write() Initiate a connection Connection establishment (TCP 3-way handshake) Data (request) accept() Blocks until a connection request from client read() Get a connection from the queue (may fork a child) read() close() Data (reply) EOF notification write() read() Ref: UNP, Stevens et al, vol1, ed 3, 2004, AW, p. 96 close() Connection closed for one client 46

UDP Client/Server Socket Functions Create a socket UDP Server socket() Assign IP addr/port # to the socket bind() UDP Client socket() sendto() Send datagram to server, with the client's address data (request) recvfrom() blocks until datagram arrives from client Receive datagram from client, with the client's address recvfrom() Receive datagram from server, with the server's address data (reply) Send datagram to client, with the server's address sendto() close() Ref: UNP, Stevens et. al, vol 1, ed 3, 2004, AW, p. 240 47

Some Relevant Socket System Calls and Header Files int socket(int family, int type, int protocol); int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int listen(int sockfd, int backlog); int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *cliaddrlen); int connect(int sockfd, struct sockaddr *servaddr, socklen_t *servaddrlen); ssize_t recvfrom(int sockfd, void *buff, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); ssize_t sendto(int sockfd, void *msg, size_t len, int flags, struct sockaddr *to, socklen_t tolen); struct hostent gethostbyname(const char *name); int shutdown(int sockfd, int howto); <sys/socket.h> <netinet/in.h> <netdb.h> 48

Some References To download textbook source code http://www.unpbook.com/src.html A tutorial on Networking Programming using Sockets http://beej.us/guide/bgnet/output/print/bgnet_usletter.pdf Networking Programming FAQs http://www.faqs.org/faqs/unix-faq/socket/ http://www.uni-giessen.de/faq/archiv/unixfaq.socket/msg00000.html Some coding examples http://www.xcf.berkeley.edu/~ali/k0d/unix/networking/ 49

Reading Assignment Scan Chapters 1, 4, and 8 50