Berkeley Sockets. Prepared by : Swapan Purkait Director. Nettech Private Ltd

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

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

Unix Network Programming

Tutorial on Socket Programming

Socket Programming. Srinidhi Varadarajan

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Network Programming with Sockets. Process Management in UNIX

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 using C

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

Socket Programming in C/C++

Implementing Network Software

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

TCP/IP - Socket Programming

NS3 Lab 1 TCP/IP Network Programming in C

Programmation Systèmes Cours 9 UNIX Domain Sockets

BSD Sockets Interface Programmer s Guide

The POSIX Socket API

UNIX Sockets. COS 461 Precept 1

Lab 4: Socket Programming: netcat part

Elementary Name and Address Conversions

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

UNIX. Sockets. mgr inż. Marcin Borkowski

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

Communication Networks. Introduction & Socket Programming Yuval Rochman

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

Concurrent Server Design Alternatives

Operating Systems Design 16. Networking: Sockets

Application Architecture

Network Programming with Sockets. Anatomy of an Internet Connection

Computer Networks Network architecture

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

CSE 333 SECTION 6. Networking and sockets

Programming guidelines on transition to IPv6

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

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

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

Review of Previous Lecture

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

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

Windows Socket Programming & IPv6 Translation Middleware

Writing a C-based Client/Server

Client / Server Programming with TCP/IP Sockets

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

An Introductory 4.4BSD Interprocess Communication Tutorial

IBM i Version 7.2. Programming Socket programming IBM

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

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

An Advanced 4.4BSD Interprocess Communication Tutorial

sys socketcall: Network systems calls on Linux

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

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

Generalised Socket Addresses for Unix Squeak

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

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

Interprocess Communication Message Passing

Lecture 7: Introduction to Sockets

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

Network Programming. Chapter The Client-Server Programming Model

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

DNS Domain Name System

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

Packet Sniffing and Spoofing Lab

Programming With Sockets 2

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

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

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

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

Network Programming using sockets

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.

CS640: Computer Networks. Naming /ETC/HOSTS

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

Name and Address Conversions

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

TCP/IP Sockets in C. Practical Guide for Programmers

Programming with TCP/IP Best Practices

Building Applications With Sockets

Implementing and testing tftp

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

Chapter 3. Internet Applications and Network Programming

Giving credit where credit is due

SSC - Communication and Networking Java Socket Programming (II)

Java Programming: Sockets in Java

Data Communication Networks. Lecture 1

An Overview of IPv6 CHAPTER

CHAPTER 7. ing with CGI

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

Objectives of Lecture. Network Architecture. Protocols. Contents

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

Direct Sockets. Christian Leber Lehrstuhl Rechnerarchitektur Universität Mannheim

Transport Layer Protocols

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

ERserver. iseries. Socket programming

Network Programming TDC 561

IPv6 Enabling CIFS/SMB Applications

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

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

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

Computer Networks/DV2 Lab

Transcription:

For Summer Training on Computer Networking visit Berkeley Sockets Prepared by : Swapan Purkait Director Nettech Private Limited swapan@nettech.in + 91 93315 90003

What is a socket? The socket is the BSD method for accomplishing inter-process communication (IPC). It is used to allow one process to speak to another (on same or different machine). Analogy: Like the telephone is used to allow one person to speak to another. Works very similar to files. Socket descriptor very similar to file descriptor. Read/write on a socket and file are very similar.

Basic Idea When two processes located on the same or different machines communicate, we define association and socket. Association: basically a 5-tuple Protocol Local IP address Local port number Remote IP address Remote port number Socket: also called half-association (a 3-tuple) Protocol, local IP address, local port number Protocol, remote IP address, remote port number

More about sockets Creating a socket is the first step in network programming using BSD socket interface. Using the socket() system call. Two main addressing formats of a socket: AF_UNIX: uses Unix pathnames to identify sockets, and are very useful for IPC between processes on the same machine. AF_INET: uses IP addresses. In addition to machine address, there is also a port number that allows more than one AF_INET socket on each machine.

Types of socket Two most common types: SOCK_STREAM: Stream sockets, which provide reliable, two-way, connection-oriented communication streams. <Uses TCP> SOCK_DGRAM: Datagram sockets, which provide connectionless, unreliable service, used for packet-by-packet transfer of information. <Uses UDP> Other types like SOCK_RAW also exist. Beyond the scope of the present discussion.

Systems calls for using sockets socket() bind() connect() listen() accept() send() & recv() sendto() & recvfrom() close() & shutdown() getpeername() gethostname() gethostbyname()

socket() :: Get the Socket Descriptor General syntax: #include <sys/types.h> #include <sys/socket.h> int socket (int domain, int type, int protocol) domain: should be set to AF_INET (typically) type: should be set to SOCK_STREAM or SOCK_DGRAM protocol: set to zero (typically) Returns: socket descriptor; -1 on error

