La lecture à portée de main
Description
Informations
Publié par | Machug |
Nombre de lectures | 101 |
Langue | English |
Extrait
RPG IV Socket Tutorial
Scott KlementRPG IV Socket Tutorial
by Scott Klement
Copyright © 2001, 2002 by Scott Klement
This tutorial strives to teach network programming using sockets to AS/400 or iSeries programmers who use the
RPG IV programming language.
It is assumed that the reader of this tutorial is already familiar with the RPG IV language, including the use of
prototypes, subprocedures, service programs and pointers in an RPG IV environment.Table of Contents
1. Introduction to TCP and Sockets ............................................................................................................................1
1.1. TCP/IP Concepts and Terminology................................................................................................................1
1.2. Overview of a TCP communications session.................................................................................................3
2. Looking up host names & services ..........................................................................................................................6
2.1. Services and Ports ..........................................................................................................................................6
2.2. Host names and addresses ..............................................................................................................................8
3. Socket API calls to create our first client program..............................................................................................14
3.1. The socket() API call....................................................................................................................................14
3.2. The connect() API call .................................................................................................................................15
3.3. The send() and recv() API calls....................................................................................................................17
3.4. Translating from ASCII to EBCDIC............................................................................................................19
3.5. The close() API call......................................................................................................................................20
3.6. Our first client program ................................................................................................................................21
4. Improving our client program ...............................................................................................................................27
4.1. improvement #1: header files .......................................................................................................................27
4.2. improvement #2: error handling...................................................................................................................30
4.3. improvement #3: Creating a Read Line utility.............................................................................................33
4.4. improvement #4: a Write Line utility ............................................................................................37
4.5. Packaging our utilities into a service program .............................................................................................39
4.6. improvement #5: Stripping out the HTTP response.....................................................................................41
4.7. improvement #6: Sending back escape messages ........................................................................................43
4.8. improvement #7: Displaying the data using DSPF ......................................................................................45
4.9. Our updated client program..........................................................................................................................46
5. Creating server programs ......................................................................................................................................51
5.1. Introduction to server programs ...................................................................................................................51
5.2. The bind() API call.......................................................................................................................................52
5.3. The listen() API call .....................................................................................................................................54
5.4. The accept() API call....................................................................................................................................54
5.5. Our first server program ...............................................................................................................................56
5.6. Testing server programs ...............................................................................................................................60
5.7. Making our program end..............................................................................................................................62
5.8. The setsockopt() API call.............................................................................................................................63
5.9. The revised server program..........................................................................................................................67
6. Handling many sockets at once using select().......................................................................................................73
6.1. Overview of handling many clients..............................................................................................................73
6.1.1. The "single program" approach:......................................................................................................73
6.2. The select() API call.....................................................................................................................................74
6.3. Utility routines for working with select().....................................................................................................76
6.4. A combination client/server example...........................................................................................................80
6.5. Blocking vs. non blocking sockets...............................................................................................................87
6.6. The fcntl() API call ......................................................................................................................................88
iii6.7. A multi client example of our server...........................................................................................................90
6.8. Adding some refinements to our approach.................................................................................................102
6.9. A Chat Application.....................................................................................................................................103
7. Handling many sockets by spawning jobs ..........................................................................................................119
7.1. Overview of job spawning approach..........................................................................................................119
7.2. The takedescriptor() and givedescriptor() API calls ..................................................................................120
7.3. Retrieving the internal job id......................................................................................................................121
7.4. Communicating the job information ..........................................................................................................124
7.5. Our server as a multi job server.................................................................................................................126
7.6. Working towards our next example............................................................................................................134
7.7. Validating a user id and password..............................................................................................................137
7.8. Running with the user’s authority ..............................................................................................................137
7.9. A "generic server" example........................................................................................................................138
7.10. Trying the "generic server" out ................................................................................................................150
8. The User Datagram Protocol ...............................................................................................................................153
8.1. Overview of using UDP .............................................................................................................................153
8.2. Creating a socket for use with UDP ...........................................................................................................154
8.3. The sendto() API call .................................................................................................................................154
8.4. The recvfrom() API call ............................................