[Network programming/C ++] string Link

Source: Internet
Author: User

After a period of study, the console version of the PXE simulator has been completed, so we are using MFC to beautify the interface.

In order to simulate multiple machines simultaneously request data to a TFTP server and download files. To maintainProgramMulti-process instead of multi-thread.

In this process, the link of the string is involved. During the packet construction process, this knowledge point also appears:

The following describes a common method of string link through two instances:

 

// Constructor, for example, "command param1" doscommand </P> <p> // example: C:> tftpmanagement.exe 5 </P> <p> tchar Command [maxprocess]; <br/> char cnumber [maxprocess]; </P> <p> tchar * PTR = command; <br/> int number = 5; </P> <p> memcpy (PTR, "C: // tftpmanagement.exe", sizeof ("C: // tftpmanagement.exe ") -1); <br/> PTR + = sizeof ("C: // tftpmanagement.exe")-1; <br/> memcpy (PTR ,"", sizeof (tchar); <br/> PTR ++; </P> <p> memset (cnumber, '/0', sizeof (cnumber )); <br/> ITOA (I + 1), cnumber, 10); // convert the int type to an integer <br/> memcpy (PTR, cnumber, strlen (cnumber )); </P> <p> // The obtained command is the desired string.

 

 

 

Similarly, we can use this method to construct network data packets. Here we use the TFTP packet request package as an example:

 

 

/* <Br/> Figure 1: rrq/WRQ </P> <p> 2 bytes string 1 string 1 string 1 string 1 <br/> + ------- + ---~~ --- + ---~~ --- + ---~~ --- + ---~~ --- + --> <Br/> | OPC | filename | 0 | mode | 0 | opt1 | 0 | value1 | 0 | <br/> + ------- + ---~~ --- + ---~~ --- + ---~~ --- + ---~~ --- + --> <Br/> ------- + ---~~ --- + <Br/> <OPTN | 0 | valuen | 0 | <br/> ------- + ---~~ --- + <Br/> */</P> <p> struct tftp_rrwq <br/> {<br/> uint16_t opcode; <br/> char filename [filelength]; <br/> byte flag1; <br/> char mode [10]; <br/> byte flag2; <br/> char options [optionsize]; <br/>}; </P> <p>/* <br/> Description: Creates a rrq data packet. <br/> return: data packet size, data packet pointer <br/> */</P> <p> int createtftprrq () <br/>{< br/> struct tftp_rrwq package; <br/> char * PTR = tftpdata; </P> <p> package. opcode = htons (1); </P> <p> memcpy (PTR, (char *) & package. opcode, sizeof (package. opcode); <br/> PTR + = sizeof (package. opcode); </P> <p> strcpy (package. filename, filename. c_str (); </P> <p> memcpy (PTR, package. filename, strlen (package. filename); <br/> PTR + = strlen (package. filename); </P> <p> package. flag1 = 0x00; <br/> memcpy (PTR, & package. flag1, sizeof (package. flag1); <br/> PTR + = sizeof (package. flag1); </P> <p> strcpy (package. mode, "netascii"); // netascii for ASCII files, octet for binary files <br/> memcpy (PTR, package. mode, strlen (package. mode); <br/> PTR + = strlen (package. mode); <br/> package. flag2 = 0x00; <br/> memcpy (PTR, & package. flag2, sizeof (package. flag2); <br/> PTR + = sizeof (package. flag2); </P> <p> // confignate options <br/> char * P = package. options; <br/> memcpy (P, "blksize", sizeof ("blksize"); <br/> P + = strlen (package. options); <br/> memset (p, 0x00, sizeof (byte); <br/> P + = sizeof (byte ); <br/> memcpy (P, "512", sizeof ("512"); <br/> P + = strlen ("512 "); <br/> memset (p, 0x00, sizeof (byte); <br/> P + = sizeof (byte ); </P> <p> memcpy (PTR, & package. options, (p-package.options); <br/> PTR + = (p-package.options); <br/> return (PTR-tftpdata); <br/>}< br/>

 

Note: The tftp protocol is the latest protocol, which is more optimized than the traditional 1350 in terms of performance and security. For details, see rfc2347, 2348, 2349.

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.