bind():: What Port am I on? Used to associate the socket with an address General syntax: #include <sys/types.h> #include <sys/socket.h> int bind (int sockfd, struct sockaddr *my_addr, int addrlen); sockfd: socket file descriptor returned by socket() my_addr: pointer to a structure that contains information about the local IP address and port number. addrlen: typically set to sizeof(struct sockaddr) Returns: -1 on error

The sockaddr Structure struct sockaddr { unsigned short char } sa_family; sa_data[14]; sockaddr_in is a parallel structure to sockaddr which a programmer uses in the program for convenience. struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; } struct in_addr { unsigned long s_addr; }

connect(): Connect to a Remote Socket General syntax: #include <sys/types.h> #include <sys/socket.h> int connect (int sockfd, struct sockaddr *serv_addr, int addrlen); sockfd: socket file descriptor returned by socket() serv_addr: pointer to a structure that contains the destination IP address and the port number addrlen: typically set to sizeof(struct sockaddr) Returns: -1 on error

listen(): Get Set for Incoming Connections Here, we wish to wait for incoming connections and handle them in some way. Two steps, first you listen(), then you accept(). General syntax: int listen (int sockfd, int backlog); sockfd: socket file descriptor returned by socket(). backlog: used to set the maximum number of requests (up to a maximum of about 20) that will be queued up before requests start being denied. Returns: -1 on error

accept(): Waiting for Incoming Connections Basic concept: Someone far away will try to connect() to your machine on a port that you are listen() ing on. Such connections will be queued up waiting to be accept() ed. accept() returns a brand new socket file descriptor to use for every single connection. Two socket file descriptors!! The original one is still listening on your port. Newly created one is finally ready to send() and recv().

accept(): contd.. General syntax: #include <sys/socket.h> int accept (int sockfd, void *addr, int *addrlen); sockfd: listen() ing socket descriptor addr: pointer to a local struct sockaddr_in (This is where the information about the incoming connection will go) addrlen: local integer variable that should be set to sizeof(struct sockaddr_in) before accept() is called. Returns: -1 on error

send() and recv(): Sending/receiving Data Used for communicating over stream sockets or connected datagram sockets. General syntax: int send (int sockfd, const void *mesg, int len, int flags); mesg: int recv a (int pointer sockfd, to void the *buf, data int you len, want unsigned to send int flags); len: length of the data in bytes buf: buffer to read the information into flags: typically set to 0 send() returns the number of bytes actually sent out, and recv() returns the number of bytes actually read into the buffer.

sendto() and recvfrom() Used to transmit and receive data packets over unconnected datagram sockets. General syntax: int sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recvfrom (int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); If you connect() a datagram socket, you can then simply use send() and recv() for all your transactions.

close() and shutdown() Used to close the connection on the socket descriptor. close (sockfd); This prevents any more reads and writes to the socket. int shutdown (int sockfd, int how); how=0 further receives are disallowed how=1 further sends are disallowed how=2 further sends and receives are disallowed (like close())

getpeername() This function will tell you who is at the other end of a connection stream socket. #include <sys/socket.h> int getpeername (int sockfd, struct sockaddr *addr, int *addrlen); sockfd: descriptor of the connected stream socket addr: pointer to a structure that will hold the information about the other side of the connection addrlen: pointer to an int that should be initialized to sizeof(struct sockaddr)

gethostname() This function returns the name of the computer that your program is running on. This name can be used by gethostbyname() to determine the IP address of the local machine. #include <unistd.h> int gethostname (char *hostname, size_t size); hostname: pointer to an array of chars that will contain the host name upon the function s return. size: length in bytes of the hostname array.

gethostbyname() Returns the IP address of a host given its name. Invokes the Domain Name Server (DNS). #include <netdb.h> struct hostent *gethostbyname (const char *name); Returns a pointer to a struct hostent: struct hostent { char *h_name; /* official name of the host */ char **h_aliases; /* NULL terminate array of alternate names */ int haddrtype; /* Type of address being returned (AF_INET) */ int h_length; /* Length of the address in bytes */ char **h_addr_list; /* Zero terminated array of network addresses */ }; #define h_addr h_adr_list[0];

Client-server Model Standard model for network applications. A server is a process that is waiting to be contacted by a client process so as to provide some service. Typical scenario: The server process is started on some computer system. Initializes itself, then goes to sleep waiting for a client request. A client process is started, either on the same system or on some other system. Client sends a request (across the network) to the server. When the server process has finished providing its service to the client, the server goes back to sleep, waiting for the next client request to arrive.

Client-server Model (contd.) Roles of the client and the server processes are asymmetric. Two types of servers: Iterative servers: Used when the server process knows in advance how long it takes to handle each request and it handles each request itself. Concurrent servers: Used when the amount of work required to handle a request is unknown; the server starts another process to handle each request.

SERVER System Calls for Connection-oriented Protocol socket() bind() listen() accept() socket() CLIENT Block until client request arrives Connection establishment connect() read() write() write() read()

System Calls for Connectionless Protocol SERVER socket() bind() recvfrom() socket() CLIENT Block until client request arrives Data (request) bind() sendto() sendto() Data (reply) recvfrom()

Connect with us at Facebook Visit www.facebook.com/nettech.in