Introduction to Berkeley Sockets

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne) Data Communication: Introduction to Berkeley Sockets INF1060: Introduction to Operating.
Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne)
Networks: TCP/IP Socket Calls1 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 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.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
ISP – 9 th Recitation Socket programming – Client side.
CS 360 – Spring 2007 Pacific University TCP section 6.5 (Read this section!) 27 Feb 2007.
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.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
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.
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Michael Welzl (revised by Hans Petter Taugbøl Kragset 2015) (adapted from lectures by Pål Halvorsen, Carsten Griwodz & Olav Lysne) Data Communication:
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
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.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
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 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Sockets and Beginning Network Programming
Data Communication: Introduction to Berkeley Sockets
Sockets and Beginning Network Programming
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
UNIX Sockets COS 461 Precept 1.
Socket Programming (Cont.)
The Pocket Guide to TCP/IP Sockets: C Version
Network Programming CSC- 341
Network Programming with Sockets
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Tutorial on Socket Programming
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
Network Programming CSC- 341
UNIX Sockets Outline Homework #1 posted by end of day
TCP Sockets Programming
Advanced Network Programming spring 2007
Network Programming Chapter 12
Chapter 2 Application Layer
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Sockets.
Today’s topic: Basic TCP API
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Introduction to Berkeley Sockets Pål Halvorsen (adapted from lectures by Carsten Griwodz & Olav Lysne) Socket introduction

Big Picture Machine Machine 1 Machine 2 process A process B network Socket introduction

Goal Introduce socket API We will write two programs A “client” and a “server” Each will run on one machine the server will run on “kaksi.ifi.uio.no” (129.240.65.193) They will work as follows The client sends the text “Hello world!” to the server The server will write the received text on the screen The server sends the received text back to the client and quits The client writes the received text onto the screen and quits Socket introduction

What we want Client Server Socket introduction <necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called “sock”> <Identify the server that you want to contact> <Connect to the server> /* Send data */ write(sock, “Hello world!”, 12); /* Read data from the socket */ read(sock, buf, 12); /* Add a string termination sign, and write to the screen. */ buf[12] = ‘\0’; printf(“%s\n”, buf); <Closing code> } <necessary includes> int main() { char buf[13]; <declare some more data structures> <create a socket called “request-sock”> <Define how the client can connect> <Wait for a connection, and create a new socket “sock” for that connection> <Identify the server that you want to contact> /* read data from the sock and write it to the screen */ read(sock, buf, 12); buf[12] = ‘\0’; printf(“%s\n”, buf ); /* send data back over the connection */ write(sock, buf, 12); <Closing code> } Socket introduction

Read Write The call read(sock, buffer, n); Reads n characters From socket sock Stores them in the character array buffer The call write(sock, buffer, n); Writes n characters From character array buffer To the socket sock Same functions used for files etc. Socket introduction

Alternative Read Write The call recv(sock, buffer, n, flags); Reads n characters From socket sock Stores them in the character array buffer Flags, normally just 0, but e.g. MSG_DONTWAIT The call send(sock, buffer, n, flags); Writes n characters From character array buffer To the socket sock Flags Socket introduction

Creation of a connection One side must be the active one take the initiative in creating the connection this side is called the client The other side must be passive it is prepared for accepting connections waits for someone else to take initiative for creating a connection this side is called the server This use of the words client and server is not entirely consistent with everyday use, but for programming this is conventional Socket introduction

Special for the server side In case of TCP one socket on the server side is dedicated to waiting for a connection for each client that takes the initiative, a separate socket on the server side is created this is useful for all servers that must be able to serve several clients concurrently (web servers, mail servers) Socket introduction

