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

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

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

Tutorial on Socket Programming

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

Socket Programming TCP UDP

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

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

TCP: Three-way handshake

Network programming(i) Lenuta Alboaie

Programming with TCP/IP. Ram Dantu

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

A Client-Server Exchange

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

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

System Programming. Sockets

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

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

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

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

CLIENT-SIDE PROGRAMMING

Client Server Computing

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

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

Unix Network Programming

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

CSE 124 Discussion Section Sockets Programming 10/10/17

Socket Programming for TCP and UDP

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Introduction to Socket Programming

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

CS321: Computer Networks Introduction to Application Layer

Elementary TCP Sockets

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

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

CS321: Computer Networks Socket Programming

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

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

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

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

CS4700/CS5700 Fundamentals of Computer Networking

Topics for this Week

CS 351 Week 15. Course Review

Communication. Sockets (Haviland Ch. 10)

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

Internet applications

Department of Computer Science

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

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

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

What s an API? Do we need standardization?

ECE 435 Network Engineering Lecture 2

CSE 333 SECTION 8. Sockets, Network Programming

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Networked Applications: Sockets. End System: Computer on the Net

EEC-484/584 Computer Networks

SOCKETS. COMP750 Distributed Systems

Lab 0. Yvan Petillot. Networks - Lab 0 1

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

CSE 333 SECTION 6. Networking and sockets

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

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CS 3516: Computer Networks

CS 640: Computer Networking

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

ICT 6544 Distributed Systems Lecture 5

Socket Programming(2/2)

Interprocess Communication

Group-A Assignment No. 6

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

The User Datagram Protocol

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

EECS122 Communications Networks Socket Programming. Jörn Altmann

CSE 333 SECTION 7. Client-Side Network Programming

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

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

Ch 7. Network Interface

ECE 435 Network Engineering Lecture 2

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

Application Architecture

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

Computer Networks Prof. Ashok K. Agrawala

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

CSE 333 Lecture network programming intro

CSE 333 Lecture 16 - network programming intro

Socket Programming 2007/03/28

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

Introduction to Socket Programming

STUDY OF SOCKET PROGRAMMING

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

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

Transcription:

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

Course Roadmap Slide 57 NM055 (11:00-12:00) Important Terms and Concepts TCP/IP and Client/Server Model Sockets and TLI Client/Server in TCP/IP NM056 (1:00-2:00) Socket Routines NM057 (2:00-3:00) Library Routines NM058 (3:00-4:00) Sample Client/Server NM059 (4:00-5:00) VMS specifics (QIOs) NM067 (6:00-7:00) Clinic - Q&A

TCP/IP Programming Slides and Source Code available via anonymous FTP: Host:: ftp.process.com Directory: [pub.decus] Slides: DECUS_F96_PROG.PS Examples: DECUS_F96_PROG_EXAMPLES.TXT Host: ftp.opus1.com Slides: DECUS_F96_PROG.PS Examples: DECUS_F96_PROG_EXAMPLES.TXT Slide 58

Programming with Sockets Berkeley Socket History Overview of the Socket Paradigm Socket addresses Other programming models Roadmap Slide 59

Berkeley Socket History Documented in such books as Stevens s Unix Network Programming Comer s Internetworking with TCP/IP, vol III First provided with Berkeley Software Distribution (BSD 4.1c) Unix for the VAX Popularized in the 1986 4.3BSD release Slide 60

Other Programming Models Socket model is widely used mainly due to wide implementation of BSD networking kernel Avaible for Unix, Windows, VMS,... Other models may be used in different environments STREAMS model (UNIX System V) Others Slide 61

Socket Paradigm Overview A socket is a communications endpoint In BSD networking, it is a data structure within the kernel A socket is named by its socket address A connection is represented by two communicating sockets Slide 62

Using sockets is like using the file system Server Client bind() listen() block until connection from client accept() read() write() connection establishment connect() write() read() close() close() Slide 63

