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



Similar documents
Network Programming TDC 561

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

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

Tutorial on Socket Programming

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

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

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

Network Programming with Sockets. Process Management in UNIX

TCP/IP - Socket Programming

Introduction to Computer Networks

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

Objectives of Lecture. Network Architecture. Protocols. Contents

Socket Programming. Srinidhi Varadarajan

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

Chapter 11. User Datagram Protocol (UDP)

Overview of TCP/IP. TCP/IP and Internet

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

Basic Operation & Management of TCP/IP Networks

Computer Networks/DV2 Lab

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

Overview of Computer Networks

Socket Programming in C/C++

Socket = an interface connection between two (dissimilar) pipes. OS provides this API to connect applications to networks. home.comcast.

ELEN 602: Computer Communications and Networking. Socket Programming Basics

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

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

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

Protocol Specification & Design. The Internet and its Protocols. Course Outline (trivia) Introduction to the Subject Teaching Methods

The TCP/IP Reference Model

PART OF THE PICTURE: The TCP/IP Communications Architecture

Ethernet. Ethernet. Network Devices

Network Models OSI vs. TCP/IP

Network Models and Protocols

E-Commerce Security. The Client-Side Vulnerabilities. Securing the Data Transaction LECTURE 7 (SECURITY)

Cross-platform TCP/IP Socket Programming in REXX

Internet Protocols. Background CHAPTER

Transport and Network Layer

Networking Basics and Network Security

Operating Systems Design 16. Networking: Sockets

How do I get to

Topics. Computer Networks. Let s Get Started! Computer Networks: Our Definition. How are Networks Used by Computers? Computer Network Components

CSIS CSIS 3230 Spring Networking, its all about the apps! Apps on the Edge. Application Architectures. Pure P2P Architecture

Lecture 28: Internet Protocols

Computer Networks - Xarxes de Computadors

Network Layers. CSC358 - Introduction to Computer Networks

Unix System Administration

Network Security TCP/IP Refresher

Packet Sniffing and Spoofing Lab

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

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

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

Computer Networks & Security 2014/2015

RARP: Reverse Address Resolution Protocol

Unit 4. Introduction to TCP/IP. Overview. Description. Unit Table of Contents

Introduction to TCP/IP

Application Architecture

Internetworking Microsoft TCP/IP on Microsoft Windows NT 4.0

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

Introduction to Socket programming using C

Layered Architectures and Applications

TCP/IP Protocol Suite. Marshal Miller Chris Chase

Slide 1 Introduction cnds@napier 1 Lecture 6 (Network Layer)

Chapter 5. Transport layer protocols

Protocols and Architecture. Protocol Architecture.

1 Data information is sent onto the network cable using which of the following? A Communication protocol B Data packet

A Heterogeneous Internetworking Model with Enhanced Management and Security Functions

finger, ftp, host, hostname, mesg, rcp, rlogin, rsh, scp, sftp, slogin, ssh, talk, telnet, users, w, walla, who, write,...

Technical Support Information Belkin internal use only

Distributed Systems. 2. Application Layer

IP Networking. Overview. Networks Impact Daily Life. IP Networking - Part 1. How Networks Impact Daily Life. How Networks Impact Daily Life

Application. Transport. Network. Data Link. Physical. Network Layers. Goal

Internet Concepts. What is a Network?

Design of a SIP Outbound Edge Proxy (EPSIP)

TCP/IP Networking An Example

Application Layer. CMPT Application Layer 1. Required Reading: Chapter 2 of the text book. Outline of Chapter 2

Internetworking. Problem: There is more than one network (heterogeneity & scale)

CS335 Sample Questions for Exam #2

CPS221 Lecture: Layered Network Architecture

TCP/IP Network Essentials. Linux System Administration and IP Services

Computer Networks CS321

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

Windows Sockets Network Programming

IP Addressing. -Internetworking (with TCP/IP) -Classful addressing -Subnetting and Supernetting -Classless addressing

You will work in groups of two on the labs. It is OK to talk to others and help each other in the lab.

Principles of Network Applications. Dr. Philip Cannata

How To Understand The Internet Of S (Netware)

Application-layer protocols

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

Transformation of honeypot raw data into structured data

Transport Layer. Chapter 3.4. Think about

Unix Security Features and TCP/IP Primer

Stateful Inspection Technology

Communications and Computer Networks

EKT 332/4 COMPUTER NETWORK

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

A network monitoring tool for student training

Chapter 3. Internet Applications and Network Programming

Transcription:

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

Part I: Socket Programming Overview Sockets (to me) Networking (or what s natural about natural logs) TCP/IP (and what it means to your life) More Sockets (we didn t get enough the first time) SAS Slide 2 Feb. 1998

What is Sockets An Application Programming Interface (API) used for InterProcess Communications (IPC). [A well defined method of connecting two processes, locally or across a network] Protocol and Language Independent Often referred to as Berkeley Sockets or BSD Sockets SAS Slide 3 Feb. 1998

