SOCKET PROGRAMMING. UNIX Programming 2014 Fall by Euiseong Seo

Similar documents
Tutorial on Socket Programming

Network Programming with Sockets. Anatomy of an Internet Connection

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

Unix Network Programming

Introduction to Socket programming using C

Implementing Network Software

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

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

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

TCP/IP - Socket Programming

Network Programming with Sockets. Process Management in UNIX

Socket Programming. Srinidhi Varadarajan

NS3 Lab 1 TCP/IP Network Programming in C

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

The POSIX Socket API

Programmation Systèmes Cours 9 UNIX Domain Sockets

UNIX Sockets. COS 461 Precept 1

A Client-Server Transaction. Systemprogrammering 2006 Föreläsning 8 Network Programming. Hardware Org of a Network Host.

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

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Computer Networks Network architecture

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

Lab 4: Socket Programming: netcat part

CSE 333 SECTION 6. Networking and sockets

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

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

Concurrent Server Design Alternatives

Application Architecture

Lecture 7: Introduction to Sockets

Writing a C-based Client/Server

BSD Sockets Interface Programmer s Guide

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

Socket Programming in C/C++

UNIX. Sockets. mgr inż. Marcin Borkowski

Communication Networks. Introduction & Socket Programming Yuval Rochman

Windows Socket Programming & IPv6 Translation Middleware

Elementary Name and Address. Conversions

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

IBM i Version 7.2. Programming Socket programming IBM

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

Review of Previous Lecture

Operating Systems Design 16. Networking: Sockets

Elementary Name and Address Conversions

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

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

Programming guidelines on transition to IPv6

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

A Client Server Transaction. Introduction to Computer Systems /18 243, fall th Lecture, Nov. 3 rd

Network Programming. Chapter The Client-Server Programming Model

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

This tutorial has been designed for everyone interested in learning the data exchange features of Unix Sockets.

Carnegie Mellon. Internetworking : Introduc0on to Computer Systems 19 th Lecture, Oct. 28, Instructors: Randy Bryant and Dave O Hallaron

Generalised Socket Addresses for Unix Squeak

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

Java Programming: Sockets in Java

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

Name and Address Conversions

Domain Name System (1)! gethostbyname (2)! gethostbyaddr (2)!

Client / Server Programming with TCP/IP Sockets

Data Communication Networks. Lecture 1

An Introductory 4.4BSD Interprocess Communication Tutorial

Revision: 1.0 PROGRAMMER S GUIDE Date: Jul 1, 10. CrossCore XA. Programmer's guide.

TCP/IP Sockets in C. Practical Guide for Programmers

Packet Sniffing and Spoofing Lab

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

RPG Does TCP/IP (Socket Progamming in RPG IV)

Chapter 11. User Datagram Protocol (UDP)

Goal: learn how to build client/server application that communicate using sockets. An interface between application and network

IPv6 Enabling CIFS/SMB Applications

Giving credit where credit is due

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Programming with TCP/IP Best Practices

Network Programming using sockets

Building Applications With Sockets

transmission media and network topologies client/server architecture layers, protocols, and sockets

Network Programming TDC 561

An Overview of IPv6 CHAPTER

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

sys socketcall: Network systems calls on Linux

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

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.

VMCI Sockets Programming Guide VMware ESXi 5.5 VMware Workstation 9.x

Internetworking / : Introduc2on to Computer Systems 20 th Lecture, Nov. 6, 2012

CS162 Operating Systems and Systems Programming Lecture 4. Introduction to I/O (Continued), Sockets, Networking. Recall: Fork and Wait

Objectives of Lecture. Network Architecture. Protocols. Contents

Interprocess Communication Message Passing

Chapter 3. Internet Applications and Network Programming

Best practices in IPv6 enabled networking software development.

CPS221 Lecture: Layered Network Architecture

15-441: Computer Networks Homework 1

Computer Networks - Xarxes de Computadors

Lecture 9: Network Security Introduction