Servers sit in a tight loop bind() listen() accept() read() write() close() Slide 64

Connectionless Client/Server is simpler Server Client bind() bind() block until data from client recvfrom() sendto() sendto() recvfrom() Slide 65

A socket is just a data structure socket Operating System Channel List Protocol Family Service Local Address Remote Address Slide 66

In C, a socket doesn t have much integer: always set to be PF_INET for TCP/IP programming sockaddr: a 2-octet address family identifier and then 14 octets of addressspecific information socket Protocol Family Service Local Address Remote Address integer: set to be SOCK_STREAM (TCP), SOCK_DGRAM (UDP), or SOCK_RAW (raw IP) Slide 67

Families and Types Address Families (or Protocol Families) AF_UNIX: AF_INET: AF_NS: AF_IMPLINK: Types: SOCK_STREAM: SOCK_DGRAM: SOCK_RAW: Unix domain sockets Internet Protocols Xerox NS Protocols IMP link layer (obsolete) Stream socket (TCP) Datagram socket (UDP) Raw socket (IP) Slide 68

TCP/IP addresses are special cases of sockaddr socket Protocol Family Service Local Address struct sockaddr { u_short sa_family; char sa_data[16]; } Remote Address Slide 69 sockaddr: a 2-octet address family identifier and then 14 octets of addressspecific information struct sockaddr_in { u_short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }

sockaddr unveiled struct sockaddr_in { u_short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; } sin_family: AF_INET for all TCP/IP addresses sin_port: 16-bit port number (as used in UDP & TCP headers) sin_addr: 32-bit IP address /* * Internet address (a structure for historical reasons) */ struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; #define s_addr S_un.S_addr Slide 70

Remember our goal: open() TCP/IP socket programming is mostly read() and write() subroutine calls All of the socket routines are there to do the equivalent of an open() in the file system. Slide 71

Five routines are used to replace the open() call socket Protocol Family Service Local Address Remote Address : allocate socket in memory, fill in protocol family and service fields bind() : fill in the local address part (local sockaddr). OPTIONAL for most clients listen() : tell network kernel that you want to receive connects ( passive open ) accept() : ask network kernel to hand you the next incoming connect ( passive open ) connect() : tell network kernel to connect to the other side ( active open ) Slide 72

Overview of Sockets (one more time) Get the socket open somehow, bind(), listen(), accept(), connect() Read and write from it read(), write() When done, close the socket close() Slide 73

Programming with Sockets Key Concepts A socket is a data structure representing a connection To open a connection Fill in the data structure Link it to the network kernel Link it to the operating system Reading and writing uses the same calls as the file system Slide 74

Coding with Sockets

General Flow for Servers/Clients is different Server Client bind() listen() block until connection from client accept() read() connection establishment connect() write() Slide 76 write() close() read() close() Let s start with clients because they re easier

Clients

Step 1: Get Socket connect() write() read() close() int socket(int domain, int type, int protocol); int s, domain, type, protocol; domain = AF_INET; /* always the same */ type = SOCK_STREAM; /* STREAM for TCP */ protocol = 0; s = socket(domain, type, protocol); if (s < 0) ERROR( Cannot create socket ); Slide 78

Step 2: Fill in remote address fields connect() write() read() close() #define SMTP_PORT 25 #define IP_ADDRESS 0xC0F50C02 /*192.245.12.2*/ struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(smtp_port); sin.sa_addr = htonl(ip_address); Slide 79

Step 3: Connect to other side connect() write() read() close() int connect(int s, struct sockaddr*name, int namelen); int status; status = connect(s, (struct sockaddr*)sin, sizeof(sin)); if (status!= 0) ERROR( Cannot connect to other side ); Slide 80

Step 4: Use the socket connect() write() read() close() char buf[linelen+1]; /* * Now go into a loop, reading data from the network, writing * it to the terminal and reading data from the terminal, * writing it to the network... */ while ( (n = read(s, buf, LINELEN) ) > 0) { fwrite(buf, n, 1, stdout); if (!fgets(buf, LINELEN, stdin)) break; write(s, buf, strlen(buf)); } if (n < 0) { ERROR( Cannot read from socket! ); } Slide 81

