windows下c語言網(wǎng)絡(luò)編程快速入門(Windows C language network programming quick start)_第1頁
windows下c語言網(wǎng)絡(luò)編程快速入門(Windows C language network programming quick start)_第2頁
windows下c語言網(wǎng)絡(luò)編程快速入門(Windows C language network programming quick start)_第3頁
windows下c語言網(wǎng)絡(luò)編程快速入門(Windows C language network programming quick start)_第4頁
windows下c語言網(wǎng)絡(luò)編程快速入門(Windows C language network programming quick start)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、windows下c語言網(wǎng)絡(luò)編程快速入門(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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論