Introduction to Socket Programming

Similar documents
Socket Programming 2007/03/28

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

A Client-Server Exchange

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication II

Socket Programming TCP UDP

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

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

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

// socket for establishing connections

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

CSE 489/589 Recitation TCP Socket & Synchronous I/O Multiplexing

ECE 435 Network Engineering Lecture 2

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

CS118 Discussion 1A, Week 3. Zengwen Yuan Dodd Hall 78, Friday 10:00 11:50 a.m.

ECE 435 Network Engineering Lecture 2

Sockets Sockets Communication domains

COMMUNICATION PROTOCOLS: REMOTE PROCEDURE CALL (RPC)

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CS118 Discussion Week 2. Taqi

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

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

CS321: Computer Networks Introduction to Application Layer

COMP/ELEC 429/556 Introduction to Computer Networks

System Programming. Sockets

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

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

Socket Programming Example

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

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

Elementary TCP Sockets

CS321: Computer Networks Socket Programming

Group-A Assignment No. 6

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Tutorial on Socket Programming

CLIENT-SIDE PROGRAMMING

Project 1: A Web Server Called Liso

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

CS 640: Computer Networking

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

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

Interprocess Communication Mechanisms

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

UNIT IV- SOCKETS Part A

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

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

(Refer Slide Time: 1:09)

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

Topics for this Week

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

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

Sockets 15H2. Inshik Song

Interprocess Communication Mechanisms

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

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

Socket Programming for TCP and UDP

Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Network Games Part II. Architecture of Network Game

EECS122 Communications Networks Socket Programming. Jörn Altmann

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

CMPSC 311- Introduction to Systems Programming Module: Network Programming

Redes de Computadores (RCOMP)

Redesde Computadores(RCOMP)

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CPS 214: Computer Networks. Slides by Adolfo Rodriguez

C Structures in Practice

CSE 333 Lecture 16 - network programming intro

CS 3516: Computer Networks

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

I/O Models. Kartik Gopalan

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

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

9/13/2007. Motivations for Sockets What s in a Socket? Working g with Sockets Concurrent Network Applications Software Engineering for Project 1

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

2/13/2014. A protocol is an agreed-upon convention that defines how communication occurs between two (or more?) endpoints

Network Programming in Python. based on Chun, chapter 2; plus material on classes

ECE 650 Systems Programming & Engineering. Spring 2018

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

CSE 333 SECTION 6. Networking and sockets

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

Introduction to Socket Programming

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

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

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

ICT 6544 Distributed Systems Lecture 5

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

Introduction and Overview Socket Programming Lower-level stuff Higher-level interfaces Security. Network Programming. Samuli Sorvakko/Nixu Oy

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

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

UNIX Sockets. COS 461 Precept 1

CSE 333 Lecture network programming intro

Configure Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) Service Settings on a Switch

CSE 333 SECTION 8. Sockets, Network Programming

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

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

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

Transcription:

Introduction to Socket Programming Sandip Chakraborty Department of Computer Science and Engineering, INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR March 21, 2017 Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 0 / 32

Basics of TCP/IP Protocols - Revisited Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 1 / 32

Basics of TCP/IP Protocols - Revisited Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 2 / 32

Basics of TCP/IP Protocols - Revisited Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 3 / 32

Basics of TCP/IP Protocols - Revisited Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 4 / 32

Basics of TCP/IP Protocols - Revisited Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 5 / 32

Connecting Network with Operating System Check the net module (download Kernel source and check /usr/src/linux/net)! Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 6 / 32

Application Multiplexing in TCP/IP Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 7 / 32

Application Multiplexing in TCP/IP Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 8 / 32

Application Multiplexing in TCP/IP Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 9 / 32

What are Sockets? Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 10 / 32

Socket Programming Framework/API A set of system calls to get the service from TCP/IP protocol stack (net module in the OS kernel). Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 11 / 32