How To Write On A Non-Blocking File On A Thumbtongue (Windows) (Windows 2) (Macintosh) (Amd64) (Apple) (Powerpoint) (Networking) (Unix) (Program) (

ERserver. iseries. Socket programming

An Advanced 4.4BSD Interprocess Communication Tutorial

Lecture 21: Networking & Protocol Stacks

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

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

Computer Networks/DV2 Lab

Sockets Interface Java (C)

Transcription:

SOCKET PROGRAMMING UNIX Programming 2014 Fall by Euiseong Seo

Communication via Network Clients and servers communicate by sending streams of bytes over connections: Point-to-point, full-duplex, and reliable A socket is an endpoint of a connection Socket address is an <IP address : port> pair A port is a 16-bit integer that identifies a process Ephemeral port: assigned automatically on client when client makes a connection request Well-known port: associated with some service provided by a server (e.g. port 80 is associated with web servers) A connection is uniquely identified by socket addresses of its endpoints (socket pair) n <client IP:client port, server IP:server port>

Communication via Network Client socket address 128.2.194.242:51213 Server socket address 208.216.181.15:80 Client Client host address 128.2.194.242 Connec&on socket pair (128.2.194.242:51213, 208.216.181.15:80) Server (port 80) Server host address 208.216.181.15 51213 is an ephemeral port allocated by OS 80 is a well- known port associated with Web servers

Client-Server Model Most network applications are built on client-server model A server and one or more clients Clients and servers are processes running on hosts (can be the same or different hosts) Server provides service by manipulating resource for clients 4. Client handles response Client process 1. Client sends request 3. Server sends response Server process 2. Server handles request Resource

Clients Examples of client programs Web browsers, ftp, telnet, ssh How does a client find servers? IP address in server socket address identifies host The (well-known) port in server socket address identifies service, and thus implicitly identifies server process that performs service Examples of well-known ports (cf. /etc/services) n Port 21: ftp n Port 23: telnet n Port 25: mail n Port 80: web

Using Ports Server host 128.2.194.242 Client host Client Service request for 128.2.194.242:80 (i.e., the Web server) Kernel Web server (port 80) Echo server (port 7) Client Service request for 128.2.194.242:7 (i.e., the echo server) Kernel Web server (port 80) Echo server (port 7)

Servers Servers are long-running processes or daemons Usually initiated during booting procedure Run continuously until the machine is turned off Each server waits for requests to arrive on a well-known port associated with a particular service Port 21: ftp server Port 23: telnet server Port 25: mail server Port 80: HTTP server A machine that runs a server process is also often referred to as a server

Sockets Sockets interface Introduced in BSD4.1 UNIX, 1981. Provides a user-level interface to network. Based on client-server paradigm Two types of transport service n Unreliable datagram (UDP) n Reliable and connection-oriented byte stream (TCP) Underlying basis for all Internet applications

Sockets What is a socket? An interface to network from applications (a door ) n To an OS, a socket is an endpoint of communication n To an application, a socket is a file descriptor n Applications read/write from/to network using file descriptor Clients and servers communicate with each by reading from and writing to socket descriptors n Main distinction between regular file I/O and socket I/O is how applications open file or socket descriptors

Sockets Hardware/software organization of networking applications Client host Server host Sockets interface (system calls) Client User code Server Hardware interface (interrupts) TCP/IP Network adapter Kernel code Hardware and firmware TCP/IP Network adapter Global IP Internet

Sockets Connec&on- oriented service Server socket() Connec&onless service Server socket() Client socket() connect() write() read() connec&on established request response bind() listen() accept() read() write() Client socket() bind() sendto() recvfrom() response request bind() recvfrom() sendto()

Creation of a Socket socket(2) creates a socket and returns a socket descriptor, which is an ordinary file descriptor Prototype #include <sys/types.h> /* See NOTES */! #include <sys/socket.h>!! int socket(int domain, int type, int protocol); protocol is usually 0 to let system choose an appropriate protocol for domain You can close SD with close()

Socket Address Structure Generic socket address For address arguments to connect(), bind(), and accept() struct sockaddr { unsigned short sa_family; /* protocol family */ char sa_data[14]; /* address data. */ }; Internet-specific socket address Must cast (sockaddr_in *) to (sockaddr *) for connect(), bind(), and accept() struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ };

Connecting to a Server Prototype #include <sys/types.h> /* See NOTES */! #include <sys/socket.h>! int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); To establish a TCP connection to a server servaddr contains <IP address, port number> of server The client does not have to call bind() before calling connect() n The kernel will choose both an ephemeral port and source IP address if necessary Client process blocks until connection establishes

Echo Client (1) #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, cfd; struct hostent *h; struct sockaddr_in saddr; char buf[maxline]; char *host = argv[1]; int port = atoi(argv[2]); if ((cfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf( socket() failed.\n ); exit(1); }

Echo Client (2) if ((h = gethostbyname(host)) == NULL) { printf( invalid hostname %s\n, host); exit(2); } bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; bcopy((char *)h- >h_addr, (char *)&saddr.sin_addr.s_addr, h- >h_length); saddr.sin_port = htons(port); } if (connect(cfd,(struct sockaddr *)&saddr,sizeof(saddr)) < 0) { printf( connect() failed.\n ); exit(3); } while ((n = read(0, buf, MAXLINE)) > 0) { write(cfd, buf, n); n = read(cfd, buf, MAXLINE); write(1, buf, n); } close(cfd);

Binding a Port to a Socket bind(2) call assigns a local address (or name) to socket descriptor Client process will use this same address to connect to this socket Prototype #include <sys/types.h> /* See NOTES */! #include <sys/socket.h>!! int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Activation of a Socket To activate a socket, use listen(2) Prototype #include <sys/types.h> /* See NOTES */! #include <sys/socket.h>!! int listen(int sockfd, int backlog); This sets maximum number of pending connections the system allows before refusing connection listen() is not need for UDP

Accepting a Client Prototype #include <sys/types.h> /* See NOTES */! #include <sys/socket.h>!! int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); family specifies the protocol family. n AF_UNIX: Local Unix domain protocols n AF_INET: IPv4 Internet protocols type specifies the communication semantics. n SOCK_STREAM n SOCK_DGRAM n SOCK_RAW: provides raw network protocol access protocol specifies a particular protocol for the socket

Accepting a Client accept() blocks until a client connects It fills in a sockaddr structure with client address It returns a new socket, communication socket A communication socket will be assigned to each client A single listening descriptor can fork many connected descriptors All actual data transfer will be done via communication sockets

Accepting a Client Client clientfd listenfd(3) Server 1. Server blocks in accept, wailng for conneclon request on listening descriptor listenfd Client clientfd Connec&on request listenfd(3) Server 2. Client makes conneclon request by calling and blocking in connect Client clientfd listenfd(3) Server connfd(4) 3. Server returns connfd from accept. Client returns from connect. ConnecLon is now established between clientfd and connfd

Echo Server (1) #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <arpa/inet.h> #define MAXLINE 80 int main (int argc, char *argv[]) { int n, listenfd, connfd, caddrlen; struct hostent *h; struct sockaddr_in saddr, caddr; char buf[maxline]; int port = atoi(argv[1]); if ((listenfd = socket(af_inet, SOCK_STREAM, 0)) < 0) { printf( socket() failed.\n ); exit(1); }

Echo Server (2) bzero((char *)&saddr, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(inaddr_any); saddr.sin_port = htons(port); if (bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { printf( bind() failed.\n ); exit(2); } if (listen(listenfd, 5) < 0) { printf( listen() failed.\n ); exit(3); } while (1) { caddrlen = sizeof(caddr); if ((connfd = accept(listenfd, (struct sockaddr *)&caddr, &caddrlen)) < 0) { printf ( accept() failed.\n ); continue; }

Echo Server (3) h = gethostbyaddr((const char *)&caddr.sin_addr.s_addr, sizeof(caddr.sin_addr.s_addr), AF_INET); printf( server connected to %s (%s)\n, h- >h_name, inet_ntoa(*(struct in_addr *)&caddr.sin_addr)); // echo while ((n = read(connfd, buf, MAXLINE)) > 0) { printf ( got %d bytes from client.\n, n); write(connfd, buf, n); } } } printf( connection terminated.\n ); close(connfd);

Echo Service Client socket Server socket bind connect Connec&on request listen accept write read close EOF read write read Await connec&on request from next client close