Connections and Associations In Socket terms a connections between two processes in called an association. An association can be abstractly defined as a 5- tuple which specifies the two processes and a method of communication. For example: {protocol, local-addr, local-process, foreign-addr, foreign-process} A half-association is a single side of an association (a 3-tuple tuple) {protocol, addr, process} SAS Slide 4 Feb. 1998

Networking Terms packet - the smallest unit that can be transferred through the network by itself protocol - a set of rules and conventions between the communicating participants A collection of protocol layers is referred to as a protocol suite, protocol family or protocol stack. TCP/IP is one such protocol suite. SAS Slide 5 Feb. 1998

Introduction to TCP/IP What (the heck) is TCP/IP? Internet Protocol (IP) User Datagram Protocol (UDP) Transmission Control Protocol (TCP) TCP/IP Applications Name Resolution Processing TCP/IP Network Diagram SAS Slide 6 Feb. 1998

What is TCP/IP? Transmission Control Protocol/Internet Protocol A network protocol suite for interprocess communication The protocol of the Internet Open, nonproprietary Integrated into UNIX operating systems Many popular networking applications telnet NFS (network file system) X11 GUI SMTP (mail) www ftp (file transfer protocol) SAS Slide 7 Feb. 1998

TCP/IP Architectural Model Process (message) REXEC / SMTP / TELNET / FTP / DNS / RPC / Local Apps. Transport (message) TCP UDP Network (packets) ICMP IP (R)ARP Data Link (frames) Ethernet Token-Ring FDDI X.25 SNA Hyperchannel Proprietary SAS Slide 8 Feb. 1998

Internet Protocol (IP) Establishes a virtual network between hosts, independent of the underlying network topology Provides routing throughout the network, using IP addressing. For example: 149.173.70.9 Features Best-effort packet delivery Connectionless (stateless) Unreliable TCP IP UDP Physical Network SAS Slide 9 Feb. 1998

User Datagram Protocol (UDP) Application Interface to IP - Packet Oriented Establishes a port, which allows IP to distinguish among processes running on the same host Features resemble IP semantics Connectionless Unreliable Checksums (optional) TCP IP UDP Physical Network SAS Slide 10 Feb. 1998

Transmission Control Protocol (TCP) Connection-oriented Stream Data Transfer Reliable Flow-Control Full-Duplex TCP Suited for critical data transfer applications IP UDP Physical Network SAS Slide 11 Feb. 1998

SAS Slide 12 Feb. 1998 The Importance of Ports Both the TCP and UDP protocols use 16 bit identifiers called ports to uniquely identify the processes involved in a socket. In UNIX the first 1024 ports for both protocols are called well known ports and are defined in the file /etc/services. Programs that bind to these ports require root access. These numbers are managed by the Internet Assigned Numbers Authority (IANA). A complete list of these assignments and more information about IANA can be found in RFC 1700

How stuff gets around (routing) TCP/IP packets are routed based on their destination IP address (ex: 10.24.2.123) Packets are passed from one network segment to another by machines called routers until the packet arrives at the network segment attached to the host with the destination IP address. Routers that act as gates to larger networks are called gateways. SAS Slide 13 Feb. 1998

Name Resolution Processing Associates an IP address to a name (hostname) Structured method of identifying hosts within an internet The Domain Name System (DNS) implements a hierarchical naming scheme which maps names like mvs mvs.sas.com to an IP address DNS is implemented by a set of cooperating servers Machines that process DNS requests are called nameservers A set of library routines called the resolver provide the logic to query nameservers SAS Slide 14 Feb. 1998

TCP Ports TCP/UDP/IP Diagram 64K Dev1.sas.com (149.179.3.3) REXEC Server port 512 1023 Well-known Ports 0 0 Well-known Ports 1023 IP Routing REXEC client REXEC client Dev2.sas.com (149.179.83.6) UDP Ports 64K internet Server1.net.sas.com (149.193.2.194) NameServer SAS Slide 15 Feb. 1998

Back to Sockets Socket Definition and Components Socket Library Functions Primary Socket Header Files Sample Client/Server Dialog Ancillary Socket Topics Beyond Sockets SAS Slide 16 Feb. 1998

Definition and Components Socket - endpoint of communication Sockets - An application programming interface (API) for interprocess communication (IPC) Attributes: Protocol Independent Language Independent Sockets implies (not requires) TCP/IP and C Socket and Connection Association A local host can be identified by it s protocol, IP address and port. A connection adds the IP address & port of the remote host. SAS Slide 17 Feb. 1998

System calls SAS Slide 18 Feb. 1998 startup / close data transfer options control other Socket Library Function Network configuration lookup host address ports for services other Utility functions data conversion address manipulation error handling

