Application Application. Internet. Interface Interface

Similar documents
Introduction to Socket programming using C

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

Tutorial on Socket Programming

Socket Programming. Srinidhi Varadarajan

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

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

Unix Network Programming

NS3 Lab 1 TCP/IP Network Programming in C

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

CSE 333 SECTION 6. Networking and sockets

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

Implementing Network Software

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

Network Programming with Sockets. Anatomy of an Internet Connection

Application Architecture

Review of Previous Lecture

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

Communication Networks. Introduction & Socket Programming Yuval Rochman

TCP/IP - Socket Programming

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

ELEN 602: Computer Communications and Networking. Socket Programming Basics

UNIX Sockets. COS 461 Precept 1

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

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

UNIX. Sockets. mgr inż. Marcin Borkowski

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

Client / Server Programming with TCP/IP Sockets

IBM i Version 7.2. Programming Socket programming IBM

Socket Programming in C/C++

Windows Socket Programming & IPv6 Translation Middleware

Programming guidelines on transition to IPv6

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

Lab 4: Socket Programming: netcat part

Concurrent Server Design Alternatives

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

The POSIX Socket API

BSD Sockets Interface Programmer s Guide

Computer Networks Network architecture

Programmation Systèmes Cours 9 UNIX Domain Sockets

Lecture 7: Introduction to Sockets

Network Programming with Sockets. Process Management in UNIX

IPv6 Enabling CIFS/SMB Applications

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

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

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

Packet Sniffing and Spoofing Lab

Operating Systems Design 16. Networking: Sockets

Writing a C-based Client/Server

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

Elementary Name and Address. Conversions

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

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

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

sys socketcall: Network systems calls on Linux

An Overview of IPv6 CHAPTER

Best practices in IPv6 enabled networking software development.

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

Giving credit where credit is due

Generalised Socket Addresses for Unix Squeak

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

Elementary Name and Address Conversions

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

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

The programming language C. sws1 1

TCP/IP Sockets in C. Practical Guide for Programmers

IPv4/IPv6 Transition for SIP VoIP Applications

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

Objectives of Lecture. Network Architecture. Protocols. Contents

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

Beej's Guide to Network Programming

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

Network Programming TDC 561

Application Development with TCP/IP. Brian S. Mitchell Drexel University

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

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

Category: Standards Track August 1995

Socket = an interface connection between two (dissimilar) pipes. OS provides this API to connect applications to networks. home.comcast.

Computer Networks/DV2 Lab

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.

Chapter 3. Internet Applications and Network Programming

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

CS 213, Fall 2000 Lab Assignment L5: Logging Web Proxy Assigned: Nov. 28, Due: Mon. Dec. 11, 11:59PM

Data Communication Networks. Lecture 1

AT12181: ATWINC1500 Wi-Fi Network Controller - AP Provision Mode. Introduction. Features. Atmel SmartConnect APPLICATION NOTE

Lecture 9: Network Security Introduction

Slide 1 Introduction cnds@napier 1 Lecture 6 (Network Layer)

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

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

Software changes for Website and Application IPv6 Readiness

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Porting and Deploying VoIP to IPv6: Lessons Learned

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

CHAPTER 7. ing with CGI

University of Amsterdam

App Note. VoxStack GSM Gateway API

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

DNS Domain Name System

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

Interprocess Communication Message Passing

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

Transcription:

An Introduction to Network Programming By Kiran Kotla TA, ECEN 602 1

How do two applications interact? Application Application Internet Interface Interface 2

Example http://www.tamu.edu www.tamu.edu Host Name Address This specifies one end point of communication Suppose mymachine.tamu.edu wants to open this webpage. It needs to talk to the web server at www.tamu.edu 3

How do two applications interact? Client (Mymachine.tamu.edu) Server (www.tamu.edu) Application Application Internet Interface Interface 4

Example (contd..) Here the tag http tells the browser to contact the web server at www.tamu.edu In this type of set up: www.tamu.edu is the server machine And Mymachine.tamu.edu is the client machine As there could be multiple applications running at the server machine, each application operates at a port number. 5

How do two applications interact? Browser Client (Mymachine.tamu.edu) Web Server Server (www.tamu.edu) Application Application Internet Interface Interface 6

How do two applications interact? Browser ( port x ) Web Server ( port y ) Client (Mymachine.tamu.edu) Server (www.tamu.edu) Application Application Internet Interface Interface 7

Application Transport Network Data link Physical TCP/IP Network Stack Network Data link Physical Application Transport Network Data link Physical 8

Network Application Programming Interface (API) The services provided (often by the operating system) that provide the interface between application and protocol software. Application Network API Protocol A Protocol B Protocol C 9

System calls General Use read() write() close() Connection-oriented oriented (TCP) connect() listen() accept() Connectionless (UDP) send() recv() 10

open_clientfd Client / Server Session Overview Client Server socket socket bind listen connect Connection request accept write read read write close EOF read close open_listenfd 11

Desired properties of Network API Generic Programming Interface. Support for message oriented and connection oriented communication. Operating System independence 12

Generic Programming Interface Support multiple communication protocol suites (families). Address (endpoint) representation independence. 13

