Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Socket Programming Application Programming Interface.
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.
Windows Sockets Purpose Windows Sockets 2 (Winsock) enables programmers to create advanced internet, intranet, and other network-capable applications to.
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.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
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.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS6: Device Management 6.1. Principles of I/O.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
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.
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.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
Socket Programming.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
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.
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.
Socket Abstraction and Interprocess Communication
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
CS 1652 Jack Lange University of Pittsburgh
Socket Programming in C
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Review: TCP Client-Server Interaction
Transport layer API: Socket Programming
Socket Abstraction and Interprocess Communication
Socket Programming in C
Socket Programming.
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Jim Fawcett CSE 681 – Software Modeling & Analysis Summer 2003
Presentation transcript:

Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets Programming

2 Copyright Notice © David A. Solomon and Mark Russinovich These materials are part of the Windows Operating System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E. Russinovich with Andreas Polze Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic environments (and not for commercial use)

3 Roadmap for Section A.2 General Concepts - Berkeley Sockets Creating a socket Binding an address Accepting connections Exchanging data Closing a connection Managing multiple connections with select()

4 Winsock Features Support for scatter-gather and asynchronous application I/O Quality of service (QoS) conventions so that applications can negotiate latency and bandwidth requirements when the underlying network supports QoS Extensibility so that Winsock can be used with protocols other than those Windows requires it to support Support for integrated namespaces other than those defined by a protocol an application is using with Winsock. A server can publish its name in Active Directory, for example, and using namespace extensions, a client can look up the server's address in Active Directory Support for multipoint messages where messages transmit to multiple receivers simultaneously

5 Windows Socket Programming Berkeley Socket programs will port to Window Sockets Exceptions: Call WSAStartup() to initialize Windows Socket DLL Use ioctlsocket() (non-portable) to configure the socket _read() and _write() can be used on sockets, but only after converting the socket descriptor to a file handle via _open_osfhandle() Use closesocket() (non-portable) rather than close to close a socket Call WSACleanup() to shut down the DLL

6 Berkeley 4.3 UNIX Sockets – connection-oriented Server socket() bind() listen() accept() read() write() socket() Client connect() write() read() connection establishment data (request) data (reply) blocks until connection from client

7 Berkeley 4.3 UNIX Sockets - connectionless Server socket() bind() recvfrom() sendto() socket() Client sendto() recvfrom() data (request) data (reply) blocks until data received from client

8 Unix SYS V.3 Transport Layer Interface – connection-oriented Server t_open() t_bind() t_listen() t_rcv() t_snd() t_open() Client t_connect() t_snd() t_rcv() data (request) data (reply) blocks until connection from client t_alloc() t_bind() t_alloc() t_accept() connection establishment

9 Unix SYS V.3 Transport Layer Interface - connectionless Server t_open() t_bind() t_rcvudata() t_sndudata() t_open() Client t_sndudata() t_rcvudata() data (request) data (reply) blocks until data received from client t_alloc() t_bind() t_alloc()

10 Create a socket af: An address format specification. The only format currently supported is AF_INET, which is the ARPA Internet address format. type: A type specification for the new socket. protocol: A particular protocol to be used with the socket, or 0 if the caller does not wish to specify a protocol. #include SOCKET socket ( int af, int type, int protocol );

11 Accept a connection on a socket s: A descriptor identifying a socket which is listening for connections after a listen(). addr: An optional pointer to a buffer which receives the address of the connecting entity, as known to the communications layer. The exact format of the addr argument is determined by the address family established when the socket was created. addrlen: An optional pointer to an integer which contains the length of the address addr. #include SOCKET accept ( SOCKET s, struct sockaddr FAR * addr, int FAR * addrlen );

12 struct sockaddr From winsock.h (Windows) or /usr/include/sys/socket.h (UNIX) /* * Structure used by kernel to store most * addresses. */ struct sockaddr { u_char sa_len; /* total length */ u_char sa_family; /* address family */ char sa_data[14]; /* actually longer; address value*/ }; #define SOCK_MAXADDRLEN 255 /* longest possible addresses */

13 Associate a local address with a socket s: A descriptor identifying an unbound socket. name: The address to assign to the socket. namelen: length of the name struct sockaddr { u_short sa_family; char sa_data[14]; }; #include int bind ( SOCKET s, const struct sockaddr FAR * name, int namelen );

