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

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

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Unix Network Programming

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

Department of Computer Science

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

CS321: Computer Networks Socket Programming

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

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CS321: Computer Networks Introduction to Application Layer

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

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

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

Socket Programming TCP UDP

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

CS 640: Computer Networking

Socket Programming for TCP and UDP

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

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

Socket Programming 2007/03/28

Tutorial on Socket Programming

TCP: Three-way handshake

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

Programming with TCP/IP. Ram Dantu

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

Elementary TCP Sockets

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

Topics for this Week

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

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

The User Datagram Protocol

Sockets 15H2. Inshik Song

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

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

Introduction to Client-Server Model

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

Introduction to Socket Programming

Group-A Assignment No. 6

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

ICT 6544 Distributed Systems Lecture 5

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

CS307 Operating Systems Processes

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

UDP CONNECT TO A SERVER

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

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

STUDY OF SOCKET PROGRAMMING

Internetworking II: Network programming. April 20, 2000

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

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

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

Applications and Layered Architectures. Chapter 2 Communication Networks Leon-Garcia, Widjaja

UNIX Sockets. COS 461 Precept 1

A Client-Server Exchange

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

BSD Sockets API. Mesut Ali ERGIN. Yeditepe University Information and Computer Science Department

Network Software Implementations

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

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

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

Sockets Sockets Communication domains

System Programming. Sockets

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

Piotr Mielecki Ph. D.

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

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

A GENERALIZED FRAMEWORK FOR CATV TRANSMISSION ON FUTURE BISDN

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

Network Programming and Protocl Design. Nixu Ltd.

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

Introduction to Socket Programming

CLIENT-SIDE PROGRAMMING

Network Socket Programming - 3 BUPT/QMUL

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

Network programming(i) Lenuta Alboaie

CSE 124 Discussion Section Sockets Programming 10/10/17

CS 3516: Computer Networks

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

vsockets Programming Guide 17 APR 2018 VMware ESXi 6.7 VMware Workstation 14

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

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

Network Socket Programming - 3 BUPT/QMUL

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

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

What s an API? Do we need standardization?

System Programming. Sockets: examples

Client Server Computing

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

UNIX System Programming Lecture 19: IP Sockets

EEC-484/584 Computer Networks

Sockets. UNIX-style IPC. Silberschatz, Galvin and Gagne 2005 Msc. Ivan A. Escobar Broitman 2007

Transcription:

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1 Work Individually (no groups) Due Date: in class, Monday, September 19 Robert T Olsen olsen@cswiscedu 7390CS Office Hours: 3-5T, 11-12F - exception for PhD Qualifying Examinees due date: September 30 Other notes - everyone is now authorized to register Interprocess Communication (IPC) Interprocess Communication (IPC) Goal: How: - write programs that communicate with each other - use sockets: abstract endpoints of communication : host : process : host IP Internet : process : socket - examples: telnet, rlogin, ftp, Mosaic

Interprocess Communication (IPC) Creating Sockets Issues - creating sockets - naming (identifying) sockets - sending/receiving data over sockets int socket(domain, type, protocol) int domain, type, protocol - creates and endpoint for communication Mechanisms - UNIX system calls and library routines (functions called from C programs) check all return codes if ((code = syscall()) < 0) { perror("syscall"); - include files: <sys/types>, <sys/socket> - return value: an integer descriptor (like a file descriptor) used in future calls, or -1 on error Types of Sockets 3) protocol: specifies particular protocol socket defined by 3 parameters 1) domain: address family (protocol family) determines address structure usually already defined by domain and type (eg, TCP for AF_INET and SOCK_STREAM, UDP for SOCK_DGRAM) you will use 0 (default protocol) eg, AF_UNIX, AF_INET, AF_OSI (also PF_UNIX, PF_INET, PF_OSI) you will use AF_INET Example if ((sd = socket(af_inet,sock_dgram,0)) < 0){ perror("socket"); 2) type: style of communication eg, SOCK_DGRAM provides unreliable, connectionless service eg, SOCK_STREAM provides reliable byte-stream service you will use SOCK_DGRAM

