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

Similar documents
UNIX System Programming Lecture 19: IP Sockets

CS321: Computer Networks Socket Programming

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

Client Server Computing

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

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

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

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 2007/03/28

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

Tutorial on Socket Programming

Socket Programming for TCP and UDP

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

CSE 124 Discussion Section Sockets Programming 10/10/17

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

Overview. Setup and Preparation. Exercise 0: Four-way handshake

Programming with TCP/IP. Ram Dantu

Unix Network Programming

Elementary TCP Sockets

Network Software Implementations

A Client-Server Exchange

Socket Programming TCP UDP

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

CS 640: Computer Networking

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

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

UNIX Sockets. COS 461 Precept 1

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

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

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

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

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

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

Network Socket Programming - 3 BUPT/QMUL

Introduction to Socket Programming

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

CS307 Operating Systems Processes

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

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

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

Department of Computer Science

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

Network Socket Programming - 3 BUPT/QMUL

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

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

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

ECE 650 Systems Programming & Engineering. Spring 2018

CLIENT-SIDE PROGRAMMING

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

Network Programming November 3, 2008

System Programming. Sockets

CS 3516: Computer Networks

TCP: Three-way handshake

System Programming. Sockets: examples

ECE 435 Network Engineering Lecture 2

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

What s an API? Do we need standardization?

Network Programming Appendix Network Programming Labs

Εργαστήριο 9 I/O Multiplexing

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

Network programming(i) Lenuta Alboaie

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

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

CSC209 Review. Yeah! We made it!

Introduction to Client-Server Model

Ken French HELP Session 1 CS4514

Sockets 15H2. Inshik Song

Application Architecture

Introduction to Berkeley Sockets

SOCKETS. COMP750 Distributed Systems

Introduction to Berkeley Sockets

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

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

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

ECE 435 Network Engineering Lecture 2

Linux Network Programming, Part 1

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

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

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

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

TCP Network Programming in C

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

Transcription:

Lecture 24 Log into Linux. Copy directory /home/hwang/cs375/lecture24 Final project posted. Due during finals week. Reminder: No class next Tuesday (11/24) Questions? Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Outline Internet (IP) sockets Concurrent servers Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 2

IP or Internet sockets can be used to communicate between two processes on two different machines connected by a network. (Or for IPC on a single machine.) An IP socket is an address/port-number pair and uniquely identifies an endpoint. A IP socket pair consists of both the server and client sockets. Every TCP connection is uniquely associated with a socket pair. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 3

Client 198.69.10.2 client1 Server 206.62.226.35 server Client 102.43.83.204 client1 198.69.10.2:1500 206.62.226.35:21 server (child1) (*:21, *.*) server (child2) 102.43.83.204:1501 206.62.226.35:21 client2 198.69.10.2:1501 206.62.226.35:21 206.62.226.35:21 198.69.10.2:1500 server (child3) 206.62.226.35:21 198.69.10.2:1501 206.62.226.35:21 102.43.83.204:1501 Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 4

Just as for local sockets, the server will call socket( ), bind( ), listen( ), and accept( ). First call socket( ) to create the socket: sfd = socket(domain, type, protocol); The domain parameter is now PF_INET. type is usually either SOCK_DGRAM (UDP) or SOCK_STREAM (TCP). (We will use SOCK_STREAM.) The protocol should be 0. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 5

bind( ) is used to assign an address (TCP port) to the socket. Address family is now AF_INET. struct sockaddr_in in_addr; // network struct sockaddr *p_addr = (struct sockaddr *)(&in_addr); in_addr.sin_family = AF_INET; in_addr.sin_port = htons(2400); in_addr.sin_addr.s_addr = htonl(inaddr_any); int len = sizeof(struct sockaddr_in); bind(sfd, p_addr, len); Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 6

On the server, we bind our socket to a particular IP address and port number. An IP address of INADDR_ANY will bind to all local IP addresses. We could instead bind to a single IP address on a single interface. Our server will bind to port 2400 in this example. Note the use of the htonl( ) and htons( ) routines. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 7

Instead of binding to a particular port, if we specify a port number of 0 in the address we will be assigned an available port number dynamically. The getsockname( ) routine can then be used to find the port number: getsockname(sfd, p_addr, &addrlen); cout << Port #: << ntohs(in_addr.sin_port) << endl; Note the use of ntohs( ). Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 8

The server then uses listen( ) and accept( ) just as for UNIX sockets. result = listen(sfd, qlength); nfd = accept(sfd, p_addr, &len); The p_addr parameter is a pointer to a sockaddr structure (that is really of type sockaddr_in). This will be filled with connecting client info (IP address and port number). Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 9

On the client we need only socket( ) and connect( ). We use the server IP and port numbers in the address: cfd = socket(pf_inet, SOCK_STREAM, 0); struct sockaddr_in in_address; struct sockaddr *address = (struct sockaddr *)(&in_address); in_address.sun_family = AF_INET; in_address.sin_port = htons(2400); in_address.sin_addr.s_addr = inet_addr("10.10.0.9"); // csserver int len = sizeof(struct sockaddr_in); res = connect(cfd, address, len); Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 10

In this example inet_addr( ) was used to convert an IP number from dotted-quad to network byte order. Typically the IP number would be obtained via getaddrinfo( ). Since our socket file descriptor is not bound to a local endpoint, connect( ) will do this for us. This is usually what we want. You could use bind( ) to bind to a particular local port number before the call to connect( ). Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 11

read( ) and write( ) can be used to send data via the socket. There also are recv( ) and send( ) routines that can used and often are recommended because they provide more options via their flags parameter. Consult the man pages, if you want to use these. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 12

Examples network_server.cpp is an example server. It is similar to the local_server.cpp from last lecture but is modified to use a TCP/IP socket instead of a local (UNIX) socket. It illustrates dynamic port assignment and the use of getsockname( ). network_client.cpp is a similarly modified version of local_client.cpp. It requires two command-line arguments, an IP address (in dotted-quad notation) and a port number. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 13

Concurrent Server Skeleton code for a concurrent server... listenfd = socket(... ); bind( listenfd,... ); listen(listenfd,... ); while ( true ) { connfd = accept( listenfd,... ); if( (pid = fork()) == 0) { close( listenfd ); } doit(connfd); // or exec(...) close( connfd ); // if not exec'd exit(0); } close( connfd ); // parent Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 14

Concurrent Server It is important that the parent call close( ) for each connected socket returned by accept( ). This ensures that the parent does not run out of file descriptors, but more importantly it ensures that when the child closes the connected socket the client connection will be terminated. (This does not happen until the open reference count on the socket reaches zero.) Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 15

Avoiding Zombies As noted previously, the parent of an exiting process must issue a wait on the process for it to terminate completely. We do not want the main server process to have to issue these waits for the processes handling client requests. Typically, in order to avoid creating zombie processes, the doublefork technique covered earlier is used. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 16