Step 5: Shut it down when done connect() write() status = close(s); if (status!= 0) ERROR( Can not close socket ); read() close() Slide 82

Servers

Server Review and Overview bind() listen() accept() read() write() close() : allocate socket in memory, fill in protocol family and service fields bind() : fill in the local address part (local sockaddr). OPTIONAL for most clients listen() : tell network kernel that you want to receive connects ( passive open ) accept() : ask network kernel to hand you the next incoming connect ( passive open ) Slide 84

Step 1: Get Socket bind() listen() accept() read() write() close() int socket(int domain, int type, int protocol); int s, domain, type, protocol; domain = PF_INET; /* always the same */ type = SOCK_STREAM; /* STREAM for TCP */ protocol = 0; s = socket(domain, type, protocol); if (s < 0) ERROR( Cannot create socket ); This should look vaguely familiar... Slide 85

Step 2: Fill in local address fields bind() listen() accept() read() write() close() #define SMTP_PORT 25 #define IP_ADDRESS 0xC0F50C02 /*192.245.12.2*/ struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(smtp_port); sin.sa_addr = INADDR_ANY; Slide 86 INADDR_ANY is a shorthand way of saying I don t care what the local address is...

Step 3: Bind address to the socket bind() listen() accept() read() write() close() int bind(int s, struct sockaddr*name, int namelen); int status; status = bind(s, (struct sockaddr*)sin, sizeof(sin)); if (status!= 0) ERROR( Cannot bind to local address ); Slide 87 INADDR_ANY is a shorthand way of saying I don t care what the local address is...

Step 4: Tell the kernel to listen bind() listen() accept() read() write() close() #define MAX_BACKLOG 5 int listen(int s, int backlog); int status; status = listen(s, MAX_BACKLOG); if (status!= 0) ERROR( Cannot ask kernel to listen ); Slide 88

Step 5: Block waiting for connect bind() listen() accept() read() write() close() int accept(int s, struct sockaddr *addr, int *addrlen); int status, addrlen, vs; struct sockaddr_in sin2; addrlen = sizeof(sin2); vs = accept(s, (struct sockaddr*)&sin2, &addrlen); if (vs < 0) ERROR( Cannot accept a connection. ); Slide 89

Step 6: Read and Write bind() listen() accept() read() write() /* Get the line from the client. */ read ( vs, buf, 256 ); /* Translate the logical name. */ log_name = getenv ( buf ); /* Get the definition string and add a new line to it. */ sprintf ( buf, "%s\n", log_name ); /* Write the translation to the client. */ write ( vs, buf, strlen ( buf ) ); close() Slide 90 Yes, this code has lots of holes in it. But you get the picture of what we re trying to do.

Step 7: Tear down when done bind() listen() status = close(s); if (status!= 0) ERROR( Can not close socket ); accept() read() write() close() Slide 91

Of course, there are more routines than those socket connect write read close bind listen accept recv, recvmsg recvfrom send, sendmsg sendto shutdown getpeername getsockopt setsockopt Create a descriptor for use in network communication Connect to a remote peer (client) Send outgoing data across a connection Acquire incoming data from a connection Terminate communication and deallocate a descriptor Bind a local IP address and protocol port to a socket Place the socket in passive mode and set backlog Accept the next incoming connection (server) Receive the next incoming datagram Receive the next incoming datagram and record source addr. Send an outgoing datagram Send an outgoing datagram to a particular dest. addr. Terminate a TCP connection in one or both directions After a connection arrives, obtain remote machine s address Obtain the current options for a socket Change the options for a socket Slide 92

Coding with Sockets Key Concepts All clients and servers basically look the same Follow a template, but make sure you understand what you re doing Lots of templates are wrong Lots of templates don t do what you think they do Slide 93