To do – slightly more details Client Server <Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called “sock”> <Identify the server that you want to contact> <Connect to the server> /* Send data */ write(sock, “Hello world!”, 12); /* Read data from the socket */ read(sock, buf, 12); /* Add a string termination sign, and write to the screen. */ buf[12] = ‘\0’; printf(“%s\n”, buf); <Closing code> } <Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called “request-sock”> <Define how the client can connect> <Wait for a connection, and create a new socket “sock” for that connection> <Identify the server that you want to contact> /* read data from the sock and write it to the screen */ read(sock, buf, 12); buf[12] = ‘\0’; printf(“%s\n”, buf ); /* send data back over the connection */ write(sock, buf, 12); <Closing code> } Socket introduction

<Necessary includes> #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <string.h> These five files are needed by both client and server They include definitions and declarations as described on the following sides Some systems will have the same declarations in different files – these should work at IFI (see /usr/include on Linux & Solaris) Socket introduction

<Create a socket> Server Client /* declarations */ int sock; /* creation of the socket */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); /* declarations */ int request_sock; /* creation of the socket */ request_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); Call to the function socket() creates a transport control block (hidden in kernel), and returns a reference to it (integer used as index) sock user kernel control block control block control block Socket introduction

More about the socket call sock = socket(int domain, int type, int protocol) PF_INET, SOCK_STREAM and IPPROTO_TCP are constants that are defined in the included files The use of the constants that we used on the previous slides creates a TCP/IP socket Many other possibilities exist Domain: PF_UNIX, PF_INET, PF_INET6, … Type: SOCK_STREAM, SOCK_DGRAM, … Protocol: IPPROTO_TCP, IPPROTO_UDP, … Socket introduction

How to identify clients to accept, and servers to contact? Machine By its IP address (e.g. 129.249.65.193) Application/service/program By (IP address and) port number Some standard applications have own, “well-known” port numbers Mail: 25 Web: 80 Look in /etc/services for more Socket introduction

Address structure struct sockaddr_in is declared in include file: sin_family address family used sin_port 16-bit transport port number sin_addr 32-bit IP address defined as a new structure in_addr having one s_addr element sin_zero padding see <netinet/in.h> Defines IP address and port number in a way the Berkeley socket API needs it Socket introduction

Address structure Server Client /* declaration */ struct sockaddr_in serveraddr; /* clear the structure */ bzero(&serveraddr, sizeof(struct sockaddr_in)); /* This will be an address of the * Internet family */ serveraddr.sin_family = AF_INET; /* Add the server address */ inet_pton(AF_INET, “129.240.65.193”, &serveraddr.sin_addr); /* Add the port number */ serveraddr.sin_port = htons(2009); /* declaration */ struct sockaddr_in serveraddr; /* clear the structure */ bzero(&serveraddr, sizeof(struct sockaddr_in)); /* This will be an address of the * Internet family */ serveraddr.sin_family = AF_INET; /* Allow all own addresses to receive */ serveraddr.sin_addr.s_addr = INADDR_ANY; /* Add the port number */ serveraddr.sin_port = htons(2009); inet_pton() is new for IPv6 and may not exist yet. Oldest: serveraddr.sin_addr.s_addr = inet_addr(“129.240.65.193”); Newer: inet_aton(“129.240.65.193”, &serveraddr.sin_addr); Socket introduction

Address structure Fill address type (“family”), address and port number into the structure serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; inet_pton( AF_INET, “129.240.65.193”, &serveraddr.sin_addr.s_addr ); serveraddr.sin_port = htons( 2009 ); AF_INET a constant indicating that Internet protocols will be used INADDR_ANY A constant meaning any (Internet) address In this context: any own Internet address Socket introduction

Byte Order Different machines may have different representation of multi-byte values Consider a 16-bit integer: made up of 2 bytes increasing memory addresses high-order byte low-order byte address A+1 address A little-endian byte order 16-bit value LSB MSB high-order byte low-order byte address A address A+1 big-endian byte order Socket introduction

