没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
RPG IV Socket Tutorial
Scott Klement
RPG 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: Creating 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
iii
6.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.............................................................................................................................156
8.5. Sample UDP server....................................................................................................................................157
8.6. Sample UDP client.....................................................................................................................................161
8.7. Trying it out................................................................................................................................................165
Colophon....................................................................................................................................................................167
iv
Chapter 1. Introduction to TCP and Sockets
Written by Scott Klement.
1.1. TCP/IP Concepts and Terminology
This section is an introduction to TCP/IP programming using a sockets API. (Sockets can also be used to work with
other network protocols, such as IPX/SPX and Appletalk, but that is beyond the scope of this document.) The
standard socket API was originally developed in the Unix world, but has been ported to OS/400 as part of the
"Unix-type" APIs, and a modified version was also ported to the Windows platform under the name "Windows
Sockets" (or "Winsock" for short)
Usually when someone refers to "TCP/IP" they are referring to the entire suite of protocols, all based on the Internet
Protocol ("IP"). Unlike a single network, where every computer is directly connected to every other computer, an
"inter-network" (or "internet") is a collection of one or more networks. These networks are all connected together to
form a larger "virtual network". Any host on this virtual network can exchange data with any other host, by referring
to the hosts "address".
The address is a 32-bit number which is unique across the entire internet. Typically, this number is broken into 4
8-bit pieces, separated by periods to make it easier for humans to read. This human readable format is called
"dotted-decimal" format, or just "dot-notation". An address displayed in this fashion looks something like
"192.168.66.21".
Different parts of this "IP Address" are used to identify which network a host is located on, and the rest of the address
is used to identify the host itself. Which part of the address and which part is the host is determined by a "network
mask" (or "netmask" for short.) The netmask is another 32-bit number which acts like like a "guide" to the IP address.
Each bit that is turned on in the netmask means that the corresponding bit in the IP address is part of the network’s
address. Each bit that is turned off means that the corresponding bit in the IP address is part of the host’s address.
Here’s an example of an IP address and netmask:
dotted-decimal same number in binary format:
-------------- -----------------------------------
IP Address: 192.168.66.21 11000000 10101000 01000010 00010101
Netmask: 255.255.255.0 11111111 11111111 11111111 00000000
Network Address is: 192.168.66 11000000 10101000 01000010
Host Address is: .21 00010101
A slightly more complicated example:
dotted-decimal same number in binary format:
-------------- -----------------------------------
IP Address: 192.168.41.175 11000000 10101000 00101001 10101111
Netmask: 255.255.255.248 11111111 11111111 11111111 11111000
Network Address is: 192.168.41.168 11000000 10101000 00101001 10101
Host Address is: 7 111
1
剩余170页未读,继续阅读
资源评论
中国彭于晏杭州分晏
- 粉丝: 87
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功