版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、windows下c语言网络编程快速入门(Windows C language network programming quick start)Windows C language network programming quick startSource: Author blog:/i_like_cpp/C language learning, the general way is to learn C, and then C+, it is best to have assembly language and microcomputer fundamentals, and then the
2、Visual C+. In this way, it takes a lot of time and stamina for learners. And in school teaching, there is no time to study the practical techniques of Windows programming.In fact, with the C language foundation, there are some basic concepts of C+ class, you can directly learn Windows C programming.
3、First, move up to Windows C languageMany languages have shown a Hello, World, as the first entry program, and the first program in C language is this:#include(main)Printf (Hello, World!);If the main function is written as a main function with arguments, it should be:#includeMain (int, arge, char, *a
4、rgv)Printf (Hello, World!);Windows Cs first program and the program in the form and principle are consistent, but there are two different:The 1. principal function receives the formal parameter, not just the number of strings in the command line, and the first address of the string.Many functions in
5、 the 2. C language can continue to be used in Windows C, but functions such as printf (), screen display, etc. cannot continue. Because Windows is a multitasking operating system, the screen is no longer unique to an application. Windows C applications display strings and need to use the API functio
6、ns provided by Windows to open their own windowsHere is a simple Windows C program that shows Hello, World.:#includeAPIENTRY WinMain (HINSTANCE, hInstance, HINSTANCE, hPrevInstance),LPSTR, lpCmdLine, int, nCmdShow)MessageBox (NULL, Hello, World), the first Windows C program, MB_OK|MB_ICONASTERISK);T
7、he primary function has four parameters:1) Hinstance: the handle of the current instance at the time the receiver is running;2) HprivInstance: a handle to the previous instance;3) LpCmdLine: program command line pointer;4) NcmdShow: an integer that specifies the way the window displays.The use of th
8、ese parameters, we will study in depth introduced.Show Hello, Word, string, and we use a MessageBox function that displays a dialog box on the screen, and its prototype is:Int, MessageBox (HWND, hWnd, LPCTSTR, lpText, LPCTSTR, lpCaption, UNIT, uType)The four parameters are:1) HWnd: the handle of the
9、 parent window;2) LpText: pointer to display string;3) LpCaption: pointer to the dialog box header string;4) UType: the type of the small icon displayed on the dialog box.Use this function to include the windows.h header file.What about debugging? A first Windows C program dialog box pops up on the
10、window. There is a line on it: Hello, World!.The world is really beautiful!Deep programming:In C, the declaration of a function, if it does not specify the return value type, and defaults to void, the main function of this program has no return value. However, in Windows programming, wed better deve
11、lop a good habit of specifying the return value type of a function, because in C+, the function returns the value type that cannot be default. While we are programming in Windows C, we will still use some of the concepts of C+, which will be helpful for further study in the future.The procedure for
12、regulating a bit should be like this:#includeInt, APIENTRY, WinMain (HINSTANCE, hInstance, HINSTANCE, hPrevInstance),LPSTR, lpCmdLine, int, nCmdShow)MessageBox (NULL, Hello, World), the first Windows C program, MB_OK|MB_ICONASTERISK);Return 0;Here, we declare type int and return a value of 0, so tha
13、t the function can be used in a more complex function call.In this section, we have mentioned the concept of handles in several places. The concept of handles and pointers is different. It is used as a value in the index table of the operating system,This prevents the application from accessing the
14、internal structure of the name object directly, reflecting the superiority of Windows resource management. For example, when a window is opened, it corresponds to a memory block in memory. The memory address in this window is often dynamically adjusted by the operating system, but it does not change
15、 with it. However, you can access this window through it, so you can use it as a pointer when you use it.Two. Get the hostname and IP address of the local computerLike the C language, functions are the basic unit of Windows C programming. However, Windows C mainly uses the API function, while networ
16、k programming mainly uses the API function provided by Winsock.Winsock is the beginning of the 90s, in order to facilitate network programming, network programming interface by Microsoft combined with several other companies jointly developed a set of WINDOWS, it is available to users and software d
17、evelopers through the dynamic link library C language, mainly by the winsock.h header files and dynamic link library winsock.dll, there are currently two version: Winsock1.1 and Winsock2.0.On the Win32 platform, access to many grassroots network protocols, Winsock is the preferred interface.When you
18、 compile the Windows C program using Visual C+6.0, you should first add wsock32.lib to its library module when you use the Winsock API function, but when you are linking, there is a error LNK2001 error. The steps to add wsock32.lib are: open the Project menu, select settings, click the link tab in t
19、he pop-up Project settings dialog box, and then add wsock32.lib in the object / library module text box.The most simple network programming is to obtain the local host name and IP address, the program uses the WSAStart (WSAClenaup), (gethostname), (gethostbyname), Winsock (four) API function, functi
20、on and usage of the four functions described below:1.WSAStartup ():function prototypeInt, PASCAL, FAR, WSAStartup (WORD, wVersionRequired, LPWSADATA, lpWSAData);instructions for useEvery application that uses Winsock must have a WSAStart function call and can use other Winsock network operating func
21、tions only after the call is successful.The WVersionRequired: indicates the version of Winsock you want to use. This is an integer of type WORD. Its high byte defines the secondary version number, and the low byte defines the main version number.The LpWSAData: is a pointer to the WSADATA data. We do
22、nt usually use this information.Return value: the call successfully returns 0; otherwise, an error message is returned.2.WSAClenaup ():function prototypeInt, PASCAL, FAR, WSACleanup (void);instructions for useAfter Winsock is used, the WSACleanup function is called to shut down the network device in
23、 order to release the resources it occupies.3.gethostname ()function prototypeInt, PASCAL, FAR, gethostname (char, FAR * name, int, namelen);instructions for useThis function can obtain the host name of the local host, among which:Name: the pointer used to point to the buffer of the acquired host na
24、me.The size of the Namelen: buffer, in bytes.Return value: if no error is returned, 0; otherwise, return the error generation?.4.gethostbyname ()function prototypeStruct, hostent, FAR * PASCAL, FAR, gethostbyname (const, char, FAR * name);instructions for useThis function can obtain the correspondin
25、g host from the host name database.The unique parameter name of this function is the host name that was previously called the function gethostname (). If no error has been returned, a pointer to the hostent structure is returned, which identifies a list of hosts.The Hostent structure is defined as f
26、ollows:Struct hostentChar FAR * h_name;Char, FAR, FAR * * h_aliases;Short h_addrtype;Char, FAR, FAR * * h_addr_list;Among them:H_name: host name address (PC).H_aliases: an air stop array consisting of host alternate names.H_addrtype: returns the type of address. For Winsock, this field is always PF_
27、INET.H_lenth: the length of each address (bytes),The corresponding PF_INET field should be 4.H_addr_list: a list of host addresses that should be terminated with null pointers. The returned addresses are arranged in a network order.Where h_addr_list0 stores the 4 byte IP address of the local host, t
28、hat is, the:H_addr_list00.h_addr_list01.h_addr_list02.h_addr_list03A simple message box to display the host name and IP address of the source, as follows:#includeInt WSA_return;WSADATA WSAData;HOSTENT *host_entry;Char host_name256;Char host_address256;Int, APIENTRY, WinMain (HINSTANCE, hInstance, HI
29、NSTANCE, hPrevInstance),LPSTR, lpCmdLine, int, nCmdShow)WSA_return=WSAStartup (0 x0101, &WSAData);If (WSA_return=0)Gethostname (host_name, 256);Host_entry=gethostbyname (host_name);If (host_entry, =0)Wsprintf (host_address, %d.%d.%d.%d.,(host_entry-h_addr_list00&0 x00ff),(host_entry-h_addr_list01&0
30、x00ff),(host_entry-h_addr_list02&0 x00ff),(host_entry-h_addr_list03&0 x00ff);MessageBox (NULL, host_address, host_name, MB_OK);WSACleanup ();Return 0;Deep programming:The front display when the IP address, we are using the message box, a programming specification should use the dialog box, how to ed
31、it a dialog box, there are a lot of the book, the editor dialog can run interface reference figure 5.Header file Get_IP.h is as follows:BOOL, APIENTRY, Hostname_ipDlgPro (HWND, hDlg, UINT, message, WPARAM, wParam, LPARAM, lParam);This program uses only one dialog box procedure, and generally puts the declaration of the process in the header file.Source program Get_IP.c:#include#include Get_IP.h#include resource.h / / this header file in the creation of resources will automatically generate,/ / and will automatica
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 某省重点项目门窗工程质量通病防治施工方案-施工方案
- 2026年麻醉药品、精神药品管理考核试题(附答案)
- 急诊急救护理不良事件管理护理查房
- 钢结构高空作业工程安全专项施工方案
- 泡沫灭火系统安装专项施工方案
- 安全生产风险应急责任制管理规定
- 2026年全员安全培训考试试题及答案解析
- 地面大理石铺贴记录
- 第三单元核心素养测试评价卷-2025-2026学年三年级上册语文(人教版)
- 关务水平测试关务基础知识-关务基本技能参考题库含答案解析
- 老年高血压特点及临床诊治流程专家共识(2024)解读
- 服装厂生产车间员工劳务合同书(34篇)
- 会计从业会计基础
- 2024办公桌椅购销合同范本
- (正式版)SHT 3046-2024 石油化工立式圆筒形钢制焊接储罐设计规范
- 装配工人培训产品装配与质量控制
- JGJ114-2014 钢筋焊接网混凝土结构技术规程
- 2023滚动轴承汽车变速箱用滚子轴承
- 建筑工地三级安全教育卡
- JJG 176-2022声校准器
- GB/T 3906-20203.6 kV~40.5 kV交流金属封闭开关设备和控制设备
评论
0/150
提交评论