Byte Order: IP address example IPv4 host address: represents a 32-bit address written on paper (”dotted decimal notation”): 129.240.71.213 binary in bits: 10000001 11110000 01000111 10001011 hexadecimal in bytes: 0x81 0xf0 0x47 0x8b Little-endian: one 4 byte int on x86, StrongARM, XScale, …: 0x8b47f081 Big-endian: one 4 byte int on PowerPC, POWER, Sparc, …: 0x81f0478b in network byte order: 0x81f0478b Socket introduction

Byte Order: Translation Byte order translation makes the communication over several platforms possible htons() / htonl() host-to-network short / long translate a 16 / 32-bit integer value to network format ntohs() / ntohl() network-to-host short/long translate a 16 / 32-bit integer value to host format Little-endian (x86 etc.): ntohl(0x81f0478b) == 0x8b47f081 Big-endian (PowerPC etc.): ntohl(0x81f0478b) == 0x81f0478b Socket introduction

Presentation and Numeric Address Formats The network… …does not interpret the “dotted decimal notation” presentation format …needs a numeric binary format in network byte order inet_pton() translate the text string to a format numeric binary needed by the address structure inet_ntop() translate the network address structure to a text string Socket introduction

How far have we gotten now? Client Server  <Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called “sock”> <Identify the server that you want to contact> <Connect to the server> /* Send data */ write(sock, “Hello world!”, 12); /* Read data from the socket */ read(sock, buf, 12); /* Add a string termination sign, and write to the screen. */ buf[12] = ‘\0’; printf(“%s\n”, buf); <Closing code> }  <Necessary includes> int main() { char buf[13]; <Declare some more data structures> <Create a socket called “request-sock”> <Define how the client can connect> <Wait for a connection, and create a new socket “sock” for that connection> <Identify the server that you want to contact> /* read data from the sock and write it to the screen */ read(sock, buf, 12); buf[12] = ‘\0’; printf(“%s\n”, buf ); /* send data back over the connection */ write(sock, buf, 12); <Closing code> }       Socket introduction

Binding, Listening, Accepting and Connecting Client Server /* Connect */ connect( sock, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in)); /* Bind the address to the socket */ bind(request_sock, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in); /* Activate listening on the socket */ listen(request_sock, SOMAXCONN); /* Wait for connection */ clientaddrlen = sizeof(struct sockaddr_in); sock = accept(request_sock, (struct sockaddr*)&clientaddr, &clientaddrlen); Socket introduction

Some details about the previous slides bind(int sfd, struct sockaddr *a, socklen_t al) tells the socket on the server side which address and port number to listen to a machine can have several addresses (several network cards, loopback, …) listen( int sfs, int backlog ) newly created sockets are assumed to be active prepares the server for listening to connect requests, and initializes a queue for connect requests the second parameter (SOMAXCONN) defines how long the queue should be - in the includes Socket introduction

More details sock = accept(int sfd, struct sockaddr *a, socklen_t *al) take the first connect request from the connect request queue wait for the connect request to arrive if the queue is empty returns a new socket that the server can use to communicate with the client clientaddr contains information about the client clientaddrlen must be initialized, so accept knows the size of clientaddr connect(int sfd, struct sockaddr *serv_a, socklen_t al) connects client socket to a server that is specified in the address structure a three-way handshake is initiated for TCP possible errors ETIMEDOUT – no response (after several tries) and timer expired ECONNREFUSED – server not running or not allowed to connect ExxxUNREACH – HOST or NET not reachable Socket introduction

Closing of Sockets Client Server /* Close the socket */ close(sock); /* Close both sockets */ close(sock); close(request_sock); Note that the semantics of close depends On the kind of protocol Some possible extra settings All data that has not been read yet may be thrown away Socket introduction