14 Internet address family In the Internet address family, a name consists of several components. For SOCK_DGRAM and SOCK_STREAM, the name consists of three parts: a host address, the protocol number (set implicitly to UDP or TCP, respectively), and a port number which identifies the application. If an application does not care what address is assigned to it, it may specify an Internet address equal to INADDR_ANY, a port equal to 0, or both. If the Internet address is equal to INADDR_ANY, any appropriate network interface will be used; this simplifies application programming in the presence of multi-homed hosts. If the port is specified as 0, the Windows Sockets implementation will assign a unique port to the application with a value between 1024 and The application may use getsockname() after bind() to learn the address that has been assigned to it getsockname() will not necessarily fill in the Internet address until the socket is connected; several Internet addresses may be valid if the host is multi-homed.

15 Example: bind to an reserved port SOCKADDR_IN sin; SOCKET s; u_short alport = IPPORT_RESERVED; /* 1024 */ sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; for (;;) { sin.sin_port = htons(alport); if (bind(s, (LPSOCKADDR)&sin, sizeof (sin)) == 0) { /* it worked */ } if ( GetLastError() != WSAEADDRINUSE) { /* fail */ } alport--; if (alport == IPPORT_RESERVED/2 ) { /* fail--all unassigned reserved ports are in use.*/ } }

16 Close a socket This function closes a socket. releases the socket descriptor s, so that further references to s will fail with the error WSAENOTSOCK. If this is the last reference to the underlying socket, the associated naming information and queued data are discarded. Semantics influenced by socket options: #include int closesocket ( SOCKET s ); OptionInterval Type of close Wait for close? SO_DONTLINGER Don‘t care GracefulNo SO_LINGERZeroHardNo SO_LINGERNon-zeroGracefulYes

17 Establish a connection to a peer s: A descriptor identifying an unconnected socket. name: The name of the peer to which the socket is to be connected. namelen: The length of the name. create a connection to the specified foreign association. The parameter s specifies an unconnected datagram or stream socket #include int connect ( SOCKET s, const struct sockaddr * name, int namelen );

18 Establish a socket to listen for incoming connection s: A descriptor identifying a bound, unconnected socket. backlog: The maximum length to which the queue of pending connections may grow. typically used by servers that could have more than one connection request at a time: if a connection request arrives with the queue full, the client will receive an error with an indication of WSAECONNREFUSED #include int listen ( SOCKET s, int backlog );

19 Receiving data from a socket (connection-oriented) s: A descriptor identifying a connected socket. buf: A buffer for the incoming data. len: The length of buf. flags: Specifies the way in which the call is made. #include int recv ( SOCKET s, char * buf, int len, int flags );

20 Receive a datagram and store the source address (connectionless) s: A descriptor identifying a bound socket. buf: A buffer for the incoming data. len: The length of buf. flags: Specifies the way in which the call is made. from: An optional pointer to a buffer which will hold the source address upon return. fromlen: An optional pointer to the size of the from buffer. #include int recvfrom ( SOCKET s, char * buf, int len, int flags, struct sockaddr * from, int * fromlen );

21 Determine the status of one or more sockets, waiting if necessary. nfds: This argument is ignored and included only for the sake of compatibility. readfds: An optional pointer to a set of sockets to be checked for readability. writefds: An optional pointer to a set of sockets to be checked for writability exceptfds: An optional pointer to a set of sockets to be checked for errors. timeout: The maximum time for select() to wait, or NULL for blocking operation. #include int select ( int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, const struct timeval * timeout );

22 Send data on a connected socket (connection-oriented) s: A descriptor identifying a connected socket. buf: A buffer containing the data to be transmitted. len: The length of the data in buf. flags: Specifies the way in which the call is made. #include int send ( SOCKET s, const char * buf, int len, int flags );

23 Send data to a specific destination (connectionless) s: A descriptor identifying a socket. buf: A buffer containing the data to be transmitted. len: The length of the data in buf. flags: Specifies the way in which the call is made. to: An optional pointer to the address of the target socket. tolen: The size of the address in to. #include int sendto ( SOCKET s, const char * buf, int len, int flags, const struct sockaddr * to, int tolen );

24 Further Reading Mark E. Russinovich and David A. Solomon, Microsoft Windows Internals, 4th Edition, Microsoft Press, 2004; Windows Sockets (from pp. 791) Abraham Silberschatz, Peter B. Galvin, Operating System Concepts, John Wiley & Sons, 6th Ed., 2003; Chapter 15 - Distributed System Structures W. Richard Stevens, Unix Network Programming, Prentice Hall Software Series, 1990; (The Book) Chapter 6 - Berkeley Sockets