Use the wattcp library to implement TCP/IP communication in real-mode DoS (complete at last)

Source: Internet
Author: User

Previous articles are here:

 

Implement TCP/IP programming under DOS real model (I)

In short, I was very busy recently. I thought it was really time-consuming to make a summary of the system. I wrote half of the previous article. I wanted to simply write the process from environment setup to coding, but let's take a closer look at the previous article, which involves a lot of nonsense and has no direct cutting topic. I want to edit it again...

 

 

// --- Body --------------

 

First, let's take a look at the topic: in the DOS environment, the real mode (not djgpp) uses the wattcp library to communicate with TCP/IP. Note the following:

1,DOS does not have a fixed IP address. For example, after the DOS system is running, the computer connects to the router. At this time, the DOS system does not have an IP address (do not think that the computer can be pinged or even the IP address does not exist. What can be pinged ?), When a program supporting packet Driver runs on the DOS system, DOS has a fixed IP address. At this time, the DOS host can be pinged;

 

2,Wattcp library, a fully open-source socket library under DOS, has sufficient and stable functions, but the interface manual is charged -_-!! Library files: http://dos5gw.download.csdn.net /;

 

3, Wattcp library is based on Packet driver NIC Driver specification, so you must find your Nic packet driver Type Driver, see the http://blog.csdn.net/dos5gw/archive/2010/01/29/5267737.aspx for details, the Network Copy function in Ghost is based on wattcp library completed; the NIC Driver Installation Method in DOS is special. The driver is generally *. com or *. EXE file, first copy the NIC Driver to any path, such as C:/driver, and then in autoexec. add "@ NIC Driver name Nic interrupt number" to the BAT file, for example:

 

Path = C:/; C:/dos7; C:/driver;

@ E100bdos 0x62

 

Note that in the second line, the model of the industrial computer I used is Yanhua 3355. The NIC chip is Intel 82551qm, the driver is "e100bdos.exe", 0x60 indicates Nic interruption, or 0x61,0x62, theoretically, the 0x60 ---- 0x80 interrupt number is acceptable;

 

4, Open bc31 and create an empty project (it is not recommended to create a new project here, because the project in bc31 has many options, including Compiler Optimization Options and floating point processing options, so find a project template,)

 

5,The wattcp Library/lib directory is the Library (wattcphg. LIB/wattcplg. LIB/wattcpsm. lib corresponds to the giant/big/micro mode respectively), add to your bc31 project, ALT + p --> Add-> select the corresponding *. lib file. Select the large or huge mode;

 

6,The wattcp library has a necessary configuration file "TCP. cfg ", placed in the current program directory. For example, if the program is executed in the C:/tcpprogreme/path, there must be a TCP in this path. CFG, as shown below,

My_ip = 192.168.188.2

Netmask = 255.255.255.0

Gateway = 192.168.188.252

 

7,Wattcp provides a C function library. To compile a C ++ program, add the following command before including the header file:

# Ifdef _ cplusplus
Extern "C "{

# Include "wattcp. H"
}
# Endif

8, In this way, we can reference the functions in wattcp. The following describes several important functions. wattcp provides many similar functions that can be selected. In fact, we can't use so many functions. Just remember the following,

(1) void sock_init (), read the setting parameters in TCP. cfg, initialize the protocol stack,

 

(2) struct tcp_socket, a very important struct, equivalent to the handle of the current socket;

 

(3) void sock_mode (tcp_socket * s, word mode); Set Transmission Mode

 

(4) int tcp_listen (tcp_socket * s, word lport, longword Ina, word port, INT (* signal_handler), word timeout); generally used on the server, for example, if you want to implement a server on the DOS side and enable a listener, the second parameter lport is the local listening port;

 