socket() bind() listen() accept() Primary Socket Calls - create a new socket and return its descriptor - associate a socket with a port and address - establish queue for connection requests - accept a connection request - initiate a connection to a remote host connect() - initiate a connection to a remote host recv() - receive data from a socket descriptor send() - send data to a socket descriptor close() - one-way close of a socket descriptor SAS Slide 19 Feb. 1998

Network Database Administration functions gethostbyname - given a hostname, returns a structure which specifies its DNS name(s) and IP address(es es) getservbyname - given service name and protocol, returns a structure which specifies its name(s) and its port address gethostname - returns hostname of local host getservbyname, getservbyport, getservent getprotobyname, getprotobynumber, getprotobyent getnetbyname, getnetbyaddr, getnetent SAS Slide 20 Feb. 1998

Socket Utility Functions ntohs/ntohl ntohl - convert short/long from network byte order (big endian) ) to host byte order htons/htonl htonl - convert short/long from host byte order to network byte order inet_ntoa ntoa/inet_addr - convert 32-bit IP address (network byte order to/from a dotted decimal string) perror() - print error message (based on errno errno ) to stderr herror() - print error message for gethostbyname() to stderr (used with DNS) SAS Slide 21 Feb. 1998

Primary Header Files Include file sequence may affect processing (order is important!) <sys/types.h> - prerequisite typedefs <errno.h> - names for errno errno values (error numbers) - struct sockaddr; ; system prototypes and constants <sys/socket.h> <netdb.h.h> <netinet/in.h> <arpa/inet.h> - network info lookup prototypes and structures - struct sockaddr_in; byte ordering macros - utility function prototypes SAS Slide 22 Feb. 1998

Sample TCP Client / Server Session Iterative Server socket() bind() Remote Client socket() listen() gethostbyname() accept() connect() recv()/send() recv()/send() close() close() SAS Slide 23 Feb. 1998

Ancillary Socket Topics UDP versus TCP Controlling/managing socket characteristics get/setsockopt setsockopt() - keepalive,, reuse, nodelay fcntl() - async signals, blocking ioctl() - file, socket, routing, interface options Blocking versus Non-blocking socket Signal based socket programming (SIGIO) Implementation specific functions SAS Slide 24 Feb. 1998

Design Considerations Data representation and conversion Server design alternatives Security Issues Portability Considerations SAS Slide 25 Feb. 1998

Data Representation Transport Protocols detail data exchange/movement; applications must interpret the data! Byte order affects data - not just addresses Text is often sent in ASCII, but ASCII versus EBCDIC is decided by the application-level protocol Structure alignment and floating point pose problems External Data Representation (XDR) can be used (even without RPC) SAS Slide 26 Feb. 1998

SAS Slide 27 Feb. 1998 Single Threaded more complex code (must track multiple concurrent requests) generally lower system overhead crash of thread disables service Multi-Tasking less complex code (written only for handling only one connection) higher system overhead (each task requires it s own process space) highly crash resistant (one or more tasks can fail without losing service) [Multi-]Threaded Server Design Alternatives shares less complex code of Multi-Tasking model system overhead between Single-Threaded and Multi-Tasking model crash resistant (but one badly behaved thread can crash service)

Security Considerations Socket semantics do NOT address security problems, such as: IP and adapter addresses Userid and passwords data encryption traces UNIX systems require root privilege when a program binds a reserved (<1024) port getpeername() returns the peer s port and IP-address: determine privileged peers and trusted hosts The Kerberos protocol provides password and data encryption, along with service authentication SAS Slide 28 Feb. 1998

Limit applications to standard socket routines, BSD 4.x Implement a portable transport module Mainframe Environment - Distribute existing applications Portability Considerations API Programmer s Reference - Details SAS/C, C/370, Interlink,, Open Connect, NSC OS/2 - REXX Sockets, Programmer s Toolkit MS Windows Sockets 1.1-2 WINSOCK.DLL (http://www.stardust.com ftp.stardust.com:/pub/winsock winsock) SAS Slide 29 Feb. 1998

Summary Basic networking and features of TCP/IP protocols Socket library organization Socket library coding techniques Awareness of more advanced topics What s Next Session 5959 - Part II - Client/Server Application SAS Slide 30 Feb. 1998

Bibliography Internetworking with TCP/IP: Volumes I, II & III, Douglas Comer, Prentice Hall, 1991 (ISBN Vol I: 0134685059, Vol III: 0138487146) The Whole Internet User s Guide & Catalog by Ed Kroll; O Reilly & Associates UNIX Network Programming by W. Richard Stevens; Prentice Hall, 1990 (ISBN 0139498761) Socket API Programmer s Reference UNIX man pages TCP/IP Illustrated: Volumes 1 & 2, W. Richard Stevens (v2 with Gary R. Wright); Addison-Wesley Publishing Company, 1994 SAS Slide 31 Feb. 1998