1. _ T () function
_ T ("") is a macro whose role is to allow your program to support Unicode encoding. Because Windows uses two character sets (ANSI and UNICODE), the former is the common single-byte method. However, this method is inconvenient to process double-byte characters such as Chinese, and it is prone to the situation of half a Chinese character. The latter is in dubyte mode to facilitate the processing of dubyte characters.
All character-related functions of Windows NT provide two versions, while Windows 9x only supports ANSI. If you compile a program in ANSI mode, _ T does not actually play any role. If a program is compiled in UNICODE mode, the compiler saves the "Hello" string in UNICODE mode. The difference between _ T and _ L is that _ L is saved in UNICODE no matter how you compile it.
- LPSTR: The 32bit Pointer Points to a string. Each character occupies 1 byte.
- LPCSTR: 32-bit pointer to a regular string, each character occupies 1 byte.
- LPCTSTR: 32-bit pointer to a regular string. Each character may take 1 or 2 bytes, depending on whether Unicode is defined.
- LPTSTR: 32-bit pointers may occupy 1 or 2 bytes per character, depending on whether Unicode is defined.
L indicates that the string resource is Unicode. For example, wchar_t Str [] = L "Hello World! "; This is the character stored in the double section.
_ T is an adaptive macro. When # ifdef _ UNICODE, _ T is L. No # ifdef _ UNICODE, _ T is ANSI.
For example:
LPTSTR lpStr = new TCHAR[32];TCHAR* szBuf = _T("Hello");
The preceding two statements make it correct for UNICODE compiling. In addition, we recommend that you use matching string functions in MS. For example, when processing LPTSTR or LPCTSTR, do not use strlen, but use _ tcslen. Otherwise, strlen cannot process the wchar_t * string in UNICODE compiling.
T is a very interesting symbol (TCHAR, LPCTSTR, LPTSTR, _ T (), _ TEXT ()...), it indicates the use of an intermediate type, which neither explicitly indicates the use of MBCS nor explicitly indicates the use of UNICODE. Which character set is used? It is determined during compilation.
Converts a char string to a Unicode string. T is a macro, not a function. A double-byte string is required in a function that requires two bytes or a string of two bytes in COM. For international compatibility. It is actually very simple. When defining strings in Visual C ++, use _ T to ensure compatibility. VC supports ascii and unicode character types, when _ T is used to convert the ascii encoding type to the unicode encoding type, the program does not need to be modified.
If you do not plan to upgrade to unicode in the future, _ T is not required. _ t ("hello world") is ansi in the ansi environment. If it is unicode, it is automatically interpreted as a dual-byte string, both unicode encoding. These benefits apply to both the ansi and unicode environments.
What is the difference between string _ T ("ABC") in vc ++ and a common string "ABC? _ T ("ABC") indicates that if unicode is defined, it indicates L "ABC", each character is a 16-bit, wide character string. If not UNICODE, It is the ascii "ABC", each character is 8 characters. "ABC" refers to the ascii string "ABC ". Equivalent:
#ifdef _UNICODE#define _T("ABC") L"ABC"#else#define _T("ABC") "ABC"#endif
A character in _ T ("ABC") occupies two bytes, while an English character occupies one byte and a Chinese character occupies two bytes.
Ii. Stack and stack
1. "Heap" and "stack" in C ++ mainly refer to the areas where data or variables and objects are stored in memory, just like the differences between China and the US, on the surface, the locations in the memory are different, but the memory allocation methods are quite different. Although the United States and China are both countries on the surface, their social systems and various welfare institutions are different, in a word, it is different areas in the memory.
2. "Heap" in C ++: a heap in a program is a piece of memory, not a heap memory. Generally, it is manually allocated by programmers, in C ++, the memory allocated by the new operator is generally used (if the malloc series of C is not used, it seems that only new is used), and it can only be manually released by programmers. Otherwise, the memory will be leaked. Of course, to allocate memory, you can only allocate variables and class objects, so they are stored as class objects and variables.
3. "stack" in C ++: memory automatically allocated by the program. For example, when calling a function and passing parameters (passing values ), the value of this parameter is allocated on the "stack" memory. When this function exits, the "stack" memory occupied will be released.
Iii. Windows Socket
Socket implementation method:
Socket is a bridge between applications and network drivers. It is created in the application and establishes a relationship with the driver through the binding operation. After that, the application sends the Socket data, and the Socket sends the data to the driver. Then, the driver sends the data to the network. After the computer receives data related to the IP address and port number bound to the Socket from the network, the driver is handed over to the Socket, and the application can extract the received data from the Socket. Network applications use sockets to send and receive data.
ISO/OSI Layer-7 Reference Model (ISO: OSI: Open System Interconnection ):
- Physical Layer: Provides binary transmission to determine how bit streams are transmitted over communication channels.
- Data link layer: Provides media access, enhances the transmission function of the physical layer, and establishes an error-free transmission line.
- Network Layer: Provides IP Addressing and routing. Because data on the network can reach the destination through multiple lines, the network layer is responsible for finding the best transmission route.
- Transport Layer: provides reliable data transmission services between the source host and the target host, and isolates the upper-and lower-layer protocols of the network, so that network applications are independent of the lower-layer protocols.
- Session Layer: establishes, organizes, and coordinates communications between two communication application processes.
- Presentation Layer: processing the representation of transmitted data, that is, the syntax and semantics of information.
The essence of peer layer communication: virtual communication between peer layer entities. The lower layer provides services to the upper layer, and the actual communication is completed at the lowest layer.
Protocols used by each layer:
- Application Layer: Telnet, FTP, HTTP, DNS, SMTP, and POP3.
- Transport Layer: Transmission Control Protocol TCP and User Datagram Protocol UDP.
- Network Layer: Internet Protocol IP, Internet Control Packet protocol ICMP, and Internet Group Management Protocol IGMP.
TCP/IP model: application layer, transmission layer, network layer, and network interface layer.
The TCP/IP model and OSI model are compared as follows:
- Application Layer: application layer, presentation layer, and Session Layer.
- Transport Layer: transport layer.
- Network Layer: network layer.
- Network Interface Layer: data link layer and physical layer.
Port: an abstract software structure (including some data structures and I/O buffers ).
Socket Type:
- Stream socket (SOCK_STREAM) is implemented based on the TCP protocol.
- Datagram socket (SOCK_DGRAM), UDP protocol implementation.
- Original socket (SOCK_RAW)
For Windows Socket "TCP" network programming instances, some development steps are as follows.
"TCP" server:
- Load the socket library.
- Create a socket.
- The bound address.
- Bind a socket.
- Listening socket.
- "Accept" is waiting for user requests.
- "Send" sends data.
- "Recv" receives data.
- Close "socket in use" and continue the loop wait.
"TCP" client:
- Load the socket library.
- Create a socket.
- The bound address.
- Bind a socket.
- Send a request.
- Receive data.
- Send data.
- Close the socket.
- "WSACleanup" Ends "socket library. (The server has been running cyclically, but none of them exist ).
For UDP communication, you do not need to establish a connection and directly receive/send the connection.
4. Multithreading
1. programs and processes
A program is a set of computer commands that are stored on a disk as a file. A process is an instance of a running program. For example, the compiled "exe" file is an executable program and stored on a disk as a program. Click Run to start an instance of the program, which is called a process. A program can have multiple processes, that is, a program can generate multiple instances.
2. Processes and threads
A process never executes anything. It is the container of a thread and the thread that actually completes code execution. A single process may contain several threads, but at least one main thread.
3. process address space
The system assigns each process an independent virtual address space. For 32-bit operating systems, the address space of a 32-bit process is 4 GB. For 32-bit pointers, the addressing range is the power of 2, that is, 4G. This is why the memory size of a 32-bit operating system generally does not exceed 4 GB.
4. Virtual Memory.
There is a "pagefile. sys" file on the disk, which is a page file. The page file transparently adds memory for the application. This part is the virtual memory.
5. threads.
- Thread Kernel Object: A small data structure used by the operating system to store thread statistics.
- Thread Stack: it is used to maintain all function parameters and local variables required for the thread to execute code.
Different threads of the same process run in the same process environment, and they share resources, so they can easily implement communication between each other. The thread runs on the CPU time slice allocated by the operating system. For Single-core CPU, multiple threads need to switch back and forth. Multi-core CPU can implement concurrent execution of multiple threads. Multithreading rather than multi-process is adopted because the thread has only one kernel object and one stack, which consumes less memory. For each process, a 4 GB virtual address space is allocated, occupying a large amount of resources. In addition, process switching requires switching the entire address space. Switching between threads only changes the execution environment.