(5) int tcp_open (tcp_socket * s, word lport, longword Ina, word port, INT (* signal_handler); generally used on the client side to Request Remote socket connections. the port parameter is the peer listening port;

 

(6) Word sock_established (tcp_socket * s); waiting for the connection to be established, generally used on the server end, after the listen function;

 

(7) Word tcp_tick (tcp_socke * s); Protocol Stack execution, note that this function should be in the program
Run the command once every other time and put it in a loop (is the program under DOS originally a loop -_-!!), If the function is not executed for a long time, the result is ~ Not clear ~

 

(8) int sock_gets (tcp_socke * s, char * Text, int Len) and char sock_puts (tcp_socke * s, char * Text) are read and write functions are easy to use, similar to puts () and gets (). For example, sock_puts (& S, "Hello wattcp") sends text messages to the peer end. These two functions can only be in sock_mode (S, tcp_mode_ascii ); the two read/write functions are not recommended, because if your data buffer char [] contains "0x00", that is, the ASC code '/0 ', then sock_puts will regard it as the string Terminator and discard the character after the first '/0;

 

(9) int sock_fastread (tcp_socke * s, byte * DP, int Len) and INT sock_write (tcp_socke * s, byte * data, int Len); binary read/write functions, the returned value is the number of successfully read/write characters. We recommend that you use these two types of read/write operations so that no (8) problem occurs;

 

(10) do not use int sock_read !! The difference between int sock_fastread and it is that sock_read is a blocking function. For example, after sock_read (& sock, receivebuff, Len) is executed, the function will always block this function until it reads len bytes, sock_fastread will not be blocked;

 

9,Sample Code 1: server code:

(1) -- wattest. h -- File

# DEFINE _ cplusplus <br/> # ifdef _ cplusplus <br/> extern "C" {<br/> # include "TCP. H "<br/> typedef unsigned long DWORD; <br/>}< br/> # endif 

(2) -- wattest. cpp -- File

# Include "wattest. H "<br/> # include <stdio. h> <br/> # include <dos. h> <br/> # include <string. h> <br/> static tcp_socket s; // defines the handle to ensure the full-cycle validity of the Program <br/> unsigned char addr_buff [20]; <br/> unsigned char send_buff [30] = "transmission OK... "; <br/> unsigned char receive_buff [128]; <br/> unsigned char MSG [] =" Please send MSG to server "; <br/> int listen_port = 9109; // server listening port <br/> void main (void) {// void Mai supported in Borland C ++ N (), <br/> int status; <br/> sock_init (); // initialize the protocol stack <br/> sock_mode (& S, tcp_mode_binary ); // binary transmission mode <br/> printf ("_ sock_init success! /N "); <br/> tcp_listen (& S, listen_port, 0, 0, null, 0 ); // listen to the specified port <br/> printf ("_ sock_listen @ %-16 s: % 5D! /N ", inet_ntoa (addr_buff, gethostid (), listen_port); <br/> while (! Sock_established (& S) // wait for client connection <br/>{< br/> tcp_tick (& S); // protocol stack execution <br/> delay (100 ); <br/>}</P> <p> printf ("TCP has been sock_established... /n "); <br/> // print this sentence, indicating that it has been connected <br/> sock_write (& S, MSG, sizeof (MSG )); // send to the client <br/> // sock_puts (& S, "connected! /N "); // send information to the server </P> <p> do {</P> <p> If (sock_dataready (& S )) {// determine whether data is sent <br/> memset (receive_buff, '/0', 100); <br/> sock_fastread (& S, receive_buff, 100 ); // accept data <br/> printf ("Received: % s/n", receive_buff); <br/> sock_write (& S, send_buff, sizeof (send_buff )); // send <br/>}< br/>}while (tcp_tick (& S) to the client; <br/> // sock_established () after an error occurs, the default goto label <br/> sock_err: <br/> printf ("!!! Sock_wait_established fff error! /N "); <br/> sock_close (& S); <br/>}

10,If you want to write a client program in the DOS environment, see: http://www.wangchao.net.cn/bbsdetail_147812.html

 

 

 

--- End ---

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.