Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Socket Programming Application Programming Interface.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
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.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
Introduction to Project 1 Web Client and Server Jan 2006.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
ECE 4110 – Internetwork Programming Client-Server Model.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
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.
CSTP FS01CS423 (cotter)1 Protocols 2 References: RFC’s 791, 793, 768, 826.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
 Wind River Systems, Inc Chapter - 13 Network Programming.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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.
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.
Chapter 2 Applications and Layered Architectures Sockets.
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.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Assignment 3 A Client/Server Application: Chatroom
Socket programming in C
CS 1652 Jack Lange University of Pittsburgh
Tutorial on Socket Programming
Transport layer API: Socket Programming
Recitation 11 – 4/29/01 Outline Sockets Interface
UDP Sockets Programming
Chapter 2 Application Layer
Socket Programming(1/2)
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Socket programming in C
Presentation transcript:

Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns a small integer (socket descriptor) that the program uses to reference the socket –Application program can then use read and write system calls on the socket –Program closes the socket when finished using it

Creating a Socket The socket system call: int sd; // socket descriptor int pf; // protocol family (one of PF_INET, // PF_PUP, PF_APPLETALK, // PF_UNIX, etc.) int type;// type of service (one of SOCK_RAW, // SOCK_DGRAM, SOCK_STREAM) int protocol;// specific protocol in pf sd = socket(pf, type, protocol)// create a new socket

Binding a Socket to an Internet Source Address (cont) The bind system call: int sd; // socket descriptor struct sockaddr_in addr;// structure specifying source // address int len;// length (in bytes) of // addr struct bind(sd,addr,len)// bind socket to source IP // address

Connecting a Socket to a Destination Address The connect system call: int sd; // socket descriptor struct sockaddr addr;// structure specifying dest addr int len;// length (in bytes) of // addr struct connect(sd,addr,len)// connect socket to // dest address Can also use a sockaddr_in struct for dest address

Sending Data Through a Socket The write system call: int sd; // socket descriptor void *buffer;// address of the data to be sent int len;// number of bytes to send write(sd,buffer,len)// send data through socket

Receiving Data Through a Socket The read system call: int sd; // socket descriptor void *buffer;// address in memory at which // to store the data int len;// number of bytes to receive read(sd,buffer,len)// receive data through socket

Closing a Socket The close system call: int sd; // socket descriptor close(sd)// close socket

Datagram Socket Example: Receiver #include main() { int sock, len; struct sockaddr_in name; char buf [1024]; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) exit(-1); name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = 0; if (bind(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1); len = sizeof(name); if (getsockname(sock, (struct sockaddr *) &name, &len)) exit(-1); printf("Receiver listening on port %d\n",ntohs(name.sin_port)); if (read(sock, buf, 1024) < 0) exit(-1); printf("%s\n",buf); close(sock); }

Datagram Socket Example: Sender #include main(int argc, char **argv) { int sock; struct sockaddr_in name; struct hostent *hp, *gethostbyname(); if (argc != 3) exit(-1); sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) exit(-1); hp = gethostbyname("prime.cs.ohiou.edu"); if (hp == 0) exit(-1); bcopy(hp->h_addr, &name.sin_addr, hp->h_length); name.sin_family = AF_INET; name.sin_port = htons(atoi(argv[1])); if (connect(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1); if (write(sock, argv[2], (strlen(argv[2])+1)) < 0) exit(-1); close(sock); }

Obtaining Local Socket Addresses The getsockname system call: int sd; // socket descriptor struct sockaddr *addr; // address structure to be filled int *len;// pointer to integer that will // contain the length of the // address getsockname(sd, addr, len); // obtain local socket address

Obtaining and Setting Socket Options The getsockopt system call: int sd; // socket descriptor int level; // option for socket or protocol? int optionid;// which specific option? void *optionval;// where to place the requested // value int *len; // length of the optionval getsockopt(sd, level, optionid, optionval, len); // obtain // socket opt