TCP/IP TCP/IP does not include an API definition. 14

Functions needed: Specify local and remote communication endpoints Initiate a connection Wait for incoming connection Send and receive data Terminate a connection gracefully Error handling 15

Berkeley Sockets Generic: support for multiple protocol families. address representation independence 16

Socket A socket is an abstract representation of a communication endpoint. Need of Sockets: establishing a connection specifying communication endpoint addresses 17

Creating a Socket int socket(int family,int type,int proto); family specifies the protocol family (PF_INET for TCP/IP). type specifies the type of service (SOCK_STREAM SOCK_STREAM, SOCK_DGRAM). protocol specifies the specific protocol (usually 0, which means the default). 18

socket() The socket() system call returns a socket descriptor (small integer) or -1 on error. socket() allocates resources needed for a communication endpoint - but it does not deal with endpoint addressing. 19

Specifying an Endpoint Address Remember that the sockets API is generic. There must be a generic way to specify endpoint addresses. TCP/IP requires an IP address and a port number for each endpoint address. Other protocol suites (families) may use other schemes. 20

Socket Descriptor Data 0 1 2 3 4 Descriptor Table Structure Family: AF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726 21

Some data types that are used sa_family_t address family socklen_t length of struct in_addr_t IPv4 address in_port_t IP port number 22

Generic socket addresses struct sockaddr { }; uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; sa_family specifies the address type. sa_data specifies the address value. 23

AF_INET For AF_INET we need: 16 bit port number 32 bit IP address 24

struct sockaddr_in (IPv4) struct sockaddr_in { }; uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; A special kind of sockaddr structure 25

Network Byte Order Bytes travel through the network in a stream of bytes. However, the architectures of the two end hosts could be different, i.e., one is little endian and the other is big endian 26

Network Byte Order (Contd..) 27

Network Byte Order (Contd..) All values stored in a sockaddr_in must be in network byte order. sin_port a TCP/IP port number. sin_addr an IP address. 28

Network Byte Order Functions h : host byte order n : network byte order s : short (16bit) l : long (32bit) uint16_t hto uint16_t nto tons(uint16_t); tohs(uint_16_t); uint32_t hto tonl(uint32_t); uint32_t nto tohl(uint32_t); 29

TCP/IP Addresses We don t need to deal with sockaddr structures since we will only deal with a real protocol family. We can use sockaddr_in structures. BUT: The C functions that make up the sockets API expect structures of type sockaddr. 30

sockaddr sockaddr_in sa_len sin_len sa_family AF_INET sin_port sa_data sin_addr sin_zero 31

Assigning an address to a socket The bind() system call is used to assign an address to an existing socket. int bind( int sockfd, const! const struct sockaddr *myaddr, int addrlen); bind returns 0 if successful or -1 on error. 32

bind() calling bind() assigns the address specified by the sockaddr structure to the socket descriptor. You can give bind() a sockaddr_in structure: bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) ); 33

bind() Example int mysock,err; struct sockaddr_in myaddr; mysock = socket(pf_inet,sock_stream,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( portnum ); myaddr.sin_addr = htonl( ipaddress); err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr)); 34

Uses for bind() There are a number of uses for bind(): Server would like to bind to a well known address (port number). Client can bind to a specific port. Client can ask the O.S. to assign any available port number. 35

Client Port Clients typically don t care what port they are assigned. When you call bind you can tell it to assign you any available port: myaddr.port = htons(0); 36

Accepting connections How can you find out what your IP address is so you can tell bind()? There is no realistic way for you to know the right IP address to give bind() - what if the computer has multiple network interfaces? specify the IP address as: INADDR_ANY, this tells the OS to take care of things. 37

IPv4 Address Conversion int inet_aton( char *, struct in_addr *); Convert ASCII dotted-decimal decimal IP address to network byte order 32 bit value. Returns 1 on success, 0 on failure. char *inet_ntoa(struct in_addr); Convert network byte ordered value to ASCII dotted-decimal decimal (a string). 38

open_clientfd Client / Server Session Overview Client Server socket socket bind listen connect Connection request accept write read read write close EOF read close open_listenfd 39

Lets put the server together... struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if((sockfd=socket(af_inet, SOCK_STREAM, 0) < 0) { // from back a couple slides printf( Error creating socket\n );... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(inaddr_any); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf( Error binding\n );... } if(listen(sockfd, 5) < 0) { // listen for incoming connections printf( Error listening\n );... } clen=sizeof(caddr) if((isock=accept(sockfd, (struct sockaddr *) &caddr, &clen)) < 0) { // accept one printf( Error accepting\n );... }

Piecing the Client Together struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; if((sockfd=socket(af_inet, SOCK_STREAM, 0) < 0) { // from back a couple slides printf( Error creating socket\n );... } if((h=gethostbyname( www.slashdot.org )) == NULL) { // Lookup the hostname printf( Unknown host\n );... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port); // specify port to connect to if((connfd=connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // connect! printf( Cannot connect\n );... }

Thank you! Please refer to the course website for more information http://www.ece.tamu.edu/~reddy/ee602_07.html 42