Complete Client Client Client ctd. Socket introduction #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <string.h> int main() { /* Declarations */ struct sockaddr_in serveraddr; int sock; char buf[13]; /* Create socket */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); /* Clear address structure */ bzero(&serveraddr, sizeof(struct sockaddr_in)); /* Add address family */ serveraddr.sin_family = AF_INET; /* Add IP address of kaksi.ifi.uio.no */ inet_pton(AF_INET, “129.240.65.193”, &serveraddr.sin_addr); /* Add the port number */ serveraddr.sin_port = htons(2009); /* Connect */ connect(sock, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in)); /* Send data */ write(sock, “Hello world!”, 12 ); /* Read data */ read(sock, buf, 12 ); /* add string end sign, write to screen*/ buf[12] = ‘\0’; printf(“%s\n”, buf); /* Close socket */ close(sock); } Socket introduction

Complete Server Server Server ctd. Socket introduction #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <string.h> int main() { /* Declarations */ struct sockaddr_in serveraddr; struct sockaddr_in clientaddr; int clientaddrlen; int request_sock, sock; char buf[13]; /* Create socket */ request_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); /* Fill in the address structure */ bzero(&serveraddr, sizeof(struct sockaddr_in)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; serveraddr.sin_port = htons(2009); /* Bind address to socket */ bind(request_sock, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr_in)); /* Activate connect request queue */ listen(request_sock, SOMAXCONN); /* Receive connection */ clientaddrlen = sizeof(struct sockaddr_in); sock = accept(request_sock, (struct sockaddr*)&clientaddr, &clientaddrlen); /* Read data from socket and write it */ read(sock, buf, 12); buf[12] = ‘\0’; printf(“%s\n”, buf); /* Send data back over connection */ write(sock, buf, 12); /*Close sockets */ close(sock); close(request_sock); } Socket introduction

Summary of Socket functions for our elementary TCP client-server bind() Client socket() listen() connect() accept() connection establishment send data: “hello world” write() read() write() send back received data: “hello world” read() close() close() Socket introduction

Compilation of socket programs The example can be downloaded from the web pages (~inf1060/programs/client-server-example) IFI’s solaris machines gcc client1.c –o client –lsocket –lnsl cc client1.c –o client –lsocket –lnsl IFI’s linux machines gcc client1.c –o client Cygwin on windows Similar for server1.c For testing, run server on kaksi (or change the address in the client) and start client on another machine Socket introduction

Complete Server Iterative servers? Server Server ctd. ... int main() { /* Declarations */ /* Create socket */ request_sock = socket(...); /* Fill in the address structure */ /* Bind address to socket */ bind(...); /* Activate connect request queue */ listen(...); /* Receive connection */ sock = accept(...); /* Process the request*/ ... /*Close sockets */ close(sock); close(request_sock); } Iterative servers? Socket introduction

Iterative Servers Concurrent servers? Server Server ctd. ... int main() { /* Declarations */ /* Create socket */ request_sock = socket(...); /* Fill in the address structure */ /* Bind address to socket */ bind(...); /* Activate connect request queue */ listen(...); for (;;) { /* Receive connection */ sock = accept(...); /* Process the request*/ ... /*Close sockets */ close(sock); } close(request_sock); Concurrent servers? Socket introduction

Concurrent Iterative Servers Server ctd. ... int main() { /* Declarations */ pid_t pid; /* Create socket */ request_sock = socket(...); /* Fill in the address structure */ /* Bind address to socket */ bind(...); /* Activate connect request queue */ listen(...); for (;;) { /* Receive connection */ sock = accept(...); if ((pid = fork()) == 0) { close(request_sock); /* Process the request*/ ... /*Close sockets */ close(sock); exit(0) } Socket introduction

Literature “Berkeley UNIX System Calls and Interprocess Communication”, Lawrence Besaw, University of Wisconsin Will be available through the course web pages Many books: Kurose/Ross, “Computer Networking: A Top-Down Approach Featuring the Internet”, 2nd ed., Addison-Wesley Andrew Tanenbaum, “Computer Networks”, 4th ed., Prentice Hall W. Richard Stevens, “Unix Network Programming – Networking APIs: Sockets and XTI”, volume 1, 2nd ed., Prentice Hall Socket introduction