Socket Types The Internet is a trade-off between performance and reliability - Can you say why? Some application requires fine grained performance (example - multimedia applications), while others require reliability (example - file transfer) Transport layer supports two services - Reliable (TCP), and Unreliable (UDP) Two types of sockets: 1 Stream Socket (SOCK STREAM): Reliable, connection oriented (TCP based) 2 Datagram Socket (SOCK DGRAM): Unreliable, connection less (UDP based) Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 12 / 32

Socket API int s = socket(domain, type, protocol); - Create a socket domain: Communication domain, typically used AF INET (IPv4 Protocol) type: Type of the socket - SOCK STREAM or SOCK DGRAM protocol: Specifies protocols - usually set to 0 Explore! int status = bind(sockid, &addrport, size); - Reserves a port for the socket. sockid: Socket identifier addrport: struct sockaddr in - the (IP) address and port of the machine (address usually set to INADDR ANY chooses a local address) size: Size of the sockaddr structure Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 13 / 32

struct sockaddr in sin family : Address family, AF INET for IPv4 Protocol sin addr.s addr: Source address, INADDR ANY to choose the local address sin port: The port number We need to use htons() function to convert the port number from host byte order to network byte order. struct sockaddr in serveraddr; int port = 3028; serveraddr.sin family = AF INET; serveraddr.sin addr.s addr = INADDR ANY; serveraddr.sin port = htons(port); Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 14 / 32

Host Byte Order to Network Byte Order - Why? Little Endian and Big Endian System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 15 / 32

Host Byte Order to Network Byte Order - Why? Little Endian and Big Endian System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 15 / 32

Host Byte Order to Network Byte Order - Why? Little Endian and Big Endian System Assume a communication from a Little Endian to a Big Endian System or vice-versa! Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 15 / 32

Host Byte Order to Network Byte Order - Why? Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 16 / 32

Listen and Accept a Socket Connection struct sockaddr in cli addr; listen(sockfd,5); clilen = sizeof(cli addr); newsockfd = accept(sockfd,(struct sockaddr *) &cli addr, &clilen); Active Open and Passive Open The server needs to announce its address, remains in the open state and waits for any incoming connections - Passive Open The client only opens a connection when there is a need for data transfer - Active Open Connection is initiated by the client Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 17 / 32

Data Transfer through Sockets 1 For SOCK STREAM: read(newsockfd,buffer,255); write(newsockfd, I got your message,18); 2 For SOCK DGRAM: recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); sendto(sock, Got your message,17,0,(struct sockaddr *)&from,fromlen); Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 18 / 32

Putting it All Together Check the details and sample codes at http://www.linuxhowtos.org/c_c++/socket.htm. Socket Programming Tutorials Beej s Guide to Network Programming - http://beej.us/guide/bgnet/ http://cs.baylor.edu/~donahoo/practical/csockets/ textcode.html http: //www.cs.rpi.edu/~moorthy/courses/os98/pgms/socket.html Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 19 / 32

Extending the Server Socket for Multiple Connections Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 20 / 32

Extending the Server Socket for Multiple Connections Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 21 / 32

select(): Synchronous I/O Multiplexing Assume this scenario: you are a server and you want to listen for incoming connections as well as keep reading from the connections you already have. listen() followed by an accept() is a blocking call. [Gives a new file descriptor] listen() accept() read()/write() close() Use select() system call. Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 22 / 32

select(): Synchronous I/O Multiplexing select() gives you the power to monitor several sockets at the same time. It ll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions, if you really want to know that. int select(int numfds, fd set *readfds, fd set *writefds, fd set *exceptfds, struct timeval *timeout); Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 23 / 32

A select() Demo #define STDIN 0 int main(void) { struct timeval tv; fd set readfds; tv.tv sec = 2; tv.tv usec = 500000; FD ZERO(&readfds); FD SET(STDIN, &readfds); select(1, &readfds, NULL, NULL, &tv); if (FD ISSET(STDIN, &readfds)) printf( A key was pressed! ); else printf( Timed out. ); return 0; } Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 24 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 25 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 26 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 27 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 28 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 29 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 30 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 31 / 32

Assignment: Design a Peer-to-Peer File Sharing System Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 32 / 32

Thank You Sandip Chakraborty (IIT Kharagpur) CS 39006 March 21, 2017 32 / 32