NYU CSCI-GA 2262 - SOCKETS - A PROGRAMMERS INTRODUCTION

Unformatted text preview:

SSOCKETSOCKETS: A P: A PROGRAMMERROGRAMMER''SSIINTRODUCTIONNTRODUCTIONZornitza GenovaUniversity of South Florida1 VERSIONS OF SOCKETS ..................................................................................................22 SOCKETS, SOCKET DESCRIPTORS, PORTS, AND CONNECTIONS .........................33 THE CLIENT/SERVER MODEL OF COMMUNICATION..............................................5Running a Sockets Program on a Windows Machine Not Connected to a Network.....5Running a Sockets Program on a Windows Machine Connected to a Network, WhenBoth Server and Client Reside on the Same Machine ...................................................64 SOCKETS ELEMENTS.......................................................................................................6Socket Creation..............................................................................................................6The Socket Address .......................................................................................................7Bind to a local port.........................................................................................................8Data representation and byte ordering ..........................................................................9Connecting a socket .....................................................................................................10The gethostbyname()function call.....................................................................11Listening for an incoming client connection................................................................15Accepting a connection from a client ..........................................................................15Sending and Receiving messages on a socket .............................................................17Closing a socket ...........................................................................................................18Report errors ................................................................................................................18Example TCP/IP Client Program (Initiating Connection)...........................................20Example TCP/IP server program (passively awaiting connection) .............................225 STREAM AND DATAGRAM SOCKETS........................................................................24Example UDP Client Program (Initiate Connections).................................................25Example UDP Server Program (Passively Await Connection) ...................................276 RUN-TIME PROGRAM CONTROL .................................................................................28Nonblocking socket calls .............................................................................................28Asynchronous I/O (Signal Driven I/O)........................................................................307 REMOTE EXECUTION OF A WINDOWS CONSOLE APPLICATION......................34Local code....................................................................................................................34Remote Code................................................................................................................373/16/03-2The Sockets Application Program Interface (API) provides a library of functions thatprogrammers can use to develop network aware applications. It has the functionality ofidentifying endpoints of the connection, establishing the communication, allowing messages tobe sent, waiting for incoming messages, terminating the communication, and error handling. Theoperating system used and the programming language both determine the specific Sockets API.The different flavors of the Sockets API are the de facto standard interface to TCP/IPprotocols that was originally defined by BSD UNIX and was later adopted by other operatingsystems. It is an open standard that provides for source code portability. Hence, we are going toconcentrate on only two of the most widely used interfaces – the Berkley Software DistributionSockets (BSD) as introduced for UNIX, and its slight modification the Windows Sockets(WinSock) API from Microsoft.This sockets material is intended for the C language programmer. (It provides externalreferences for the C++, Visual Basic, and PASCAL languages.) The Windows operating systemis in the center of our discussion. At the same time, topics from the original BSD UNIXspecification are introduced in order to point out (usually minor) differences in the socketsspecifications for the two operating systems. Basic knowledge of the TCP/IP and UDP networkprotocols is assumed. Most of the code would compile on both Windows and UNIX likesystems.1 VERSIONS OF SOCKETSThe original BSD specification works well for UNIX-like systems (see Table 1).We do cover C language sockets exclusively, but most other programming languages, suchas C++, Visual Basic, PASCAL, etc., can take advantage of the Winsock API, as well. The onlyrequirement is that the language has to recognize dynamic link libraries (DLLs). In a 32-bitWindows environment you will need to import the wsock32.lib to take advantage of theWinSock API. This library has to be linked, so that at run time the dynamic link library3/16/03-3wsock32.dll gets loaded. wsock32.dll runs over the TCP/IP stack. Windows NT,Windows 2000, and Windows 95 include the file wsock32.dll by default. When you createyour executables, if you link with wsock32.lib library, you will implicitly link thewsock32.dll at run time, without adding lines of code to your source file. For WinsockVersions 1.1 and 2 development documentation, articles, software, and sample code, please referto URL http://www.stardust.com/.A good further reference on the topic can be found from the Web site of Windows SocketsNetwork Programming by Bob Quinn, at URL http://www.sockets.com/a_d.htm . For the VisualBasic programmer - the book Client/Server Programming with Microsoft Visual Basic byKenneth L. Spencer and Ken Miller is a good first sockets reference; a collection of TCP/IPnetworking components and libraries for Visual Basic developers is available athttp://www.catalyst.com/. An old but well-known C++ reference including a class libraryavailable on CD-ROM is Win32 Network Programming: Windows 95 and Windows NTNetwork Programming Using MFC by Ralph Davis at URL:http://cseng.aw.com/book/author/0,3832,0201489309,00.html.For a comprehensive family tree of available UNIX-like


View Full Document

NYU CSCI-GA 2262 - SOCKETS - A PROGRAMMERS INTRODUCTION

Download SOCKETS - A PROGRAMMERS INTRODUCTION
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view SOCKETS - A PROGRAMMERS INTRODUCTION and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view SOCKETS - A PROGRAMMERS INTRODUCTION 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?