Programming Socket Programming
Total Page:16
File Type:pdf, Size:1020Kb
IBM i Version 7.2 Programming Socket programming IBM Note Before using this information and the product it supports, read the information in “Notices” on page 179. This edition applies to version IBM i 7.2 (product number 5770-SS1) and to all subsequent releases and modifications until otherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC) models nor does it run on CISC models. This document may contain references to Licensed Internal Code. Licensed Internal Code is Machine Code and is licensed to you under the terms of the IBM License Agreement for Machine Code. © Copyright International Business Machines Corporation 2001, 2013. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Socket programming............................................................................................. 1 What's new for IBM i 7.2..............................................................................................................................1 PDF files for Socket programming...............................................................................................................1 Prerequisites for socket programming........................................................................................................3 How sockets work........................................................................................................................................3 Socket characteristics..................................................................................................................................6 Socket address structure ...................................................................................................................... 7 Socket address family............................................................................................................................ 8 Socket type...........................................................................................................................................12 Socket protocols...................................................................................................................................13 Basic socket design................................................................................................................................... 14 Creating a connection-oriented socket............................................................................................... 14 Creating a connectionless socket........................................................................................................ 21 Designing applications with address families..................................................................................... 26 Advanced socket concepts........................................................................................................................39 Asynchronous I/O.................................................................................................................................39 Secure sockets..................................................................................................................................... 43 Client SOCKS support...........................................................................................................................51 Thread safety........................................................................................................................................54 Nonblocking I/O................................................................................................................................... 54 Signals.................................................................................................................................................. 56 IP multicasting..................................................................................................................................... 57 File data transfer—send_file() and accept_and_recv()..........................................................58 Out-of-band data..................................................................................................................................58 I/O multiplexing—select().................................................................................................................... 60 Socket network functions.................................................................................................................... 60 Domain Name System support............................................................................................................ 61 Berkeley Software Distribution compatibility..................................................................................... 64 UNIX 98 compatibility..........................................................................................................................66 Descriptor passing between processes: sendmsg() and recvmsg()...................................................70 Sockets-related User Exit Points......................................................................................................... 72 Socket scenario: Creating an application to accept IPv4 and IPv6 clients..............................................74 Example: Accepting connections from both IPv6 and IPv4 clients................................................... 75 Example: IPv4 or IPv6 client ...............................................................................................................80 Socket application design recommendations...........................................................................................82 Examples: Socket application designs......................................................................................................86 Examples: Connection-oriented designs.............................................................................................86 Example: Using asynchronous I/O.................................................................................................... 108 Examples: Establishing secure connections..................................................................................... 114 Example: Using gethostbyaddr_r() for threadsafe network routines....................................... 138 Example: Nonblocking I/O and select()....................................................................................... 140 Using poll() instead of select()................................................................................................. 145 Example: Using signals with blocking socket APIs...........................................................................150 Examples: Using multicasting with AF_INET.................................................................................... 153 Example: Updating and querying DNS...............................................................................................158 Examples: Transferring file data using send_file() and accept_and_recv() APIs...............161 Xsockets tool........................................................................................................................................... 167 Configuring Xsockets......................................................................................................................... 167 Configuring Xsockets to use a Web browser.....................................................................................170 Using Xsockets .................................................................................................................................. 174 Deleting objects created by the Xsockets tool..................................................................................176 iii Customizing Xsockets........................................................................................................................ 176 Serviceability tools.................................................................................................................................. 176 Notices..............................................................................................................179 Programming interface information........................................................................................................180 Trademarks..............................................................................................................................................180 Terms and conditions.............................................................................................................................. 181 iv Socket programming A socket is a communications connection point (endpoint) that you can name and address in a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes. The processes that use a socket can reside on the same system or different systems on different networks. Sockets are useful for both stand-alone and network applications. Sockets allow you to exchange information between processes on the same machine or across a network, distribute work to the most efficient machine, and they easily allow access to centralized data. Socket application program interfaces (APIs) are the network standard for TCP/IP. A wide range of operating systems support socket APIs. IBM® i sockets support multiple transport and networking protocols. Socket system