C language implementation TCPIP communication under DOS

xiaoxiao2021-03-06  16

This article comes from the implementation experience of TCP / IP communication between POS (DOS) and PC (Windows2000).

I really have a little better when I just took the task, I didn't start. What language is used? What compiler is used? How to achieve? Under the operating system of DOS, do you find the DOS driver of today's NIC, how to install drivers to NIC ..., which makes me doubts about the programmers who have not been developed by DOS operating system. Later, I found out the relevant information, I know that I have provided a free WATTCP library to implement the TCP / IP protocol stack in DOS earlier. Full of joyful downloads, but found that although the library does not want money, the documentation of the library is not a free lunch. Without the explanation of the document, the library is like a block of chicken, and the taste of food is inexpensive, and unfortunately. Later, I continued to go between Google, Baidu, 9CBS, my whole week, and I took the network of southeastern northwestern. Fortunately, I will encounter a person, to bear the guidance, plus my own exploration, and finally completed the task. Since this information is scattered on the domestic website, it is not easy to find, and the personnel engaged in the online programming of DOS is still (especially in the embedded field), I will bring the whole process from the head to the end. In this paper, it will come. This article aims to be able to take less detours for network developers who have to deal with DOS.

Start with this task from the beginning, I determined a direction: I have to find the library that can control the socket under DOS. If this is not, under limited time and the limited level, you must complete the TCP / IP protocol stack, it is basically unlikely. I called Ertos, from WatTcp library (not only, the TCP / IP protocol stack also implemented DOS more threads). I have to get this library and header file, I have to thank Chen Dao, a company in Zhuhai, said enthusiastic help.

The following is true, starting to teach you to complete the TCP / IP communication under DOS.

First, the environment

Compilation environment: Windows98 BC4.5 for DOS, the download address of the compiler in: http://www.alaum.com/down/bc45.rar

Client program running environment: DOS622

Services: VC 6.0, Windows2000sev

BC4.5 for DOS Project Creation:

After the installation is successful. Find the directory where BC4.5 is located, copy all the .h files under the include folder in, which is the header file that Ertostcp must be. Then open BC4.5, select Menu Project-> New Project. Set the engineering path, the project is called TCPC.IDE. Platform Select DOS (Standard), other defaults, click the OK button to create a new project. After the new engineering creation, the compiler created a .cpp file with the same name as the project, since the program is completed in the C language, so it does not involve the C syntax, so delete. Create a blank TCPC.c file in the project with a directory, all of the Ertostcp.lib, RTOS.LIB, and TCPC.C files provided here in the project view of BC4.5. This project is created.

Second, the NIC driver installation:

This library is driven as the underlying DOS Ethernet Packet Driver. Packet Driver is a hardware-related device driver. The Packet Driver of Different NICs is different. You can also find it in the NIC installation disk DOS directory or PCPKT directory. You can also download at www.simtel.net. Find the .com file (there may be several, you can try one by one as an example, I have used the NE2000 compatible network card and D-Link network card, found Packet Driver in their driver package. Put in DOS manually run .com file: NE2000 0x62 or DLKFET 0x62 (62 and 60 are interrupt numbers in the NIC, both). Network card information will appear after success, display the correct NIC MAC address, otherwise, prompt the error message or the broadcast address of the network card. Tell you that Packet Drive and NIC do not match. After the installation is successful, the system can run the TCP / IP program to load the command into the autoexec.bat file. Packet Drive needs to be reloaded after each system is started; in addition, it is recommended to add support for extended memory in Config.sys and to reside DOS to high-end. Here is an example of a complete config.sys file. Config.sys device = himem.sys dos = high, umb files = 40

Third, the code is written:

Prepare work, you can write the code next.

The following is an example of the client code:

#include

#include

#include

#include

#include

#include

#include

Void main (void)

{

Static TCP_Socket S;

Char Remotbuf [100] = "lanry";

Char Serverip [20] = "192.168.0.100"; // Server IP Address

CHAR Addrbuf [50];

Char szread [512];

DWORD HOST;

Int status;

INT rlen = 5;

SOCK_INIT (); / * Initialization Protocol Stack * /

Printf ("Wattcp.cfg Address IS: [% S] / N", INET_NTOA (Addrbuf, gethostId ()))));

Printf ("Connecting TO% S:% D / N", ServerIP, 9104);

Host = inet_addr (serverip); / * server-side IP * /

IF (! TCP_Open (& S, 0, Host, 9104, NULL) / * Connection Server * /

{

Printf ("Unable to Open TCPC Session / N");

Goto Sock_ERR;

}

Printf ("Wating to Be Establisd / N / R");

SOCK_WAIT_ESTABLISHED (& S, Sock_delay, Null, & status);

Printf ("Connection Is Successful! / N");

/ * The following code users can modify * /

While (1)

{

Rt_sleep (10);

TCP_TICK (NULL); // Execute the opportunity to the protocol stack

SOCK_WRITE (& S, (Byte *) remotebuf, rlen); / / send information to the server // receive information sent by the server

IF (SOCK_DATAREADY (& S))

{

MEMSET (Szread, 0, 512);

// No blocking receive data, another blocking receive data function is SOCK_READ

SOCK_FASTREAD (& S, (Byte *) szread, sizeof (szread));

}

}

SOCK_ERR:

PUTS ("Unable to connect to the server! / n");

SOCK_CLOSE (& S);

}

Compilation and running, do you have "NO Packet Drvier Found" under 98?

No problem, because Packet Driver is driven to pure DOS, so Socket initialization cannot be performed under 98.

Fourth, set IP:

The concept of IP under the DOS system is not just relative to the machine. Different TCP programs in the same machine can also have different IPs. The IP address of the machine can only be determined when the program is running. With the TCP program of the Wattcp library, its IP is set in the Wattcp.cfg file in the program. When Socket is initialized, the system looks up the configuration file in the current path and setup path, put the configuration file in the current path so that the system can find the configuration file. The smallest configuration file contains the following: Local IP address, subnet mask, gateway. E.g:

my_ip = 192.168.0.8 #ip Address

Netmask = 255.255.255.0 # 子 网 mask

Nameserver = 192.168.0.1 # Name Server

Gateway = 192.168.0.1 # gateway

Domainslist = "uWaterloo.ca" # domain list

The configuration file can contain any parameters required by any server or client. Such as the password of the FTP service, etc. If only C / S mode communication is performed, it is already enough to configure it as above.

V. Test

Since it is a C / S mode programming, there is a client to have a server, readers can write a simple server to test. A pair or normal network cable interconnect, the server is set to the DOS system, and the client is set to the DOS system using the PC and PC. Make sure the Wattcp.cfg is set correctly. You can run the program. Oh, is it seen that the server jumps in the breakpoint on the accept function? If the answer is affirmative, my article is also achieved!

Click to download source code

转载请注明原文地址:https://www.9cbs.com/read-44820.html

New Post(0)