文章是从我的网志中贴过来的,其中的图片可能过不来,看完整内容,请访问我的网志:
点击进入《DOS编程技术》
windows下作网络编程不是一件很难的事,但在DOS下就不是很容易了,对很多人来说甚至是无从下手,本文详细阐述在DOS下进行网络编程的方法,下一篇文章讲给出一个具体的实例。
要在DOS下进行网络编程,首先要有一个Packet Driver,这是一个与硬件相关的驱动程序,符合FTP Software提出的PC/TCP Packet Driver规范,有兴趣的读者可以在下面地址下载这份规范:
http://blog.hengch.com/specification/packetdriver.pdf
我使用的机器主板上的网络芯片是rt8139,对应的Packet Driver是rtspkt.exe,驱动方法是在autoexec.bat中加入下面这一行:
@rtspkt 0x62
其中,0x62为中断向量,如果在你的机器上这个中断向量已经被占用,你可以改成其他的未被占用的中断向量号,按照PC/TCP规范,应该在0x60----0x80之间。
有了Packet Driver后,我们还需要有一个好用的能够提供TCP/IP Socket编程接口的函数库,在DJGPP下我们建议使用WATT-32库,这个库比DJGPP官方网站上提供的WATTCP库内容更加丰富,而且文档完整和范例程序丰富,可以在下面网站上下载到:
http://www.bgnett.no/~giva/
WATT-32是以源代码的形式发行的,所以在使用前需要自行进行编译链接,整个过程如下(以下步骤是建立在你已经按照前面的网志文章《在DOS下的DJGPP+RHIDE安装实作》正确安装完毕DJGPP和RHIDE):
首先从上述网址上下载WATT-32,共有3个zip包,如下:
watt32b*.zip,watt32s*.zip,watt32d*.zip
其中“*”会随版本号不同有所不同。
通过U盘或其他媒介作为载体把3个文件拷贝到要配置的机器上,由于DOS不支持长文件名,需要把这三个文件分别改成:watt32b.zip,watt32s.zip和watt32d.zip
将三个文件解压缩到一个子目录下,例如:c:\net\watt
c:\>md net
c:\>md net\watt
c:\>unzip32 watt32b.zip -d c:\net\watt
c:\>unzip32 watt32s.zip -d c:\net\watt
c:\>unzip32 watt32d.zip -d c:\net\watt
在解压缩过程中,有一些共用文件会产生覆盖,没有关系,覆盖所有的文件。
在环境变量中增加变量:WATT_ROOT
需要修改autoexec.bat,增加下面一行:
set WATT_ROOT=c:\net\watt
然后重新启动计算机。
产生make文件
c:\>cd\net\watt\src
c:\net\watt\src>configur djgpp
这一步完成后会看到提示,要求你执行make -f djgpp.mak,照做就好了。
生成WATT-32库
照上一步的提示
c:\net\watt\src>make -f djgpp.mak
这个步骤时间比较长,需要耐心等待一会。在编译过程中会有一些“警告”出现,不用管它们。
为使用WATT-32库配置环境变量
在编译完成后,我们还要在autoexec.bat里增加四个环境变量,我们在步骤4中增加的WATT_ROOT环境变量仅在编译的过程中有用,实际使用中并不需要这个环境变量,所以可以去掉(当然,不去掉也没有关系)。
在autoexec.bat中增加下面四行:
set WATTCP.CFG=c:\net\watt\bin
set ETC=c:\net\watt\bin
set C_INCLUDE_PATH=c:/net/watt/inc
set LIBRARY_PATH=c:/net/watt/lib
WATTCP.CFG是WATT-32的配置文件wattcp.cfg所在的位置,你也可以把wattcp.cfg放在其他目录下,比如:c:\net\cfg目录下,但要记得把set WATTCP.CFG=c:\net\watt\bin这句改成:
set WATTCP.CFG=c:\net\cfg
至此,安装已经完成,应该可以在c:\net\watt\lib目录下看到文件libwatt.a,这就是我们需要的网络函数库。
此时,可能仍然不能进行网络编程,还需要实际配置一下wattcp.cfg文件,前面提到,该文件放置在c:\net\watt\bin目录下,我们可以在该目录下看到该文件的样板,至少我们要在配置文件中配置IP地址和地址掩码,类似下面的形式:
my_ip=192.168.0.10
mask=255.255.255.0
有时,还需要配置网关和解析服务器,类似下面:
gateway=192.168.0.1
nameserver=202.106.134.133
nameserver可以写一个或者多个,每个解析服务器占一行。
一般情况下,配置这四个参数就足够了,如果希望配置更多的参数可以参考wattcp.cfg中的说明。
学习DOS下的网络编程,有一篇文章很值得一读,《Beej's Guide to Network Programming Using internet Sockets》,写此文时,其最新版本已经是2.4.5,2007年8月5日完成的,我最初看到这篇文章的版本还是1.5.5,是1999年1月13日写的,看来作者还在不断更新,这篇文章的主页在:
http://beej.us/guide/bgnet/
该文的1.5.5版在网上有多个版本的中文译本,以下是其中的一个:
http://www.chinalinuxpub.com/doc/pro/is.html
你也可以在下面地址下载到2007年8月5日写的该文的2.4.5版,PDF格式:
http://blog.hengch.com/article/bgnet_A4_2.pdf
该文中所提到的数据结构及函数均为BSD规范下的网络编程的数据结构和函数,在WATT-32库中均适用。
另外,读者可以在下面地址下载到关于WATT-32库中的所有函数的使用说明:
http://blog.hengch.com/Manual/watt-32.chm
实际上,WATT-32并没有出一本完整好用的手册,而是沿用了WATTCP的手册,但由于WATTCP的手册是收费的,所以在此不便公开,有对此手册感兴趣的读者可以访问下面网站并索取:
http://www.erickengelke.com/wattcp/docs.shtml
在WATT-32的bin目录下还有很多子目录,里面有很多的范例程序,在开始进行网络编程前,可以先看看这些范例程序,下面我们拿其中的一个范例来说明,如何在RHIDE下编译含有WATT-32库的程序。
我们以ftpsrv范例来说明如何在RHIDE下编译,首先进入该范例的目录,并用RHIDE打开范例程序:
c:\>cd\net\watt\bin\ftpsrv
c:\net\watt\bin\ftpsrv>rhide ftpsrv
这样,RHIDE会自动建立一个ftpsrv的project,目前该project中没有任何项目,按下面步骤把程序ftpsrv.c加入到project中:
alt+p-->选择Add Item-->选择ftpsrv.c-->回车-->按Esc键退出
这样我们就在屏幕下方的Project Window中看到了一个项目:ftpsrv.c,此时如果你选择编译链接(按ALT+C再选择Make),会在链接时产生一些错误,这是由于我们没有把WATT-32库链接进去的原因,按下面方法操作:
ALT+O-->选择Libraraies-->填入watt-->按SHIFT+TAB(此时光标应停在watt前的上-->按空格(看到)-->回车
此时再按如下步骤进行编译链接,就可以生成ftpsrv.exe。
ALT+C-->选择Make-->回车
至此,我们学会了在DJGPP下安装、配置WATT-32的过程,同时学会了在DJGPP下使用RHIDE编译使用WATT-32库的程序,我们已经做好了进行网络编程的准备。
更多关于DOS编程的文章看我的网志
点击进入《DOS编程技术》
Last edited by whowin on 2008-5-9 at 11:50 AM ]
The article is pasted from my blog. The pictures may not come through. For the complete content, please visit my blog:
Click to enter "DOS Programming Technology"
Network programming under Windows is not a very difficult task, but it's not easy at all under DOS. For many people, it's even hard to start. This article elaborates in detail the methods of network programming under DOS, and the next article will give a specific example.
To carry out network programming under DOS, first of all, there should be a Packet Driver. It's a hardware-related driver that conforms to the PC/TCP Packet Driver specification proposed by FTP Software. Readers who are interested can download this specification at the following address:
http://blog.hengch.com/specification/packetdriver.pdf
The network chip on the motherboard of the machine I use is rt8139. The corresponding Packet Driver is rtspkt.exe. The method of driving is to add the following line in autoexec.bat:
@rtspkt 0x62
Among them, 0x62 is the interrupt vector. If this interrupt vector is already occupied on your machine, you can change it to other unoccupied interrupt vector numbers. According to the PC/TCP specification, it should be between 0x60----0x80.
After having the Packet Driver, we also need a useful function library that can provide TCP/IP Socket programming interfaces. Under DJGPP, we suggest using the WATT-32 library. This library has more abundant content than the WATTCP library provided on the DJGPP official website, and it has complete documents and rich sample programs. It can be downloaded at the following website:
http://www.bgnett.no/~giva/
WATT-32 is released in the form of source code, so it needs to be compiled and linked by yourself before use. The whole process is as follows (the following steps are based on the fact that you have correctly installed DJGPP and RHIDE according to the previous blog article "Installation and Implementation of DJGPP + RHIDE under DOS"):
First, download WATT-32 from the above website. There are 3 zip packages, as follows:
watt32b*.zip, watt32s*.zip, watt32d*.zip
Among them, "*" will be different with different version numbers.
Copy the 3 files to the machine to be configured through a U disk or other media. Since DOS does not support long file names, the three files need to be renamed respectively: watt32b.zip, watt32s.zip and watt32d.zip
Unzip the three files to a subdirectory, for example: c:\net\watt
c:\>md net
c:\>md net\watt
c:\>unzip32 watt32b.zip -d c:\net\watt
c:\>unzip32 watt32s.zip -d c:\net\watt
c:\>unzip32 watt32d.zip -d c:\net\watt
During the unzipping process, some shared files will be overwritten. It doesn't matter, overwrite all the files.
Add the variable WATT_ROOT in the environment variable.
Need to modify autoexec.bat and add the following line:
set WATT_ROOT=c:\net\watt
Then restart the computer.
Generate the make file
c:\>cd\net\watt\src
c:\net\watt\src>configur djgpp
After this step, you will see a prompt asking you to execute make -f djgpp.mak, just do it.
Generate the WATT-32 library
Follow the prompt of the previous step
c:\net\watt\src>make -f djgpp.mak
This step takes a long time, and you need to be patient. There will be some "warnings" during the compilation process, just ignore them.
Configure the environment variables for using the WATT-32 library
After the compilation is completed, we also need to add four environment variables in autoexec.bat. The WATT_ROOT environment variable added in step 4 is only useful during the compilation process, and it is not needed in actual use, so it can be removed (of course, it doesn't matter if you don't remove it).
Add the following four lines in autoexec.bat:
set WATTCP.CFG=c:\net\watt\bin
set ETC=c:\net\watt\bin
set C_INCLUDE_PATH=c:/net/watt/inc
set LIBRARY_PATH=c:/net/watt/lib
WATTCP.CFG is the location of the configuration file wattcp.cfg of WATT-32. You can also put wattcp.cfg in other directories, such as: c:\net\cfg directory, but remember to change the line set WATTCP.CFG=c:\net\watt\bin to:
set WATTCP.CFG=c:\net\cfg
So far, the installation has been completed. You should be able to see the file libwatt.a in the c:\net\watt\lib directory, which is the network function library we need.
At this time, network programming may still not be possible. It is also necessary to actually configure the wattcp.cfg file. As mentioned earlier, this file is placed in the c:\net\watt\bin directory. We can see the sample of this file in this directory. At least we need to configure the IP address and address mask in the configuration file, similar to the following form:
my_ip=192.168.0.10
mask=255.255.255.0
Sometimes, it is also necessary to configure the gateway and the resolver, similar to the following:
gateway=192.168.0.1
nameserver=202.106.134.133
There can be one or more nameservers, and each resolver occupies one line.
Generally speaking, configuring these four parameters is enough. If you want to configure more parameters, you can refer to the instructions in wattcp.cfg.
To learn network programming under DOS, there is an article that is very worth reading, "Beej's Guide to Network Programming Using internet Sockets". When this article was written, its latest version was already 2.4.5, completed on August 5, 2007. The version I initially saw of this article was still 1.5.5, written on January 13, 1999. It seems that the author is still updating it continuously. The homepage of this article is:
http://beej.us/guide/bgnet/
The 1.5.5 version of this article has multiple Chinese translations on the Internet. The following is one of them:
http://www.chinalinuxpub.com/doc/pro/is.html
You can also download the 2.4.5 version of this article written on August 5, 2007 in PDF format at the following address:
http://blog.hengch.com/article/bgnet_A4_2.pdf
The data structures and functions mentioned in this article are all the data structures and functions for network programming under the BSD specification, and they are all applicable in the WATT-32 library.
In addition, readers can download the usage instructions for all functions in the WATT-32 library at the following address:
http://blog.hengch.com/Manual/watt-32.chm
In fact, WATT-32 has not produced a complete and easy-to-use manual, but has continued to use the manual of WATTCP. But since the manual of WATTCP is chargeable, it is not convenient to disclose it here. Readers who are interested in this manual can visit the following website and request it:
http://www.erickengelke.com/wattcp/docs.shtml
There are also many subdirectories in the bin directory of WATT-32, and there are many sample programs in them. Before starting network programming, you can first look at these sample programs. Let's take one of the samples to illustrate how to compile a program containing the WATT-32 library under RHIDE.
We take the ftpsrv sample to illustrate how to compile under RHIDE. First, enter the directory of this sample and open the sample program with RHIDE:
c:\>cd\net\watt\bin\ftpsrv
c:\net\watt\bin\ftpsrv>rhide ftpsrv
In this way, RHIDE will automatically create a project of ftpsrv. At present, there are no items in this project. Follow the steps below to add the program ftpsrv.c to the project:
alt+p-->select Add Item-->select ftpsrv.c-->press Enter-->press Esc key to exit
In this way, we can see an item: ftpsrv.c in the Project Window at the bottom of the screen. At this time, if you choose to compile and link (press ALT+C and then select Make), some errors will occur during the linking process. This is because we have not linked the WATT-32 library. Operate as follows:
ALT+O-->select Libraraies-->enter watt-->press SHIFT+TAB (at this time, the cursor should stop on the before watt-->press space (see )-->press Enter
Then compile and link according to the following steps, and ftpsrv.exe can be generated.
ALT+C-->select Make-->press Enter
So far, we have learned the process of installing and configuring WATT-32 under DJGPP, and also learned how to compile a program using the WATT-32 library under DJGPP with RHIDE. We are ready for network programming.
For more articles about DOS programming, please visit my blog
Click to enter "DOS Programming Technology"
Last edited by whowin on 2008-5-9 at 11:50 AM ]