Naming Sockets Socket Addresses int bind(sd, name, namelen) struct sockaddr *name; int namelen; - several types of socket addresses - you will use sockaddr_in, because you use AF_INET - assigns a name to a socket - include files: <sys/types>, <sys/socket>, <netinet/inh> - return value: 0 on success, -1 on error - sockaddr_in is a C structure with 3 important fields: sin_family, sin_addr, sin_port sin_family determines remaining address structure (eg, AF_INET) sin_addr identifies host sin_port identifies process on host Internet Addresses and Ports sin_addr values - four bytes (abcd) - eg, sun22 is 1281054022 (see /etc/hosts) Example (binding local address) struct sockaddr_in addr; bzero((char *) &addr, sizeof(addr)); addrsin_family = AF_INET; addrsin_port = htons(0); addrsin_addrs_addr = htonl(inaddr_any); if (bind(sd, (struct sockaddr *) &addr, sizeof(addr)) < 0){ perror("bind"); - if you specify INADDR_ANY for local host address, all host addresses apply sin_port values - 0-1024 reserved for system - well known ports are important finger is port 79, rlogin is 513 (see /etc/services) - if you specify 0, the system picks a port

Sending/Receiving Data (SOCK_DGRAM) Sending/Receiving Data (SOCK_DGRAM) int sendto(sd, msg, len, flags, to, tolen)) char *msg; int len, flags; struct sockaddr *to; int tolen; - return value: number of bytes sent, or -1 on error int recvfrom(sd, buf, len, flags, from, fromlen)) char *buf; int len, flags; struct sockaddr *from; int *tolen; - return value: number of bytes received, Key Points: or -1 on error - must have created a socket with descriptor sd and bound local address to it, if a reply is desired - must know destinations address and port and have filled to structure appropriately - flags parameter will be 0 for you Key Points: - must have created a socket with descriptor sd and bound appropriate address to it - source address is filled in the from structure - flags parameter will be 0 for you - by default, call blocks until data arrives * Example of client using UDP protocol #include <sys/types> #include <sys/socket> #include <netinet/inh> main(argc, argv) int argc; char *argv[]; { int sockfd; struct sockaddr_in cli_addr, serv_addr; * Fill in the structure "serv_addr" with the address of the * server that we want to send to bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addrsin_family = AF_INET; serv_addrsin_addrs_addr = inet_addr(serv_host_addr); serv_addrsin_port = htons(serv_udp_port); * Open a UDP socket (an Internet datagram socket) if ( (sockfd = socket(af_inet, SOCK_DGRAM, 0)) < 0) { perror("client: cannot open socket"); exit(1) * Bind any local address for us bzero((char *) &cli_addr, sizeof(cli_addr)); zero out cli_addrsin_family = AF_INET; cli_addrsin_addrs_addr = htonl(inaddr_any); cli_addrsin_port = htons(0); if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) { perror("client: cannot bind local address"); * Example of server using UDP protocol #include <sys/types> #include <sys/socket> #include <netinet/inh> main(argc, argv) int argc; char *argv[]; { int sockfd; struct sockaddr_in serv_addr, cli_addr; * Open a UDP socket (an Internet datagram socket) if ( (sockfd = socket(af_inet, SOCK_DGRAM, 0)) < 0) { perror("server: cannot open socket"); exit(1) * Bind our local address so that the client can send to us bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addrsin_family = AF_INET; serv_addrsin_addrs_addr = htonl(inaddr_any); serv_addrsin_port = htons(serv_udp_port); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("server: cannot bind local address"); while (1) { n = recvfrom(sockfd, msg, msgsize, 0, &cli_addr, &clilen); if (n < 0) { perror("server: recvfrom"); if (sendto(sockfd, msg, len, 0, &serv_addr, sizeof(serv_addr)!= len) { perror("client: sendto"); n = recvfrom(sockfd, buf, bufsize, 0, (struct sockaddr *) 0, (int *) 0); if (n < 0) { perror("client: recvfrom"); if (sendto(sockfd, msg, n, 0, &cli_addr, clilen)!= n) perror("server: sendto"); close(sockfd); exit(0);

Other System Calls int connect(sd, name, namelen) struct sockaddr *name; int namelen; - specifies peer with which sd is to be associated int listen(sd, backlog) int sd, backlog; - specifies maximum backlog of connections a server will allow int close(sd) - deletes descriptor from system tables int select(width, rfds, wfds, efds, timeout) int width; fd_set *rfds, *wfds, *efds; struct timeval *timeout; - allows server to wait for data on multiple sockets at the same time - can be blocking, non-blocking, or timeout int accept(sd, addr, addrlen) struct sockaddr *addr; int *addrlen - extracts first connection off queue of pending connections Library Routines htonl(), htons(), ntohl(), ntohs() struct hostent *gethostbyname(name) char *name; - returns pointer to structure from which host address can be obtained int gethostname(name, namelen) char *name; int namelen; - returns name of current processor int getsockname(sd, name, namelen) - convert between network byte order and host byte order - required since some machines are big endian and others are little endian (network order is big endian) - big endian address byte 0 0 1 2 3 4 4 5 6 7 - little endian address byte 0 3 2 1 0 4 7 6 5 4 - convert to network order before sending, struct sockaddr *name; int *namelen; and back to host order after receiving - returns address structure for sd (can get port information, etc)

How to Get Help Unix Programmer s Manual (man pages) - man function-name Document located on Web Server (Berkeley UNIX System Calls and IPC) TA s (e-mail or office hours)