Network Programming Berkeley Sockets (BSD)

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Sockets Programming Network API Socket Structures Socket Functions
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
1 Netcomm Sockets Communication Networks Recitation 1.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
1 Programming with TCP/IP by Armin R. Mikler. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software.
ECE 4110 – Internetwork Programming Client-Server Model.
Operating Systems Chapter 9 Distributed Communication.
Client-Side Network Programming
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
The Sockets Library and Concepts Rudra Dutta CSC Spring 2007, Section 001.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Introduction to Sockets
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Transport layer API: Socket Programming
UDP Sockets Programming
TCP Sockets Programming
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Tutorial on Socket Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
CSI 4118 – UNIVERSITY OF OTTAWA
Presentation transcript:

Network Programming Berkeley Sockets (BSD) Socket to me!

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

Network API Generic Programming Interface. Support for message oriented and connection oriented communication. Work similar with well-known I/O services (when this makes sense). Operating System independence.

Generic Programming Interface Support multiple communication protocol suites (families). Address (endpoint) representation independence. Provide special services for Client and Server?

TCP/IP TCP/IP does not include an API definition. There are a variety of APIs for use with TCP/IP: BSD Sockets (C language) TLI, XTI (C language) Winsock MacTCP

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

BSD Sockets Generic: support for multiple protocol families. address representation independence Uses existing I/O programming interface as much as possible.

Socket A socket is an abstract representation of a communication endpoint. Sockets work like Unix I/O services (files I/O, pipes & message queues etc). … but Sockets (obviously) have special needs: Client & Server role definition connection-oriented & connectionless network connections more parameters must be specified (protocol type, local-addr, local process, foreign-addr, foreign-process) specifying communication endpoint addresses and dealing with multiple communication protocols (INET, XNS, Unix Domain) stream and message oriented services

Unix Descriptor Table 1 2 3 4 Descriptor Table Data structure for file 0 1 Data structure for file 1 2 3 Data structure for file 2 4

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

Socket system calls - Overview “Install a telephone line” Creates a communication endpoint (allocate the necessary resources) for later use. Returns a socket descriptor. bind() “Get a telephone number” Binds a socket (descriptor) to an address listen() “Enable "call waiting" for your telephone” Sets the maximum number of requests that will be queued before requests start being denied. accept() “Wait for incoming calls and pickup the telephone when it rings” Blocks until connection request appears. Takes the first request on the queue and creates (returns) a new socket (socket descriptor) (passive open) connect() “Dialing (a specific phone number)” (Tries to) Make a connection to a listening socket (active open) read(), recvfrom() write(), sendto() “Let’s talk now…” Read (write) from (to) a connected stream socket or a established datagram socket close(), shutdown() “Hanging Up” Closes a socket (takes care about any unsent data)

Socket system calls for a typical connection-oriented session (TCP)

Socket system calls for a typical connectionless session (UDP)

Creating a Socket … int socket(int family,int type,int proto); #include <sys/types> #include <sys/socket.h> … int socket(int family,int type,int proto); family specifies the protocol family AF_INET (or PF_INET) for TCP/IP protocols AF_NS (or PF_NS) for Xerox NS protocols AF_UNIX (or PF_UNIX) for Unix internal protocols type specifies the type of service SOCK_STREAM for stream socket (TCP) SOCK_DGRAM for datagram socket (UDP). SOCK_RAW for raw datagrams (IP) protocol specifies the specific protocol (usually 0, which means the default).

socket() socket() system call returns a socket descriptor (small integer) or a -1 on error. socket() allocates resources needed for a communication endpoint - but it does not deal with endpoint addressing socket().specifies one element of the TCP’s 5-tuple or the UDP’s 3-tuple

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.

POSIX data types int8_t signed 8bit int uint8_t unsigned 8 bit int u_char, u_short, u_int, u_long

More POSIX data types sa_family_t address family socklen_t length of struct in_addr_t IPv4 address in_port_t IP port number

Generic socket addresses Used by kernel struct sockaddr { u_short sa_len; u_short sa_family; char sa_data[14]; }; definition in <sys/socket.h> sa_family specifies the address type. sa_data specifies the address value.

sockaddr An address that will allow me to use sockets to communicate with my kids. address type AF_DAVESKIDS address values: Andrea 1 Mom 5 Jeff 2 Dad 6 Robert 3 Dog 7 Emily 4

AF_DAVESKIDS Initializing a sockaddr structure to point to Robert: struct sockaddr robster; robster.sa_family = AF_DAVESKIDS; robster.sa_data[0] = 3; Really old picture!

AF_INET For AF_DAVESKIDS we only needed 1 byte to specify the address. For AF_INET we need: 16 bit port number 32 bit IP address IPv4 only!

struct sockaddr_in (IPv4) A special kind of sockaddr structure struct in_addr { u_long s_addr; }; struct sockaddr_in { u_short sin_len; short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; definition in <sys/socket.h>

Data representation multibyte values (e.g. 16-bit integers) little endian least significant byte first (DEC, Intel) big endian most significant byte first (Sun, SGI, HP) how do we store the integer 0x12345678 ? 0x12 0x34 0x56 0x78 0x101 0x102 0x103 0x100 0x78 0x56 0x34 0x12 0x101 0x102 0x103 0x100 Big Endian Little Endian

Ignoring Network Byte Order network byte order = big endian (TCP/IP, XNS, SNA) integer fields in protocol headers 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. Common Mistake: Ignoring Network Byte Order

Network Byte Order Functions ‘h’ : host byte order ‘n’ : network byte order ‘s’ : short (16bit) ‘l’ : long (32bit) u_short htons(u_short); u_short ntohs(u_short); u_long htonl(u_long); u_long ntohl(u_long);

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.

sockaddr_in sockaddr sa_len sin_len sa_family AF_INET sin_port sin_addr sin_zero sa_data

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

bind() calling bind() assigns the address specified by the sockaddr structure to the socket descriptor. bind() fills in the local-addr and local-port elements of the association 5-tuple. you can give bind() a sockaddr_in structure: bind( mysock, (struct sockaddr*) &myaddr, sizeof(myaddr) );

bind() Example int mysock,err; struct sockaddr_in myaddr; mysock = socket(AF_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));

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. TCP/UDP Ports 0 – 1023 : privileged ports (only for root processes) typically used by servers 1024 – 65535 : automatically assigned (by the kernel) ports typically used by clients

Port assignment - who cares ? 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);

What is my IP address ? 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.

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

Byte Operations bcopy (char *src, char *dest, int nbytes); moves the specified number of bytes from source to destination (similar with memcpy) bzero (char *dest int nbytes); writes the specified number of null bytes to destination (similar with memset)