Network programming tip

zhaozj2021-02-16  51

Network programming tip

(Author: mikespook | Date: 2003-7-4 | Views: 321)

Keywords: network, IP, hostname this article has submitted "hacking security base". The article is "hacker safety base", and it is not necessary to reprint without the "hacking security base". Thank you for your cooperation! Here will give you several commonly used network programmed tips. For the sake of convenience, I will use C Builder 5 to do demos. You should first make a formulation in C Builder 5. For tip one to four, you should add "$ (BCB) / lib / psdk" to library path in the Directories / Conditionals tab in Project Options (Figure 1). And add "#inclucde" wininet.h "in the header file (

.h). Add "USELIB (" Wininet.Lib ") in the .cpp file;". For tips five, six, as long as the header file ( .h) is added "#include" winsock2.h "". In the example below, SBMSG is an example of TSTATUSBAR. EDTURL, EdthostName is an example of TEDIT. BTNTIP1, BTNTIP2, BTNTIP3, BTNTIP4, BTNTIP5, BTNTIP6 are examples of TButton. First, try the function of network connection: InternettTemptConnect function protest: DWORD InternetTemptConnect (DWORD DWRESERVED); This function can be said to be very simple, just try the computer to connect to the network. I don't consider what way is used. When successfully connected to the network, the function returns ERROR_SUCCESS, that is, 0. The parameters of the function will always be 0. One thing to note is that when the computer is using "Kitten" is also a dial-up method, the dial-up connection is automatically activated when the Internet is executed. This is also a dial-up connection function that many software used. For example, Foxmail and Flashget. The procedure is as follows: void __fastcall tfrmmain :: btntip1click (TOBJECT * Sender) {if (error_success == InternettattemptConnect (0)) sbmsg-> simpletext = "Network connection is successful!"; Else SBMSG-> SimpleText = "Network connection failed!"; } Second, check the function of the remote host status: InternetCheckconnection function protest: BOOL InternetCheckConnection (LPCSTR LPSZURL, DWORD DWFLAGS, DWORD DWRESERVED); This function is the same, you can check if the remote host is connected. If the connection successfully returns true, if the connection fails returns false. The parameter LPSZURL is the IP address or URL of the remote host. DWFlags can only set a value of Flag_ICC_FORCE_CONNECTION, which is 0x00000001. DWRESERVED is a reserved parameter and can only be set to 0.

Procedure is as follows: void __fastcall TfrmMain :: btnTip2Click (TObject * Sender) {if (InternetCheckConnection (edtURL-> Text.c_str (), FLAG_ICC_FORCE_CONNECTION, 0)) sbMsg-> SimpleText = "network connection success!"; Else sbMsg-> SimpleText = "Network connection failed!";} III, check the local network connection status and obtain the function of network connection mode: InternetGetConnectedState function protest: BOOL InternetGetConnectedState (LPDWORD LPDWFLAGS, DWORD DWRESERVED); this function is functional. It can: 1. Judgment the network connection is through the NIC or by the treatment of the demodulator 2. Whether to access the Internet through the agent 3. Judgment the connection is ON line or off line 4. Judgment whether to install "Dial-up Network Service" 5. Judgment Treatment Strip Whether it is using it here we only need to use its first function. The parameter lpdwflags returns the current network status. Here we use the following two markers to judge. Internet_Connection_Modem By Treating the Demand Connection Network Internet_Connection_LAN through the LAN Connection Network Parameter DWRESERVED is still reserved, set to 0. When the function returns True, it will be described to connect to the network, otherwise it returns false. Procedure is as follows: void __fastcall TfrmMain :: btnTip3Click (TObject * Sender) {DWORD dwFlag; if (! InternetGetConnectedState (& dwFlag, 0)) sbMsg-> SimpleText = "The network is not connected."; Else if (dwFlag & INTERNET_CONNECTION_MODEM) sbMsg-> SimpleText = "Using the Treatment Strobe to access the network."; Else if (dwflag & Internet_connection_lan) sbmsg-> simpletext = "NIC Internet.";} II. Judging whether to use the proxy connection still use the above InternetGetConnectedState function. When the logo of the LPDWFLAGS determines that is Internet_connection_proxy, the local use agent is connected to the network. Procedure is as follows: void __fastcall TfrmMain :: btnTip4Click (TObject * Sender) {DWORD dwFlag; if (! InternetGetConnectedState (& dwFlag, 0)) sbMsg-> SimpleText = "The network is not connected."; Else if (dwFlag & INTERNET_CONNECTION_PROXY) sbMsg-> SimpleText = "Use a proxy connection."; Else sbmsg-> simpletext = "does not use the proxy connection.

"} 5, get the function of this machine IP address: gethostname, gethostByname, INET_NTOA function protest: int gethostname (CHAR FAR * Name, INT Namelen); Struct Hostent Far * gethostByname (Const Char Far * Name); Char Far * INET_NTOA (STRUCT IN_ADDR IN); function getHostName can get the local computer hostname. Parameter Name is the character pointer to save the hostname after calling the function. Parameter Namelen is the length of the Name parameter. Function getHostByName You can use the host name incorporated by the parameter Name A Hostent structure. This structure is as follows: struct hostent {char far * h_name; char far * far * h_aliases; short h_addrtype; short h_ength; char far * far * h_addr_list;}; where H_Name returns the host name; h_aliases return host alias H_addrtype returns the type of IP address; h_length returns the length of IP address; h_addr_list returns an IP address. This is especially true to H_Addr_List. Because a computer may have multiple valid IP addresses, h_addr_list is actually an array. And H_addr_list returns the IP address represented in the network bit order, but also needs to be converted to "." to be separated by "." to "XXX.xxx.xxx.xxx" by the inet_ntoa function. This form of IP address. Parameters IN is an in_addr structure. We just know how to use the inet_ntoa function, if you want to know more about you can check MSDN. The program is as follows: void __fastcall tfrmmain :: btntip5click (Tobject * sender) {Char ChostName [256]; ANSISUSTRING stradd; gethostname (chostname, sizeof (chostname) Hostent * host = gethostByname (chOSTNAME); if (null == host) sbmsg-> simpletext = "Gets an IP address error. "; Else {IN_ADDR ADDR; for (int i = 0; host-> h_addr_list [i]! = 0; i) // Note The cycle here {Memcpy (& addr, host-> h_addr_list [i], sizeof IN_ADDR); stradd = stradd "" STRPAS (inet_ntoa (addr));} sbmsg-> simpletext = "Native IP address is" stradd;}} This is that the cycle. I said earlike Because a computer may have multiple valid IP addresses, h_addr_list is actually an array.

转载请注明原文地址:https://www.9cbs.com/read-20332.html

New Post(0)