Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Socket Programming. Srinidhi Varadarajan

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

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

Introduction to Socket programming using C

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

NS3 Lab 1 TCP/IP Network Programming in C

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

CSE 333 SECTION 6. Networking and sockets

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

Socket Programming in C/C++

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Implementing Network Software

TCP/IP - Socket Programming

Network Programming with Sockets. Process Management in UNIX

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

Programmation Systèmes Cours 9 UNIX Domain Sockets

The POSIX Socket API

UNIX. Sockets. mgr inż. Marcin Borkowski

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

BSD Sockets Interface Programmer s Guide

UNIX Sockets. COS 461 Precept 1

Concurrent Server Design Alternatives

Computer Networks Network architecture

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

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

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

Windows Socket Programming & IPv6 Translation Middleware

Lecture 7: Introduction to Sockets

Application Architecture

Communication Networks. Introduction & Socket Programming Yuval Rochman

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

Writing a C-based Client/Server

Client / Server Programming with TCP/IP Sockets

Elementary Name and Address. Conversions

Network Programming. Chapter The Client-Server Programming Model

Giving credit where credit is due

Review of Previous Lecture

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

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

Programming guidelines on transition to IPv6

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

Packet Sniffing and Spoofing Lab

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

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

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

IPv6 Enabling CIFS/SMB Applications

Operating Systems Design 16. Networking: Sockets

IBM i Version 7.2. Programming Socket programming IBM

Java Programming: Sockets in Java

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

Generalised Socket Addresses for Unix Squeak

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

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

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

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

Elementary Name and Address Conversions

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

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

TCP/IP Sockets in C. Practical Guide for Programmers

Best practices in IPv6 enabled networking software development.

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

An Introductory 4.4BSD Interprocess Communication Tutorial

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

Name and Address Conversions

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

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

Data Communication Networks. Lecture 1

Network Programming TDC 561

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

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

Chapter 11. User Datagram Protocol (UDP)

Lecture 9: Network Security Introduction

sys socketcall: Network systems calls on Linux

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

Network Programming using sockets

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

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

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

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

Chapter 3. Internet Applications and Network Programming

System Calls and Standard I/O

Computer Networks - Xarxes de Computadors

Hardware Prerequisites Atmel SAM W25 Xplained Pro Evaluation Kit Atmel IO1 extension Micro-USB Cable (Micro-A / Micro-B)

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

Objectives of Lecture. Network Architecture. Protocols. Contents

An Overview of IPv6 CHAPTER

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

Internet Addressing. Name: identifies what the object is Address: identifies where it is Route: identifies how to get there

Data Communication & Networks G

Porting and Deploying VoIP to IPv6: Lessons Learned

An Advanced 4.4BSD Interprocess Communication Tutorial

DNS Domain Name System

Programming With Sockets 2

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

SSC - Communication and Networking Java Socket Programming (II)

Transcription:

Sockets Hyo-bong Son (proshb@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Sockets (1) Sockets interface Introduced in BSD4.1 UNIX, 1981. Provides a user-level interface to the network. Explicitly created, used, released by applications. Based on client/server paradigm Two types of transport service Unreliable datagram Reliable, connection-oriented byte stream Underlying basis for all Internet applications 2

Socket Address Structure(1) 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) */ ; 3

IP Addresses (1) Storing IP addresses IP addresses (and other integer values such as port number) are always stored in memory in network byte order (big endian) /* Internet address structure */ struct in_addr { unsigned int s_addr; /* network byte order (big-endian) */ ; Handy network byte-order conversion functions: htonl(): long int from host to network byte order htons(): short int from host to network byte order ntohl(): long int from network to host byte order ntohs(): short int from network to host byte order 4

IP Addresses (2) Dotted decimal notation By convention, each byte in a 32-bit IP address is represented by its decimal value and separated by a period. IP address 0x739198B5 = 115.145.152.181 Converting functions inet_aton(): a dotted decimal string to an IP address in network byte order inet_ntoa(): an IP address in network byte order to its corresponding dotted decimal string n denotes network representation. a denotes application representation. 5

Socket Address Structure(2) How to fill a socket address structure? int inet_pton(int af, const char *src, void *dst); Returns 1 on success int af address family (AF_INET, AF_INET6) const char *src character string containing address void *dst socket address structure pointer 6

Socket Address Structure(2) How to extract a socket address structure? const char *inet_ntop(int af, const char *src, char *dst, socklen_t size); Returns pointer to dst, NULL on error int af address family (AF_INET, AF_INET6) const char *src address structure src char *dst buffer to copy the address socklen_t size buffer size 7

TCP Sockets Client socket Server socket bind connect Connection request listen accept write read close EOF read write read Await connection request from next client close 8

socket() int socket (int family, int type, int protocol) Specifying the type of communication socket() creates a socket descriptor. -1 on error. family specifies the protocol family. AF_UNIX: Local Unix domain protocols AF_INET: IPv4 Internet protocols type specifies the communication semantics. SOCK_STREAM: provides sequenced, reliable, two-way, connection-based byte streams SOCK_DGRAM: supports datagrams (connectionless, unreliable messages of a fixed maximum length) protocol specifies a particular protocol to be used with the socket. 9

connect() int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen) Used by a TCP client to establish a connection with a TCP server. servaddr contains <IP address, port number> of the server. The client does not have to call bind() before calling connect(). The kernel will choose both an ephemeral port and the source IP address if necessary. Client process suspends (blocks) until the connection is created. 10

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); 11

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); 12

TCP Sockets Client socket Server socket bind connect Connection request listen accept write read close EOF read write read Await connection request from next client close 13

bind() int bind (int sockfd, struct sockaddr *myaddr, socklen_t addrlen) bind() gives the socket sockfd the local address myaddr. myaddr is addrlen bytes long. Servers bind their well-known port when they start. If a TCP server binds a specific IP address to its socket, this restricts the socket to receive incoming client connections destined only to that IP address. Normally, a TCP client let the kernel choose an ephemeral port and a client IP address. 14

listen() int listen (int sockfd, int backlog) listen() converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests. When a socket is created, it is assumed to be an active socket, that is, a client socket that will issue a connect(). backlog specifies the maximum number of connections that the kernel should queue for this socket. Historically, a backlog of 5 was used, as that was the maximum value supported by 4.2BSD. Busy HTTP servers must specify a much larger backlog, and newer kernels must support larger values. 15

accept() (1) int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) accept() blocks waiting for a connection request. accept() returns a connected descriptor with the same properties as the listening descriptor. The kernel creates one connected socket for each client connection that is accepted. Returns when the connection between client and server is created and ready for I/O transfers. All I/O with the client will be done via the connected socket. The cliaddr and addrlen arguments are used to return the address of the connected peer process (the client) 16

accept() (2) Client clientfd listenfd(3) Server 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd. Client clientfd Connection request listenfd(3) Server 2. Client makes connection request by calling and blocking in connect. Client clientfd listenfd(3) Server connfd(4) 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd. 17

accept() (3) Listening descriptor End point for client connection requests Created once and exists for lifetime of the server Connected descriptor End point of the connection between client and server A new descriptor is created each time the server accepts a connection request from a client. Exists only as long as it takes to service client. Why the distinction? Allows for concurrent servers that can communicate over many client connections simultaneously. 18

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); 19

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; 20

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); 21

Exercises Make Connection oriented(tcp) DB! Client requests followings to server GetValue: Request the number PutValue: Put a word to server GetRank: Find the rank for the word. Server should manage the DB 22