Obtaining and Setting Socket Options (cont) Values for level (from ): –SOL_SOCKEToption for the socket –IPPROTO_IPoption for IP –IPPROTO_ICMPoption for ICMP –IPPROTO_TCPoption for TCP –IPPROTO_UDPoption for UDP

Obtaining and Setting Socket Options (cont) Values for optionid (from ): –SO_TYPEsocket type –SO_SNDBUFsend buffer size –SO_RCVBUFreceive buffer size –SO_DEBUGdebugging info available? –SO_ACCEPTCONNsocket listening enabled? –SO_BROADCASTbroadcast supported? –SO_REUSEADDRaddress reuse allowed? –SO_KEEPALIVEkeep alive after close?

Obtaining and Setting Socket Options (cont) The setsockopt system call: int sd; // socket descriptor int level; // option for socket or protocol? int optionid;// which specific option? void *optionval;// option value int *len; // length of the option value setsockopt(sd, level, optionid, optionval, len); // set option // value

Socket Options for Servers The listen system call: int sd; // socket descriptor int length; // length of request queue listen(sd, length)// set socket request queue // length Can only be used for SOCK_STREAM sockets

Servers: Accepting Connections The accept system call: int sd; // socket descriptor struct sockaddr *name;// address of client int *len; // length of address struct newsock = accept(sd, addr, len) // accept connection A new socket is created that connects to the client Server handles request, sends reply, closes newsock

Servers That Provide Multiple Services The select system call: int ndesc; // check descriptors 0...ndesc-1 void *indesc;// descriptors to check for input void *outdesc; // descriptors to check for output void *excdesc;// descriptors to check for exceptions int *timeout;// how long to wait for a connection nready = select(ndesc, indesc, outdesc, excdesc, timeout) // determine which descriptors are // ready for I/O

Servers That Provide Multiple Services (cont) The select system call: nready = select(ndesc, indesc, outdesc, excdesc, timeout) Returns the number of descriptors from the specified set that are ready for I/O A process can use select to communicate over more than one socket at a time Typically each socket provides a distinct service

Miscellaneous (Useful) System Calls The gethostname system call: char *name;// buffer to store name int length;// size of buffer in bytes gethostname(name, length)// get name of host Defined in include file Process can learn host it’s running on

Miscellaneous (Useful) System Calls (cont) The network byte order conversion routines: –Network-to-host (short), ntohs, convert a short int from network byte order to host byte order –Network-to-host (long), ntohl, convert a long int from network byte order to host byte order –Host-to-network (short), htons, convert a short int from network byte order to host byte order –Host-to-network (long), htonl, convert a long int from network byte order to host byte order

Miscellaneous (Useful) System Calls (cont) Testing htons: #include main(int argc, char **argv) { if (argc != 2) exit(-1); printf("%d\n",atoi(argv[1])); printf("%d\n",htons(atoi(argv[1]))); }

Miscellaneous (Useful) System Calls (cont) The gethostbyname system call: struct hostent *h;// hostent structure char *name;// host name h = gethostbyname(name);// fill in hostent with // info about name

Miscellaneous (Useful) System Calls (cont) The hostent structure (defined in ): struct hostent { char *h_name;/* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */

OHCE Server Example Check that program is running on the proper host Create a socket on which the server will receive requests Bind the socket to the well-known port for the ohce service Loop forever –Receive request and process it –Send reply to port and machine specified in the client’s request

OHCE Client Example Create a socket on which to receive the server’s reply Bind it to some port on the local machine Create a socket on which to send a request to the server Bind it to the port the server’s listening on Create a request message and send it to the server Wait for a reply and print out the result

Summary The client-server model is widely-used for application interaction over a TCP/IP internet –Server: a program that offers a service that can be reached over a network Examples: web server, file server –Client: a program that requests a service from a server Examples: ping, browser Application programs typically interact with the networking software by using sockets and system calls: –Socket – create a socket –Bind – bind a socket to a port –Read/write – get/put data in a socket –Etc.